Solved

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

Posted on 2005-05-03
932 Views
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.

Thanks!
-Paul
0
Question by:PMH4514

LVL 48

Accepted Solution

SHORT state = GetKeyState(...);

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

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?
0

LVL 43

Assisted Solution

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

LVL 48

Expert Comment

Right.
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.
0

Author Comment

oh I get it..

thanks!
0

## Featured Post

### 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…