简体   繁体   English

仅当窗口处于焦点时才寻找按键

[英]look for keypress only if window is in focus

I am writing a program which interfaces with some testing equipment. 我正在编写一个与某些测试设备连接的程序。 I want the user to be able to press a button to stop the test, but only if the program is in focus. 我希望用户能够按下按钮来停止测试,但前提是程序处于焦点位置。 This is because they can be using their pc for other work at the same time. 这是因为他们可以同时将自己的PC用于其他工作。 This is what I have now. 这就是我现在所拥有的。

if ((GetAsyncKeyState(VK_ESCAPE) & 0x8000)&&(GetFocus() == FindWindow(NULL,TEXT("Window name")))){ 如果((GetAsyncKeyState(VK_ESCAPE)&0x8000)&&(GetFocus()== FindWindow(NULL,TEXT(“窗口名称”)))){

 break;

} }

The problem is, the keypress is still determined even when the user is working on other programs. 问题是,即使用户正在使用其他程序,仍会确定按键。 I also want this to happen without waiting for user input so the program can continue running(i realize I could probably use threading but I think that seems a little overkill in this case). 我也希望在不等待用户输入的情况下发生这种情况,以便程序可以继续运行(我意识到我可能可以使用线程,但是在这种情况下,我认为这似乎有点过头了)。 If anybody knows a statement I can use in the second half of the IF statement that would be great, or even a different method altogether. 如果有人知道我可以在IF语句的下半部分使用一个很好的语句,甚至可以使用完全不同的方法。

Thank you. 谢谢。

Wild guess this one. 狂猜这个。 Maybe you could use the GetForegroundWindow function and check the handle against your test window handle. 也许您可以使用GetForegroundWindow函数并对照测试窗口句柄检查该句柄。 Possibly a better method is for your test window to handle the WM_KEYDOWN message. 可能更好的方法是让您的测试窗口处理WM_KEYDOWN消息。 My understanding is that this message is only posted if the window has focus. 我的理解是,仅当窗口具有焦点时才会发布此消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM