Windows 中的按键事件
是否可以在 Windows (XP) 中获取按下按键的事件?我有一个线程,它有一个 while(1) 循环,我在那里打印一些数据。它必须是同步的,这就是我使用 WaitForMultipleObjects(2, events, FALSE, INFINITE); 的原因; events 是一个句柄数组,它包含 2 个句柄。其中一个是来自另一个线程的事件,该事件表明服务器收到一条新消息,另一个事件应该向我发出用户按下某个键 (1-7) 的信号。我如何获得第二个句柄/事件?
is it possible to get an event for pressed key in Windows (XP)? I have a thread, it has a while(1)-loop and i print some data there. It must be synchronize thats why i use WaitForMultipleObjects(2, events, FALSE, INFINITE);
events is an array of handles and it contains 2 handles. One of them is an event from the other thread that signals, that the server got a new message and the other one should signal me that the user pressed a key (1-7). How can i get this second handle/event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在寻找
MsgWaitForMultipleObjects
。这也可以检索消息,例如WM_KEYDOWN
。对于关键事件,您不需要HANDLE
。You're looking for
MsgWaitForMultipleObjects
. This can retrieve messages as well, such asWM_KEYDOWN
. You don't need aHANDLE
for key events.您必须实现一个消息循环来侦听
WM_KEYDOWN
或WM_KEYUP
。然后你应该调用适当的方法。You have to implement a message loop to listen for
WM_KEYDOWN
orWM_KEYUP
. Then you should call the appropriate method.在您的程序中,您应该有一个线程处理到达程序的事件,例如键盘、鼠标等。在该线程中,您可以检测按下或释放您感兴趣的键的键盘事件是否到达。如果是这样,您向另一个线程发出信号。
In your program, you should have a thread handling events arriving to your program, such as keyboard, mouse etc. In that thread, you can detect if a keyboard event pressing or releasing the key you are interested in arrives. If so, you signal your other thread.