忽略 X11 应用程序中的自动重复 [英] Ignore auto repeat in X11 applications

查看:12
本文介绍了忽略 X11 应用程序中的自动重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您在启用 AutoRepeat 时按住 X11 中的某个键,您会不断收到 KeyPressKeyRelease 事件.我知道 AutoRepeat 可以使用函数 XAutoRepeatOff() 禁用,但这会改变整个 X 服务器的设置.有没有办法为单个应用程序禁用 AutoRepeat 或忽略重复的击键?

If you press and hold a key in X11 while AutoRepeat is enabled, you continuously receive KeyPress and KeyRelease events. I know that AutoRepeat can be disabled using the function XAutoRepeatOff(), but this changes the setting for the whole X server. Is there a way to either disable AutoRepeat for a single application or to ignore repeated keystrokes?

我正在寻找的是按下一个键时的单个 KeyPress 事件和释放键时的单个 KeyRelease 事件,而不会干扰 X 服务器的自动重复设置.

What I'm looking for is a single KeyPress event when a key is pressed and a single KeyRelease event when a key is released, without interfering with the X server's AutoRepeat setting.

这是一个帮助您入门的最小示例(主要来自 初学者 Xlib 教程):

Here's a minimal example to get you going (mostly from the Beginner Xlib Tutorial):

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>

Display *dis;
Window win;
XEvent report;

int main ()
{
  dis = XOpenDisplay (NULL);
  // XAutoRepeatOn(dis);
  win = XCreateSimpleWindow (dis, RootWindow (dis, 0), 1, 1, 500, 500,
        0, BlackPixel (dis, 0), BlackPixel (dis, 0));
  XSelectInput (dis, win, KeyPressMask | KeyReleaseMask);
  XMapWindow (dis, win);
  XFlush (dis);

  while (1)
    {
      XNextEvent (dis, &report);
      switch (report.type)
 {
 case KeyPress:
   fprintf (stdout, "key #%ld was pressed.
",
     (long) XLookupKeysym (&report.xkey, 0));
   break;
 case KeyRelease:
   fprintf (stdout, "key #%ld was released.
",
     (long) XLookupKeysym (&report.xkey, 0));
   break;
 }
    }

  return (0);
}

推荐答案

当你收到一个按键释放并且下一个事件是同一个按键组合的按键时,它会自动重复并且按键没有被准确释放.您可以使用这样的代码来查看下一个事件

When you receive a key release and the next event is a key press of the same key combination, then it's auto-repeat and the key wasn't acutally released. You can use code like this to peek next event

if (event->type == KeyRelease && XEventsQueued(disp, QueuedAfterReading))
{
  XEvent nev;
  XPeekEvent(disp, &nev);
    
  if (nev.type == KeyPress && nev.xkey.time == event->xkey.time &&
      nev.xkey.keycode == event->xkey.keycode)
  {
    /* Key wasn’t actually released */
  }
}

这篇关于忽略 X11 应用程序中的自动重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆