Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to get low level enough to detect key up events, you should process the WM_KEYDOWN and WM_KEYUP events:</p> <ul> <li><a href="http://msdn.microsoft.com/en-us/library/ms646267%28v=vs.85%29.aspx#_win32_Keystroke_Messages" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ms646267%28v=vs.85%29.aspx#_win32_Keystroke_Messages</a></li> </ul> <blockquote> <p>Pressing a key causes a WM_KEYDOWN or WM_SYSKEYDOWN message to be placed in the thread message queue attached to the window that has the keyboard focus. Releasing a key causes a WM_KEYUP or WM_SYSKEYUP message to be placed in the queue.</p> <p>Key-up and key-down messages typically occur in pairs, but if the user holds down a key long enough to start the keyboard's automatic repeat feature, the system generates a number of WM_KEYDOWN or WM_SYSKEYDOWN messages in a row. It then generates a single WM_KEYUP or WM_SYSKEYUP message when the user releases the key.</p> </blockquote> <p>To distinguish between the left and right versions of the Shift, Ctrl, or Alt keys, you have to use the <code>MapVirtualKey()</code> function or the 'extended key' bit in the lParam passed with the virtual key's message. The following function will perform that translation for you - just pass in the virtual keycode and the lParam from the message, and you'll get back the left/right specific virtual keycodes as appropriate:</p> <pre><code>WPARAM MapLeftRightKeys( WPARAM vk, LPARAM lParam) { WPARAM new_vk = vk; UINT scancode = (lParam &amp; 0x00ff0000) &gt;&gt; 16; int extended = (lParam &amp; 0x01000000) != 0; switch (vk) { case VK_SHIFT: new_vk = MapVirtualKey(scancode, MAPVK_VSC_TO_VK_EX); break; case VK_CONTROL: new_vk = extended ? VK_RCONTROL : VK_LCONTROL; break; case VK_MENU: new_vk = extended ? VK_RMENU : VK_LMENU; break; default: // not a key we map from generic to left/right specialized // just return it. new_vk = vk; break; } return new_vk; } </code></pre> <p>If the virtual keycode passed in isn't one that maps to a left/right version, the original keycode is passed back unchanged. So you can just run the <code>WM_KEYDOWN</code>/<code>WM_KEYUP</code>/<code>WM_SYSKEYDOWN</code>/<code>WM_SYSKEYUP</code> message parameters through the function whenever you need to distinguish between the left and right variants.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload