使用 GetKeyState()

发布于 2024-11-15 16:53:10 字数 402 浏览 3 评论 0原文

我希望在按下按键时有一个布尔事件切换。具体来说,是 's' 键。我被指出函数 GetKeyState(),据说它在 Win32 API 下工作。我知道字母“s”的 ASCII 代码是 115,所以我的代码如下:

if (GetKeyState(115) == 1)
{
<EVENT>
}

但是,这不起作用。为什么?以下是 MSDN 参考: http://msdn .microsoft.com/en-us/library/ms646301%28v=vs.85%29.aspx ...“如果低位为1,则密钥已切换”

I would like to have a boolean event toggle when a key is pressed. Specifically, the 's' key. I have been pointed to the function GetKeyState(), which supposedly works under the Win32 API. I understand the ASCII code for the letter 's' is 115, and so my code is as follows:

if (GetKeyState(115) == 1)
{
<EVENT>
}

However, this does not work. Why? Here is the MSDN reference: http://msdn.microsoft.com/en-us/library/ms646301%28v=vs.85%29.aspx ... "If the low-order bit is 1, the key is toggled"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风追烟花雨 2024-11-22 16:53:10

据我了解,你需要做的:

if( GetKeyState(115) & 0x8000 )
{
    <EVENT>
}

最高位告诉是否按下了键。最低的表示按键是否已切换(例如,大写锁定是否已打开)。

From what I understand you need to do:

if( GetKeyState(115) & 0x8000 )
{
    <EVENT>
}

The highest bit tells if key is pressed. The lowest tells if key is toggled (like, if caps lock is turned on).

禾厶谷欠 2024-11-22 16:53:10

由于 SHORT 有符号,因此高位等于符号位。

因此,要测试是否按下给定的键,只需测试 GetKeyState() 返回的值是否为负:

if (GetKeyState('S') < 0) {
    // The S key is down.
} else {
    // The S key is up.
}

此外,115 是 's' 的 ASCII 代码。我相信,您应该使用大写字母 83 来测试“S”键。

Since SHORT is signed, high-order bit equals sign bit.

Therefore to test if a given key is pressed, simply test if the value returned by GetKeyState() is negative:

if (GetKeyState('S') < 0) {
    // The S key is down.
} else {
    // The S key is up.
}

Besides, 115 is ASCII code for 's'. I believe, you should use capital case 83 to test the 'S' key.

绿光 2024-11-22 16:53:10

我使用全局变量 bool altgr

示例:

void Editor::HandleKey(char car) {

    bool shift = false;
    //bool altgr = false;
    bool printable = false;

    if (car == 27) SendMessage(hwnd, WM_DESTROY, 0, 0);

    if ((GetKeyState(VK_CAPITAL) & 0x0001) == 1) shift = true;
    if ((GetKeyState(VK_SHIFT) & 0x8000) == 0x8000) shift = true;
    // if(GetKeyState(VK_RMENU) & 0x80000000 == 0x80000000) altgr = true;
    if (car == 18) altgr = true; 

I use a global variable bool altgr

Example:

void Editor::HandleKey(char car) {

    bool shift = false;
    //bool altgr = false;
    bool printable = false;

    if (car == 27) SendMessage(hwnd, WM_DESTROY, 0, 0);

    if ((GetKeyState(VK_CAPITAL) & 0x0001) == 1) shift = true;
    if ((GetKeyState(VK_SHIFT) & 0x8000) == 0x8000) shift = true;
    // if(GetKeyState(VK_RMENU) & 0x80000000 == 0x80000000) altgr = true;
    if (car == 18) altgr = true; 
你怎么这么可爱啊 2024-11-22 16:53:10

有时您想使用组合键

为了避免组合键(例如:VK_SHIFT && VK_LEFT)满足两个条件的情况:

std::cout << "Shift RIGHT pressed" << std::endl;
std::cout << "LEFT key pressed" << std::endl;

只需使用 Sleep(...);GetAsyncKeyState(VK_...) < /代码>

<一href="https://stackoverflow.com/questions/17770753/getkeystate-vs-getasynckeystate-vs-getch/17771038">GetKeyState() 与 GetAsyncKeyState() 与 getch() 比较?

#include <windows.h>  

    ...

    while (1)
    {
        if ((GetKeyState(VK_SHIFT) & 0x8000) && (GetAsyncKeyState(VK_LEFT) & 0x8000))
        {
            Sleep(200);
            std::cout << "Shift LEFT pressed" << std::endl;
        }

        if ((GetKeyState(VK_SHIFT) & 0x8000) && (GetAsyncKeyState(VK_RIGHT) & 0x8000))
        {
            Sleep(200);
            std::cout << "Shift RIGHT pressed" << std::endl;
        }

        if (GetAsyncKeyState(VK_RIGHT))
        {
            std::cout << "RIGHT key pressed" << std::endl;
        }

        if (GetAsyncKeyState(VK_LEFT))
        {
            std::cout << "LEFT key pressed" << std::endl;
        }
   }

Sometimes you want to use a combination of keys.

To avoid the situations when a combination of keys (eg: VK_SHIFT && VK_LEFT) satisfies two conditions:

std::cout << "Shift RIGHT pressed" << std::endl;
std::cout << "LEFT key pressed" << std::endl;

just use Sleep(...); and GetAsyncKeyState(VK_...)

GetKeyState() vs. GetAsyncKeyState() vs. getch()?

#include <windows.h>  

    ...

    while (1)
    {
        if ((GetKeyState(VK_SHIFT) & 0x8000) && (GetAsyncKeyState(VK_LEFT) & 0x8000))
        {
            Sleep(200);
            std::cout << "Shift LEFT pressed" << std::endl;
        }

        if ((GetKeyState(VK_SHIFT) & 0x8000) && (GetAsyncKeyState(VK_RIGHT) & 0x8000))
        {
            Sleep(200);
            std::cout << "Shift RIGHT pressed" << std::endl;
        }

        if (GetAsyncKeyState(VK_RIGHT))
        {
            std::cout << "RIGHT key pressed" << std::endl;
        }

        if (GetAsyncKeyState(VK_LEFT))
        {
            std::cout << "LEFT key pressed" << std::endl;
        }
   }
Bonjour°[大白 2024-11-22 16:53:10

有点晚了,但高位是 0x80000000 而不是 0x8000,改变它,它会正常工作。

另一位 - 无用 - 它的作用就好像你在按下 LSHIFT 时按下了 CAPS LOCK。

Bit late for this but the high order bit is 0x80000000 not 0x8000, change this and it will work fine.

The other bit - useful for nothing - it acts as if you pressed CAPS LOCK when you pressed LSHIFT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文