How to detect keystrokes when program out focus

Please discuss general Delphi programming topics here.

How to detect keystrokes when program out focus

Postby Edwin » March 7th, 2006, 9:46 am

Hi,

I want my program to react on a 'key down' and 'key up', even when the progrsm does not has focus, is minimized or hided.
Does onyone know how to make that?
Thanks,

Edwin
User avatar
Edwin
Active Member
Active Member
 
Posts: 18
Joined: November 18th, 2005, 11:57 pm
Location: Breda, NL

Postby Kambiz » March 7th, 2006, 10:47 am

The RegisterHotKey function defines a hot key for the current thread.

Code: Select all
BOOL RegisterHotKey(

    HWND hWnd,   // window to receive hot-key notification
    int id,   // identifier of hot key
    UINT fsModifiers,   // key-modifier flags
    UINT vk    // virtual-key code
   );



Parameters

hWnd

Identifies the window that will receive WM_HOTKEY messages generated by the hot key. If this parameter is NULL, WM_HOTKEY messages are posted to the message queue of the calling thread and must be processed in the message loop.

id

Specifies the identifier of the hot key. No other hot key in the calling thread should have the same identifier. An application must specify a value in the range 0x0000 through 0xBFFF. A shared dynamic-link library (DLL) must specify a value in the range 0xC000 through 0xFFFF (the range returned by the GlobalAddAtom function). To avoid conflicts with hot-key identifiers defined by other shared DLLs, a DLL should use the GlobalAddAtom function to obtain the hot-key identifier.

fsModifiers

Specifies keys that must be pressed in combination with the key specified by the nVirtKey parameter in order to generate the WM_HOTKEY message. The fsModifiers parameter can be a combination of the following values:

MOD_ALT: Either ALT key must be held down.
MOD_CONTROL: Either CTRL key must be held down.
MOD_SHIFT: Either SHIFT key must be held down.


vk

Specifies the virtual-key code of the hot key.



Return Values

If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero.

Remarks

When a key is pressed, the system looks for a match against all thread hot keys. Upon finding a match, the system posts the WM_HOTKEY message to the message queue of the thread that registered the hot key. This message is posted to the beginning of the queue so it is removed by the next iteration of the message loop.
This function cannot associate a hot key with a window created by another thread.
RegisterHotKey fails if the keystrokes specified for the hot key have already been registered by another hot key.

If the window identified by the hWnd parameter already registered a hot key with the same identifier as that specified by the id parameter, the new values for the fsModifiers and vk parameters replace the previously specified values for these parameters.

See Also

GlobalAddAtom, UnregisterHotKey, WM_HOTKEY
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Postby Edwin » March 7th, 2006, 3:44 pm

This works! Thanks a lot,

Edwin
User avatar
Edwin
Active Member
Active Member
 
Posts: 18
Joined: November 18th, 2005, 11:57 pm
Location: Breda, NL

Postby Johnny_Bit » March 7th, 2006, 8:12 pm

Hmm... that's MSDN... Taht thing you have to have if you do stuff under windooze... Why you haven't checked it first? Or searched for aomething along lines of API HOOK? I swear, if I haven't seen at least 100 threads regarding Api hooks for hooking up keyboard then may my linux scream loud with kernel panic right now!

//what I was trying to say is: If you wouldn't have internet, you'd know the answer after 10 minutes long search throught api documentation. Or if you'd even simply search for it at first rather than asking directly question, answer would be reavaled to you in less than it took you to actually write your post down.

//Edit: Kambiz is right, but still, search first then ask. For somebody like you it shouldn't take more than 20-30 seconds
Last edited by Johnny_Bit on March 8th, 2006, 7:14 pm, edited 1 time in total.
Johnny_Bit
VIP Member
VIP Member
 
Posts: 455
Joined: June 15th, 2003, 9:56 am

Postby Kambiz » March 8th, 2006, 7:58 am

Johnny_Bit,

It doesn't work always.

Sometimes after finding a solution on MSDN and implementing it, I've realized there was a much easier solution. I could prevent extra work just by asking a simple question.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Postby Edwin » March 12th, 2006, 3:46 pm

Hi,

I did find it before, but I would like to know if there were other ways.
I still had to do some tricks to get it work. :wink:
Edwin
User avatar
Edwin
Active Member
Active Member
 
Posts: 18
Joined: November 18th, 2005, 11:57 pm
Location: Breda, NL


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 10 guests

cron