如何检测密钥发布C++
因此,我正在制作一个软件来帮助我进行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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getKeyState()
需要一个窗口和一个消息循环,以使状态计算机更新。但是您没有消息循环。另外,getch()
无论如何都会吞下任何键按+释放。另外,
getKeystate(0x51)&amp; 0x0001
不是检测键发布的正确方法。该位用于检测可切换键的切换状态,例如 capslock 等。在您的示例中,您需要摆脱
getch()
并使用getAsyncKeyState()
,例如:否则,您可以使用
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 useGetAsyncKeyState()
instead, eg:Otherwise, you can use a
WH_KEYBOARD[_LL]
hook viaSetWindowsHookEx()
instead, so you can receive actual key down/up notifications in real-time.