Considering New App - Hotkey Monitor
Posted: Sun Jan 31, 2021 2:36 pm
One of the ongoing challenges for having CMC work reliably from the comfort of your favorite chair is proper support for MCE Remotes and reliable app behavior when you press a button. Though I've spent countless hours trying to make CMC work reliably well with an MCE remote, the end result is still lacking - and for that I place the blame squarely on Windows.
For example, even though CMC creates a Windows Shortcut to launch CMC that includes a Hotkey combo to trigger the shortcut, Windows prevents the shortcut from executing if the target program is already running. While at first glance that seems like a fair result to prevent accidentally running multiple copies of a program, it breaks the ability of a 2nd copy of CMC to bring the initial instance to the top and into focus.
Additionally, Windows captures the hotkey combo and prevents running apps from seeing it. For example, CTRL+ALT+8 is the hotkey I programmed for launching CMC. If you press that combo while CMC is already running, CMC sees only "CTRL+ALT", and not the 8. So I can't even program CMC to monitor for that keystroke and take action, since Windows captures it.
I've also noticed that CMC takes a few extra seconds to launch via hotkey - I believe that this is Windows checking to see if the target program is already running before launching a new instance. As hard as I've worked to make CMC launch quickly, it frustrates me that Windows is slowing it down for a feature I don't even like.
So I have a couple ideas on how to tackle this challenge for a future version of CMC, and thought I would post them here for comment.
Add Additional Hotkeys for refocusing CMC
This first option is perhaps the simplest. CMC already has many hotkeys programmed into it, and they work pretty great. The idea here is to simply add a new hotkey that will refocus CMC and bring it to the top. You could map this hotkey to your remote, and any time you find CMC not responding because it has lost focus, pressing the configured button would hopefully resolve the issue.
This does have some potential problems. First of all, it requires you to dedicate an extra button on your remote for this behavior, and perhaps you are already running short of buttons. Second, if CMC has lost focus, then it probably won't even see the hotkey to respond to it. And third, Windows actively tries to block apps from stealing focus. Put altogether, I'm thinking that this is a pretty bad idea.
Create a new background process to monitor Hotkeys
This idea has been suggested to me by several users over the years, and I've been resistant to it simply because I felt I could solve the problem in CMC itself. Now that Windows has utterly defeated those efforts, I'm reconsidering this idea.
Basically, I would create a new standalone app that would run either in the System Tray or as a Background Process. It would monitor for certain hotkeys, and when it sees them it would take the appropriate action. It would be smart enough to know that if CMC is not running, then to launch CMC, but if CMC is running then to bring CMC into focus to respond to commands. I'm thinking that this app could reduce CMC launching time by 1-2 seconds.
There are some unknown challenges with this approach. First, I'm worried what a virus scanner will think of a background process that monitors keystrokes. It's very possible this will look like keylogger malware. I'm also not sure if I can successfully have it monitor for all keystrokes while running in the systray or as a background process. Basically, for this app to be successful, it needs to see every keystroke no matter what app is running. There's also the concern of what users may think of such an app. Are you guys open to the idea of a keystroke monitor running 24/7 on your HTPC's? Or is this crossing a line? I'm thinking use of such an app would have to be optional, of course, but if no one is willing to run such an app I don't think I should waste time programming it.
If I did create such an app, it would allow for some advanced functionality that is not possible today. For example, the current Wake-on-LAN solution on works if CMC is running, so that doesn't solve the need for users who want to send a WoL magic packet on login or boot. If this new app ran as a background process launched at system startup, then it could also handle those advanced WoL scenarios.
3rd Party Solutions
The above two options were all I could come up with, though I'm certainly open to other ideas.
Additionally, it occurred to me that perhaps there's already a good hotkey tool produced by someone else. The requirements for launching CMC are really basic, so I'm thinking pretty much any old hotkey tool could get the job done. In my research into the various Windows limitations for hotkeys, several times I came across users commenting on how you needed 3rd party tools to get the desired behavior that Windows fails to deliver.
I've never used any of these tools, so if anyone has recommendations please let me know.
For example, even though CMC creates a Windows Shortcut to launch CMC that includes a Hotkey combo to trigger the shortcut, Windows prevents the shortcut from executing if the target program is already running. While at first glance that seems like a fair result to prevent accidentally running multiple copies of a program, it breaks the ability of a 2nd copy of CMC to bring the initial instance to the top and into focus.
Additionally, Windows captures the hotkey combo and prevents running apps from seeing it. For example, CTRL+ALT+8 is the hotkey I programmed for launching CMC. If you press that combo while CMC is already running, CMC sees only "CTRL+ALT", and not the 8. So I can't even program CMC to monitor for that keystroke and take action, since Windows captures it.
I've also noticed that CMC takes a few extra seconds to launch via hotkey - I believe that this is Windows checking to see if the target program is already running before launching a new instance. As hard as I've worked to make CMC launch quickly, it frustrates me that Windows is slowing it down for a feature I don't even like.
So I have a couple ideas on how to tackle this challenge for a future version of CMC, and thought I would post them here for comment.
Add Additional Hotkeys for refocusing CMC
This first option is perhaps the simplest. CMC already has many hotkeys programmed into it, and they work pretty great. The idea here is to simply add a new hotkey that will refocus CMC and bring it to the top. You could map this hotkey to your remote, and any time you find CMC not responding because it has lost focus, pressing the configured button would hopefully resolve the issue.
This does have some potential problems. First of all, it requires you to dedicate an extra button on your remote for this behavior, and perhaps you are already running short of buttons. Second, if CMC has lost focus, then it probably won't even see the hotkey to respond to it. And third, Windows actively tries to block apps from stealing focus. Put altogether, I'm thinking that this is a pretty bad idea.
Create a new background process to monitor Hotkeys
This idea has been suggested to me by several users over the years, and I've been resistant to it simply because I felt I could solve the problem in CMC itself. Now that Windows has utterly defeated those efforts, I'm reconsidering this idea.
Basically, I would create a new standalone app that would run either in the System Tray or as a Background Process. It would monitor for certain hotkeys, and when it sees them it would take the appropriate action. It would be smart enough to know that if CMC is not running, then to launch CMC, but if CMC is running then to bring CMC into focus to respond to commands. I'm thinking that this app could reduce CMC launching time by 1-2 seconds.
There are some unknown challenges with this approach. First, I'm worried what a virus scanner will think of a background process that monitors keystrokes. It's very possible this will look like keylogger malware. I'm also not sure if I can successfully have it monitor for all keystrokes while running in the systray or as a background process. Basically, for this app to be successful, it needs to see every keystroke no matter what app is running. There's also the concern of what users may think of such an app. Are you guys open to the idea of a keystroke monitor running 24/7 on your HTPC's? Or is this crossing a line? I'm thinking use of such an app would have to be optional, of course, but if no one is willing to run such an app I don't think I should waste time programming it.
If I did create such an app, it would allow for some advanced functionality that is not possible today. For example, the current Wake-on-LAN solution on works if CMC is running, so that doesn't solve the need for users who want to send a WoL magic packet on login or boot. If this new app ran as a background process launched at system startup, then it could also handle those advanced WoL scenarios.
3rd Party Solutions
The above two options were all I could come up with, though I'm certainly open to other ideas.
Additionally, it occurred to me that perhaps there's already a good hotkey tool produced by someone else. The requirements for launching CMC are really basic, so I'm thinking pretty much any old hotkey tool could get the job done. In my research into the various Windows limitations for hotkeys, several times I came across users commenting on how you needed 3rd party tools to get the desired behavior that Windows fails to deliver.
I've never used any of these tools, so if anyone has recommendations please let me know.