GetKeyState, how to check high order bit,  trap Ctrl-Z

Posted on 2005-05-03
Last Modified: 2013-11-20
I'm trying to figure this out. should be so simple but I'm at a loss (once again!)

In my PreTranslateMessage(MSG* pMsg) handler, I want to trap CTRL-Z and CTRL-Y

How do I do this? MSGN says to use GetKeyState and check the high-order bit of the return value, I'm not sure how to do that or if it's the right way all together.

Question by:PMH4514
    LVL 48

    Accepted Solution

    SHORT state = GetKeyState(...);

    if ( state & 0x8000 )     // 1000000000000000 binary
        // high bit of state is 1

    Author Comment

    oh so anding it with 1000000000000000  will get the high-bit.

    would my understanding be correct to state that anding it with 0000000000000001  would then get the low bit?
    LVL 43

    Assisted Solution

    An alternative
          BOOL bIsCtrlKeyDown=::GetAsyncKeyState(VK_CONTROL)<0;
    LVL 48

    Expert Comment

    Other way to write this:

    if ( state & (SHORT)(1<<15) )    // is bit 15 1 ?

    This gives the same runtime performance because constant 1<<15 is calculated at compile time.
    To test bit n use 1<<n.

    Author Comment

    oh I get it..


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
    Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    794 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

    18 Experts available now in Live!

    Get 1:1 Help Now