Hello,
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,
Johnny

Recommended Answers

All 9 Replies

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.

Well, what I was thinking of doing was using a Windows Service.

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

When the .NET says Focus or Focused are they talking about the foreground application?

Also, with this how would I use the KeyDown event or PreviewKeyDown event. Also, which is better.

Thank you so much,
Johnny

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,
Johnny

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.