Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Detect if shift key is down

Posted on 2006-06-02
Medium Priority
Last Modified: 2010-05-18
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!
Question by:ahammar
LVL 22

Expert Comment

ID: 16819144
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.


Accepted Solution

Naveen Swamy earned 2000 total points
ID: 16819376
LVL 23

Author Comment

ID: 16819711
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.


Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question