我如何左右键(Ctrl和Alt)区分? [英] How do I distinguish between left and right keys (CTRL and ALT)?

查看:487
本文介绍了我如何左右键(Ctrl和Alt)区分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始利用Win32的的原始输入功能来检测键盘上的所有按键。到目前为止,一切是伟大的工作!我可以在右边的小键盘上的顶行号码和号码之间进行区分。我甚至可以左,右shift键的检测。然而,控制和Alt键不C $ CS返回唯一扫描$。在<大骨节病>控制键返回29,而<大骨节病> ALT 键返回56。

I started making use of Win32's raw input features to detect all keys on the keyboard. So far, everything is working great! I can distinguish between numbers on the top row and numbers in the keypad on the right. I can even detect between the left and right shift keys. However, the control and alt keys do not return unique scan codes. The control key returns 29, and the alt key returns 56.

有关这些键检查关键州的流行的方法是 GetAsyncKeyState 。我已经测试使用 VK_LCONTROL VK_RCONTROL 的功能,它的工作原理,但只帮助我捕捉按键事件。我真的希望能够捕捉到了关键事件以及。很明显,该API是某种知道哪个键正在pressed的;如何得到阿霍德的信息?

The popular method for checking key states on these keys is GetAsyncKeyState. I have tested that function using VK_LCONTROL and VK_RCONTROL, and it works, but that only helps me for capturing key down events. I would really like to be able to capture key up events as well. It is obvious that the API is somehow aware of which key is being pressed; how do I get ahold of that information?

我目前提取的 RAWKEYBOARD 结构的使code 场扫描code。这给了我一下,除了<大骨节病> CTRL 和<大骨节病> ALT 每个键(和它的左/右对齐)的信息。我怎么会去捕获键向上事件(并且知道它是左/右)?是否有可能只使用 RAWKEYBOARD 结构?还是我编造某种解决办法呢?

I am currently extracting the scan code from the RAWKEYBOARD structure's MakeCode field. That gives me information about every key (and its left/right alignment) except CTRL and ALT. How would I go about capturing the key up events (and knowing whether it is left/right)? Is it possible using just the RAWKEYBOARD structure? Or do I have to concoct some kind of workaround?

推荐答案

如果你想获得低的水平足以检测按键弹起事件,你应该处理WM_KEYDOWN和WM_KEYUP事件:

If you want to get low level enough to detect key up events, you should process the WM_KEYDOWN and WM_KEYUP events:

pressing一键导致WM_KEYDOWN或
  要放置在WM_SYSKEYDOWN消息
  附着到线程消息队列
  具有键盘的窗口
  焦点。释放一个键会导致
  WM_KEYUP或WM_SYSKEYUP消息是
  放置在队列中。

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.

钥匙和钥匙通常向下信息
  是成对出现的,但如果用户持有
  上下足一键长启动
  键盘的自动重复功能,
  系统生成多项
  WM_KEYDOWN或WM_SYSKEYDOWN消息
  连续。然后,它生成一个
  WM_KEYUP或WM_SYSKEYUP消息时
  用户释放键

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.

要的Shift,Ctrl或Alt键的左,右版本之间的区别,你必须使用 MapVirtualKey()函数或扩展键位在lParam中传递与虚拟键的消息。下面的函数将执行转换为你 - 只是通过在虚拟键code和从消息中的lParam,你会找回左/右特定的虚拟键codeS适当的:

To distinguish between the left and right versions of the Shift, Ctrl, or Alt keys, you have to use the MapVirtualKey() 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:

WPARAM MapLeftRightKeys( WPARAM vk, LPARAM lParam)
{
    WPARAM new_vk = vk;
    UINT scancode = (lParam & 0x00ff0000) >> 16;
    int extended  = (lParam & 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是不是一个映射到左/右的版本,原来的钥匙code传递回不变。所以,你可以运行 WM_KEYDOWN / WM_KEYUP / WM_SYSKEYDOWN / WM_SYSKEYUP 通过当您需要在左右变型来区分功能的消息参数。

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 WM_KEYDOWN/WM_KEYUP/WM_SYSKEYDOWN/WM_SYSKEYUP message parameters through the function whenever you need to distinguish between the left and right variants.

这篇关于我如何左右键(Ctrl和Alt)区分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆