Board index » cppbuilder » Capturing key presses - is there a better way?

Capturing key presses - is there a better way?


2007-03-07 04:16:41 AM
cppbuilder32
This code does not capture the "key up" for keys other than function
keys...is there a simple thing I am missing? Is there a better way to
globally capture all key presses for my application? I am trying to capture
whether or not the shift key is pressed while another key is also pressed at
the same time. THANKS!!
void __fastcall TGUCI_CommsFormC::CheckKey(tagMSG &Msg, bool &Handled){
static bool bShiftDown;
if (Msg.message == WM_KEYUP) {
int iKeyTest = (int) Msg.wParam;
if (iKeyTest == 16) { //Shift Key
bShiftDown = false;
Handled = true;
}
} else if (Msg.message == WM_KEYDOWN ) {
int iKeyTest = (int) Msg.wParam;
//Check for the shift key
if (iKeyTest == 16) { //Shift Key
bShiftDown = true;
Handled = true;
}
}
}
 
 

Re:Capturing key presses - is there a better way?

"panders" < XXXX@XXXXX.COM >wrote in message
Quote
This code does not capture the "key up" for keys other
than function keys...
For future reference, this was not an IDE issue. You should have
posted this to the VCL newsgroups instead.
The code you have shown works fine for me when I try it. It does
capture the shift keystrokes as expected.
Quote
Is there a better way to globally capture all key presses for my
application?
You can use a keyboard hook via the Win32 API SetWindowsHookEx()
function.
Quote
static bool bShiftDown;
You are not initializing the variable:
static bool bShiftDown = false;
Quote
Handled = true;
You are discarding all shift key down events. Is that what you really
want?
Gambit
 

Re:Capturing key presses - is there a better way?

Quote
I am trying to capture whether or not the shift key is pressed while
another key is also pressed at the same time.
You could try using the GetKeyState function to see if the shift key is
pressed for each keypress message. The result of the method changes as you
process keypresses through the message loop, so if your function is being
called for every key pressed from your message handler, then calling
GetKeyState in that function should return that state of the shift key for
that particular message.
Cheers,
David
 

{smallsort}