Domanda

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?

È stato utile?

Soluzione

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

Altri suggerimenti

When Ctrl+Z is in the accelerator list (as you wrote in a comment), than you don't Need to handle the control sequence. You will receive a WM_COMMAND message with the value, that is defined in the accelerator list.

Accelerators make it much easier and more flexible to handle the keystrokes. If a key sequence is in the accelerator list the message is translated in PreTranslateMessage and the WM_COMMAND message is send to the mainframe. Than the command handlers will do the rest. The keystroke will never arrive in the control.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top