1

I have been trying to register 3 hotkeys. I followed this example (or this).

The code is working IF I register a single hotkey. However, if I try to register 3 hotkeys, only the last registered one is working:

procedure TFrmSettings.FormCreate;
begin
 atomN:= GlobalAddAtom(pchar('atomN'));    
 atomT:= GlobalAddAtom(pchar('atomT'));
 atomH:= GlobalAddAtom(pchar('atomH'));
end;


procedure TFrmSettings.test1;
begin
 RegisterHotShortCut(Handle, atomN, hotN.HotKey);        // first registered 
 RegisterHotShortCut(Handle, atomH, hotH.HotKey);        // second registered 
 RegisterHotShortCut(Handle, atomT, hotT.HotKey);        // third - only this works
end;

RegisterHotShortCut is declared in the web page above mentioned.

function RegisterHotShortCut(const h:THandle; const Atom: integer; const ShortCut: TShortCut): Boolean;
VAR
  key : Word;
  Shift: TShiftState;
begin
  Windows.UnregisterHotKey(h, Atom);                                                    
  ShortCutToKey(ShortCut, key, shift);
  Result:= RegisterHotKey(h, Atom, ShiftState2Modifier(Shift), key);
end;

If in the code above, I comment the last line, then the previous hotkey (hotH) is working. This means that I declared everything right but the last hotkey takes control over (unregisters) the others.

Or saying it in other way, pressing the key combination for the for first registered shortcuts do not make the program to enter the TFrmSettings.HotyKeyMsg(VAR msg: TMessage) procedure.

Any hints? Thanks.


Edit1:

  1. There are simple snippets that seems to work for other but I really need to let the user to change the shortcut so I need the ShortCutToKey procedure.

  2. I should declare an atom for each global shortcut. Right?


Edit2: SOLVED The above code fails ONLY if I use the "GlobalAddAtom" function. If instead of atoms I use just numbers 1, 2 and 3, the code works. If I use 4,5,6, the code fails again. Problem (semi) solved.

5 Answers 5

1

i'v been using code like this for my multiple hotkeys, i don't know if this is what you are trying to do : first declare

procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;

then in put these in form.create or elsewehere

  RegisterHotKey(Handle, 1, MOD_SHIFT, VK_F9); 
  RegisterHotKey(Handle, 2, MOD_WIN, VK_F8);
  RegisterHotKey(Handle, 3, MOD_ALT, VK_F7);

and the hotkey handle procedure

procedure TForm1.WMHOTKEY(var Msg: TWMHOTKEY);
begin
  case Msg.HotKey of
    1 : ShowMEssage('this is 1 (shift+F9)');
    2 : ShowMessage('this is 2 (Win+F8)');
    3 : ShowMessage('this is 3 (ALT+F7)');
  end;
end;

don't forget to unregister it when closing ur app. been tested on winxp+sp3 , delphi 2006

2
  • Hi. Your post helped me to find out that the code works only if you use numbers 12,3 instead of atoms.
    – IceCold
    Apr 14, 2009 at 22:12
  • yes, because it is the message you receive with WMHOTKEY procedure, it has to be numbers i think. and to avoid confiliction with others applications u should use higher random numbers of your own. 1,2,3 was only example ;)
    – avar
    Apr 15, 2009 at 1:59
0

Searching for RegisterHotShortCut on google only returns 5 hits, and to me that indicates that this function is not the right answer. Searching for "delphi register hotkey" gives 235000 hits, and the article How to register a global hotkey (from 2007, using RegisterHotKey) seems to be similar to the article you looked at (from 2003).

2
  • Hi. RegisterHotShortCut is declared in the web page above mentioned.
    – IceCold
    Apr 13, 2009 at 11:17
  • Oops, yes you are correct, I see that you have included it in the question now. I am afraid I cannot help further.
    – hlovdal
    Apr 13, 2009 at 11:51
0

Yes, you need an Atom for each shortcut. I am curious about the call you are making to UnregisterHotKey for each one before you register it. If you didn't previously register that hotkey then you don't need it. Try removing that line and see if that fixes it. It sounds like the UnregisterHotKey is removing the previous hotkey even though it has a different atom . .

0
0

plz check this code, it uses atoms and works fines ( i tested it) http://www.swissdelphicenter.ch/torry/showcode.php?id=147

edit1 : please keep in mind that, a reason for registering failure is if the key combination you are trying to register is already registered by the system or another application. (see the above link)

0

SOLVED The above code fails ONLY if I use the "GlobalAddAtom" function. If instead of atoms I use just numbers 1, 2 and 3, the code works. If I use 4,5,6, the code fails again. Problem (semi) solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.