How would I go about being able to subscribe to the keyboard? For example, I would like my program to be alerted anytime a key on the keyboard is pressed, not just while my windows is in focus?

Also, while I am asking, how do I get the windows that is in focus?

Thanks in advance,

7 Years
Discussion Span
Last Post by Diamonddrake

We do not say an application has focus, we say an application is activated (meaning it is the foreground application) It continually checks (via its dispatcher or eventloop) for keydowns, mouseclicks etc. and processes them furter as it sees fit. You could perhaps start a background process to do what you want?

>>how do I get the windows that is in focus?
Check the Activated and Deactivated events of the application object.


I came across http://msdn.microsoft.com/en-us/libr...d_members.aspx and noticed that it gets input regardless of which program is foreground.

Of course the low level Keyboard class does that. It passes the KeyDown events etc. to the forground application which has the "Focus".
What would happen if you had Word in the foreground and Excel in the background, and you pressed the 'A' key? Do you think the A-character would be showing up in a cell in Excel?


I just need to answer one question and dig the other.
To handle keys pressed when your application on top or activated (any term you prefer) you need to set Form property (Key preview = true) then handle Key press event handler.

The question I want to discuss with you, what about if I want to get the keys user pressed whatever the status of my application (minimized, maximized, etc...) is answer is to override Windows API (the queue handles the key pressed as example..) I don't know actually how do I, but I'll try to develop an application and keep you up-to-date.


I am looking at what his program does, it seems to me like it just disables. Can you help me with figuring out how I should change that so I can get the keys that are pressed regardless of my window's status.

Thanks for looking into this,


Look by my reading in Windows APIs I developed application gets handler HWND or Intptr to the active window GetForegroundWindow that not neccessary assoicated with your program I'll try to mix both samples to get keys pressed regardless which window is active.


just google Low Level Keyboard Hooks in C# Thats the trick, but be careful. if you want to have an application process all keyevents, be sure to use something like a combination, or else you will have erratic behavior. But that's the trick, I wrote a couple of auto hot key style apps using keyboard hooks.

Happy Coding

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.