Detect if shift key is down

Posted on 2006-06-02
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

    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.

    LVL 9

    Accepted Solution

    LVL 23

    Author Comment

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    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…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now