Link to home
Start Free TrialLog in
Avatar of ahammar
ahammarFlag for United States of America

asked on

Detect if shift key is down

Hello all,

How can I tell if my shift key is pressed even when my app does not have focus.  I already have it so that it captures keys, but I can't figure out how to tell if the shift key is down.  I am not too good with API's, but I was able to alter some code that I got to capture only the keys I wanted, so please by descriptive in how I need to use whatever code you give me.  I tried using GetAsyncKeyState, but I couldn't get it to work.  I could be going about all wrong though, or maybe that's not even what I should use.


Thanks and Cheers!
ahammar
Avatar of rspahitz
rspahitz
Flag of United States of America image

If you want to check for a shift key state from your app, you either need to add an event procedure to your app or you need to hook into Windows to ask it the state.
I'm not sure why you need to check for the shift key when your app does NOT have the focus, so I'll leave that part to the API experts.

For the form, you can handle this through the KeyDown/KeyUp events where you can check the state of the Shift key/Control key/Alt key.

If you want it ANYWHERE on the form, you'll need to change the form'sKeyPreview property then use the form's KeyDown/KeyUp event to get the state.

ASKER CERTIFIED SOLUTION
Avatar of Naveen Swamy
Naveen Swamy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ahammar

ASKER

Thanks navstar16

I already had a keyboard hook, I just didn't know how to use the GetAsyncKeyState API, or if that was even what I needed.
I got it to work though by reviewing the second link you listed.

Thanks!
Cheers!
ahammar