PDA

View Full Version : Grab keyboard events in Windows



durbrak
4th February 2007, 12:46
Hello,
I'm trying to write a really small program for myself. Whenever I use my Vol+ and Vol- keys on my keyboard I want to make a small OSD which shows the current volume.
Well somehow I need to "grab" key events in Windows but I have no clue how to start.
Qt only seems to support keyboard events within its own application but of course that's not going to work for me since the application will be hidden (and therefore not focused) and keyboard events won't work.
I know this is possible, e.g. Trillian has a shortcut for "Display Contact List" and whenever the user presses that shortcut, the contact list opens.
I guess I have to use native functionality for that but have no clue where to look. Can maybe someone point me to the right direction?
Thanks.

kiker99
4th February 2007, 20:56
I have done this before, but I currently don't have access to the code anymore.

AFAIK, there are 2 possibilities:
1. use GetAsyncKeyState (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/getasynckeystate.asp) in a loop
2. install a global keyboard hook with SetWindowsHookEx (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/setwindowshookex.asp)

Your profile says you're german so this will help you: :)
http://dotnet.mvps.org/vb/articles/keyboardhook/