I am trying to cut/copy/paste in my application and for that I need to capture Ctrl+Z etc. I have written following code in my OnKeyDown() event handler:
if(GetKeyState(VK_CONTROL)<0)
{
WPARAM wparam = (WPARAM(nChar));
switch(wparam)
{
case 'z':
//display message box
break;
default:
break;
}
}
But my problem is that the multiple key strokes never get captured. I can capture single button press for Ctrl key or any other key for that matter.But if I press Ctrl+Z on my keyboard, only ctrl is captured and z is ignored. Could somebody pls suggest a better way to handle multiple key strokes?
Try this:
if (GetKeyState(VK_CONTROL)&0x80)
{ if ((nChar==_T('z'))||(nChar==_T('Z')))
{ // indicate activity
Beep(800, 50);
}
}
Additional Information:
From MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/ms646301(v=vs.85).aspx
The function GetKeyState
retrieves the status of the specified virtual key. The status specifies whether the key is up, down, or toggled (on, off—alternating each time the key is pressed).
The key to check is the control-key, ie: VK_CONTROL. You can also refer to GetAsyncKeyState
for more supported key name macro.
The operation (&0x80) is checking for the single high-order bit (MSB) of SHORT (8-bits)
Return value: SHORT
If the high-order bit is 1, the key is down; otherwise, it is up.
If the low-order bit is 1, the key is toggled