Jump to content
Ian Branch

Scope of a HotKey??

Recommended Posts

Hi Team,

D10.3.2 not that it probably makes any difference.

 

If I have two Apps with the same HotKey but doing different things, it is only the first App loaded that the HotKey works for, doesn't matter if the second App has focus or not. 😞

So - Two questions...

1.  Is there any way to make a HotKey effective only for the App in focus?

2.  If an App has several forms, is there any way to have the HotKey work for only the form in focus?

Regrettably my suspicion in both cases is No, It's a Windows thing, however I am ready, willing and able to be pleasantly surprised. 😉

 

Regards & TIA,

Ian

Share this post


Link to post

If you are referring to the Win32 RegisterHotKey() function and WM_HOTKEY window message, then such hotkeys are system-wide and any given hotkey can only be registered for one window at a time.  To use the same hotkey with multiple processes at the same time, you will have to resort to using a global keyboard hook and handle the keystrokes manually.

Edited by Remy Lebeau

Share this post


Link to post

Hi Remy,

Thanks for your input.

>> you will have to resort to using a global keyboard hook and handle the keystrokes manually.

Not my area of expertise I'm afraid.  Can you point me to some examples pls?

Regards,

Ian 

Share this post


Link to post

Hey, I'm far, far from an expert on these matters so my thinking may be very flawed here.  But when app #2 gains focus, perhaps it could send a message (and wait for a response) to app #1 telling it to de-register the hotkey?  And then app #2 registers the hot key.  And same for when app #1 gains focus, it messages and waits for app #2 to de-register the hotkey.

Share this post


Link to post
14 hours ago, Tom F said:

Hey, I'm far, far from an expert on these matters so my thinking may be very flawed here.  But when app #2 gains focus, perhaps it could send a message (and wait for a response) to app #1 telling it to de-register the hotkey?  And then app #2 registers the hot key.  And same for when app #1 gains focus, it messages and waits for app #2 to de-register the hotkey.

That's fine if you control both apps.  But what if you want to use a hotkey that is used by another app that you don't control?  Then you are SOL.  This is why apps are supposed to pick unique hotkeys for themselves, or allow the user to configure the hotkeys (see the VCL's THotKey component and TextToShortCut() function).

  • Like 1

Share this post


Link to post

Hi Remy, Tom,

As I suspected, and Remy so eloquently expressed, I am SOL. 

This came to light in my own testing when I implemented a HotKey in the App, went to test it and my AV popped up.  WTH!

The Apps themselves use many HotKeys.  Perhaps I need to move them to a Menu/Submenu scenario.

Will think on it.

 

Thanks to you both for your inputs.

Regards,

Ian

Share this post


Link to post

It's easy. If those hotkeys are really app-wide hotkeys and not a case for menu/action components, just hook the application messages and evaluate WM_KEYDOWN.

VCL does the same. For every single menu/action visible.  Every single time you press a key in your app.

Share this post


Link to post
On 9/4/2019 at 3:03 PM, Attila Kovacs said:

VCL does the same. For every single menu/action visible.  Every single time you press a key in your app.

Yup, see A Key’s Odyssey for more details about that.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×