如何检测密钥发布C++

发布于 2025-01-23 08:36:41 字数 644 浏览 3 评论 0原文

因此,我正在制作一个软件来帮助我进行Arduino项目,它被认为像Putty一样。我对Arduino进行了编程,以播放Qi(C4-C5)的不同笔记,并且该软件被弹奏以播放Thoes Notes,但是我需要将其检测到何时按下键并发布键,我该怎么做?我搜索并找到了一些东西,但是它不像在这里那样工作,这是代码:

int main(){
int KeyGet;

while(1)
    {
    KeyGet = getch();
    if (GetKeyState(0x51) & 0x8000)
    {
        cout<<"key is pressed"<< endl;
    }
    else if(GetKeyState(0x51)& 0x0001)
    {

        cout<<"key is released"<< endl;
    }


    }


return 0;

当我按Q(0x51)而不是“键释放”时,程序只是打印“按下键”发布“当我按其他Q的其他内容时,我尝试了getAsynckeystate,并尝试了

If (GetKeyState(0x51) != 0)

它仍然可以进行工作。

So i am making a software to help me with an arduino project, it is supossed to be like PUTTY. I programed the arduino to play different notes from Q-I(C4-C5) and the software is supossed to play thoes notes but i need to make it detect when the Key is pressed and released, how do i do that? I searched and found something but it is not working like it's supposed to here is the code:

int main(){
int KeyGet;

while(1)
    {
    KeyGet = getch();
    if (GetKeyState(0x51) & 0x8000)
    {
        cout<<"key is pressed"<< endl;
    }
    else if(GetKeyState(0x51)& 0x0001)
    {

        cout<<"key is released"<< endl;
    }


    }


return 0;

The program just prints "key is pressed" when i press Q(0x51) and not "key released" as it should, insted it prints "key released" when i press something else other the Q. And i tried GetAsyncKeyState and tried

If (GetKeyState(0x51) != 0)

it still doesent work.

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

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

发布评论

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

评论(1

沐歌 2025-01-30 08:36:41

getKeyState()需要一个窗口和一个消息循环,以使状态计算机更新。但是您没有消息循环。另外,getch()无论如何都会吞下任何键按+释放。

另外,getKeystate(0x51)&amp; 0x0001不是检测键发布的正确方法。该位用于检测可切换键的切换状态,例如 capslock 等。

在您的示例中,您需要摆脱getch()并使用getAsyncKeyState(),例如:

int main(){
    bool down = false;
    while (1) {
        if (GetAsyncKeyState(0x51) < 0) {
            if (!down) {
                down = true;
                cout << "key is pressed" << endl;
            }
        }
        else {
            if (down) {
                down = false;
                cout << "key is released"<< endl;
            }
        }
        Sleep(0);
    }
    return 0;
}

否则,您可以使用wh_keyboard [_ll]通过setWindowShookex()而不是挂钩,以便您可以接收实际的键down/down/down/down/down/实时通知。

GetKeyState() requires a window and a message loop to keep the state machine updated. But you do not have a message loop. Also, getch() would swallow any key press+release anyway.

Also, GetKeyState(0x51) & 0x0001 is not the right way to detect a key release. That bit is meant for detecting the toggle state of togglable keys like CapsLock, etc.

In your example, you would need to get rid of getch() and use GetAsyncKeyState() instead, eg:

int main(){
    bool down = false;
    while (1) {
        if (GetAsyncKeyState(0x51) < 0) {
            if (!down) {
                down = true;
                cout << "key is pressed" << endl;
            }
        }
        else {
            if (down) {
                down = false;
                cout << "key is released"<< endl;
            }
        }
        Sleep(0);
    }
    return 0;
}

Otherwise, you can use a WH_KEYBOARD[_LL] hook via SetWindowsHookEx() instead, so you can receive actual key down/up notifications in real-time.

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