I have a Delphi 2007 project that has run fine on Windos XP, Vista and "7" for years. It was an upgrade from Delphi 5 thus "MainFormOnTaskBar" was "false" by default (I never changed it in DPR). In this scenario, the system-wide hot key worked "system-wide" with following code in main form's OnCreate event handler.
HotKey_xyz := GlobalAddAtom('Hotkey_xyz');
if NOT RegisterHotKey(Self.Handle, HotKey_xyz, MOD_CONTROL, VK_F12) then
ShowMessage('Unable to register Control-F12 as system-wide hot key') ;
(I have GlobalDeleteAtom() and UnregisterHotKey() in Form.OnDestroy as expected.)
Now, I need a Form to show its own button on Taskbar, so I set "Application.MainFormOnTaskBar := True" in DPR. This works as expected. However, this has the side-effect that Control-F12 does NOT work system-wide, it works ONLY IF my application has focus (so, it is NOT "system-wide" anymore.)
I have extensively searched the 'Net have found many articles regarding how/why "MainFormOnTaskBar" affects certain subform/modal form behaviors. However, I have found nothing regarding its effect on a "System-Wide Hot Key" issue that I describe above. I have tested and retested my application with MainFormOnTaskBar set to true and false while all else remains exactly the same. I can positively verify that the above described issue with System-wide hot key relates to MainFormOnTaskBar flag.
I will greatly appreciate any guidance regarding a work-around. I do need BOTH - a system-wide hot key AND a form with its own button on taskbar.
Thank You very much.
Self.Handle
toApplication.Handle
in theRegisterHotkey
call in the meantime? TheWM_HOTKEY
message will still get to your form, because the application's message handler doesn't do anything with it; it will be sent on to your form just like usual.) – Ken WhiteApplication.Handle
is used to register the hot key, then theWM_HOTKEY
messages WILL NOT be directed to the TForm, directly or indirectly. They will be directed toTApplication
instead, so to catch those messages you have to use theTApplication.OnMessage
event and/or theTApplication.HookMainWindow()
method. – Remy LebeauTApplication.WndProc
, which has nothing to deal with it. It's then dispatched to other windows. (I can positively confirm this, because I have a component that uses this method to routeWM_HOTKEY
messages to whatever form it's placed on viaApplication.Handle
.) Your answer (withAllocHWnd
) looks good, though. – Ken White