tags:

views:

127

answers:

3

I'm trying to get my application to do something when CTRL+S is pressed. I'm just not sure how the W and L params work for WM_KEYDOWN. MSDN has something about bit fields which i'm not sure about. How can I detect CTRL and S? Thanks

What do I do if another control aside from hWnd has focus?

+2  A: 

Well, this is the big list of virtual key codes.

CTRL-S is going to be sent through as 2 WM_KEYDOWN messages - a message when the ctrl key is pressed (VK_LCONTROL or VK_RCONTROL) followed by a 0x53 for the "S" key.

Rather than processing both messages, wait for the key down message for the 'S' press then call GetKeyState using the magic value VK_CONTROL (otheriwse you'd need to test individually for the left AND right control keys) to see if the S was pressed with CTRL held down.

--

Obviously, keyboard messages are sent directly to the window that has focus. To get accelerator combinations to work at the application scope you need to check the messages before dispatching them to the focus window - i.e. in your message pump. See the documentation for TranslateAccelerator.

If you want to handle system wide keypresses, the other answer points to the hot key api.

Chris Becke
What do I do is another control aside from hWnd has focus?
Milo
To handle ctrl-X combinations app wide you would use the accelerator api. Systemwide - the hotkey api.
Chris Becke
A: 

I would suggest using a hotkey. Check out RegisterHotKey.

X-N2O
That is system-wide, and definitely a bad practice for regular application scenarios.
ssg
A: 

When the WPARAM is equal to the CTRL VKcode, then set a bool in the state of your object. Then, when S comes up, if Ctrlbool, you've got CTRL-S.

DeadMG
What if he leaves Ctrl key before pressing S? Then you need to handle keyup messages to track it. Chris' reply is the appropriate approach.
ssg