KeyRelease 事件从未发送
我在捕捉按键释放事件以阻止我的角色在游戏中行走时遇到了一些问题。
我正在尝试这样做:
switch (xev.type)
{
case Expose:
{
XGetWindowAttributes(dpy, win, &gwa);
glViewport(0, 0, gwa.width, gwa.height);
}
break;
case KeyPress:
{
int key = XLookupKeysym(&xev.xkey, 0);
if (key == XK_Escape)
{
glXMakeCurrent(dpy, None, NULL);
glXDestroyContext(dpy, glc);
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
running = false;
return 0;
}
else
{
input->setKey(key, true);
}
}
break;
case KeyRelease:
{
unsigned short is_retriggered = 0;
if (XEventsQueued(dpy, QueuedAfterReading))
{
XEvent nev;
XPeekEvent(dpy, &nev);
if (nev.type == KeyPress && nev.xkey.time
== xev.xkey.time && nev.xkey.keycode
== xev.xkey.keycode)
{
// delete retriggered KeyPress event
XNextEvent(dpy, &xev);
is_retriggered = 1;
}
}
if (!is_retriggered)
input->setKey(XLookupKeysym(&xev.xkey, 0), false);
}
break;
}
但我只收到重新触发的按键释放事件,这是我不想要的。 (尽管释放/重新按下会产生相同的结果,但将来可能会出现问题) 当我物理释放按键时,没有捕获任何事件。
哦,input->setKey() 基本上在 std::map 中将 bool 设置为 true(或 false),没什么特别的
I'm having a little problem catching a key released event to stop my character from walking in my game..
i'm trying to do this:
switch (xev.type)
{
case Expose:
{
XGetWindowAttributes(dpy, win, &gwa);
glViewport(0, 0, gwa.width, gwa.height);
}
break;
case KeyPress:
{
int key = XLookupKeysym(&xev.xkey, 0);
if (key == XK_Escape)
{
glXMakeCurrent(dpy, None, NULL);
glXDestroyContext(dpy, glc);
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
running = false;
return 0;
}
else
{
input->setKey(key, true);
}
}
break;
case KeyRelease:
{
unsigned short is_retriggered = 0;
if (XEventsQueued(dpy, QueuedAfterReading))
{
XEvent nev;
XPeekEvent(dpy, &nev);
if (nev.type == KeyPress && nev.xkey.time
== xev.xkey.time && nev.xkey.keycode
== xev.xkey.keycode)
{
// delete retriggered KeyPress event
XNextEvent(dpy, &xev);
is_retriggered = 1;
}
}
if (!is_retriggered)
input->setKey(XLookupKeysym(&xev.xkey, 0), false);
}
break;
}
But I only get the re-triggered key release events, which I don't want. (even though a release/re-press would have the same result, but in the future it might give problems)
When i physically release the key, no event is caught.
oh, and input->setKey() basically sets a bool to true (or false) in a std::map, nothing special
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个常见的问题。如果您没有注册特定活动(或全部),您将不会收到通知。
我们所有人,无论经历如何,有一天都会陷入困境......:)
一些有用的链接:
This is a common gotcha. If you don't register for specific events (or all), you will not be notified.
All of us, doesn't matter the experience, will fall in this one day... :)
Some usefull links:
注册 KeyReleaseMask 解决了这个问题。
XSelectInput(dis, win, KeyPressMask | KeyReleaseMask);
Registering the KeyReleaseMask solved the problem.
XSelectInput(dis, win, KeyPressMask | KeyReleaseMask);