Easiest way to detect key press (non-console)

Pages: 12
I tried looking at hooks and immediately got confused, I think I'll set that topic aside for later.

I want to detect if a key is pressed, and if it is I want the VK value for that key so I can press it again through keybd_event().
What OS are you using?
Windows 7
You'll find the VK value in the wParam of a WM_KEYDOWN message. You need to process it in your WindowProc() function. http://msdn.microsoft.com/en-us/library/ms646280(VS.85).aspx
Eh forgot to say that I'm a beginner, is there a sample of this somewhere, I don't understand how I'm supposed to access this parameter or how a WindowProc() function is supposed to be set up.
Last edited on
Get Code::Blocks if you haven't already and create a new Win32 application.
Some code will be created that sets up a window and a WindowProc function.
So what will I need to do in Code Blocks after I create this application?
You should definitely read the MSDN guide for getting started with Windows API programming: http://msdn.microsoft.com/en-us/library/ff381409(VS.85).aspx
Thanks for replying : ).

I looked into it but I don't see how I'm supposed to tie keyboard stuff into that, also I don't want to have a window appear on the screen, I want this program to run in the background.
Last edited on
Robert265 wrote:
I looked into it but I don't see how I'm supposed to tie keyboard stuff into that

It can be done the way I explained. But you need to understand how a Windows API program works before you do that.

also I don't want to have a window appear on the screen, I want this program to run in the background.

You still need a Windows program if you want to interact with the desktop window. You can make the window invisible, but you really need to understand what you're doing. Just randomly looking for code snippets won't get you very far.

EDIT: may I ask why do you need to get keyboard input from the desktop window?
Last edited on
I guess he wants to write a keylogger. To handle global key-presses use GetAsyncKeyState. To hide the window just don't call ShowWindow. Though it would be much simpler if you made this a console application and use GetConsoleWindow and ShowWindow to hide it as soon as the program starts.
Last edited on
Well basically I'm running windows 7 and I don't have any shortcuts on my desktop, so I want to write a little program that automatically puts anything I type (when the desktop is in focus) into the little search box when you open the start menu. I have the focus and menu-opening done, just need the key detection now. Well I kinda wanted to do a quick and dirty program, but if the only way I can do this is with windows then I guess I'll learn them then.
Ok I think I have everything set up right, how do I access the wParam though, is it just message.wParam (for some reason my visual studio isn't doing any syntax highlighting or suggestions or anything : <)?
Yes (as long as message is a MSG structure). If you need to know more about the MSG struct: http://msdn.microsoft.com/en-us/library/ms644958(VS.85).aspx
It seems like WM_KEYDOWN only occurs when the application itself is focussed, is there a way around this?
I already told you... GetAsyncKeyState
Oh sorry (and thanks) I was going on someone else's comment, I'll try this and get back to you.
Wait you need to pass in a certain key to check?
Yes. Just use a loop that checks all virtual key values and if you find a key-press call ToAscii to convert the virtual key to a printable character.

http://msdn.microsoft.com/en-us/library/ms646316%28VS.85%29.aspx
Hm I guess it's kind of good in a way since it lets me specify what characters I want to "detect", ok I'll try this and get back to you, thanks again : ).

EDIT: Wait WndProc only responds to messages right, so wouldn't I have to create my own thread or something to constantly check for button presses?
Last edited on
Pages: 12