使用 XLookupString 处理 C-q [英] Handle C-q with XLookupString

查看:35
本文介绍了使用 XLookupString 处理 C-q的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个处理 Ctrl-q 事件以退出应用程序的 X11 代码的工作示例:

This is a working example of X11 code that handles Ctrl-q event to quit application:

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

void exitOnCondition(char cond, const char *msg, int exitCode, Display *dpy, Window *w, GC *gc) {
    if(cond) {
        printf("%s\n", msg);
        if(dpy && gc) XFreeGC(dpy, *gc);
        if(dpy && w) XDestroyWindow(dpy, *w);
        if(dpy) XCloseDisplay(dpy);
        exit(exitCode);
    }
}

int main(int argc, char **argv) {
      Display *dpy = XOpenDisplay(0);
      exitOnCondition(dpy == 0, "Error: XOpenDisplay failed", -1, dpy, 0, 0);

      int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
      int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));

      Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 
                     200, 100, 0, blackColor, blackColor);

      //Tell X Server to send MapNotify events
      XSelectInput(dpy, w, StructureNotifyMask | KeyPressMask);

      //Make window appear
      XMapWindow(dpy, w);

      //Graphics Context
      GC gc = XCreateGC(dpy, w, 0, 0);

      //Set white color for drawing
      XSetForeground(dpy, gc, whiteColor);

      //Wait for the MapNotify event
      for(;;) {
        XEvent e;
        XNextEvent(dpy, &e);
        if (e.type == MapNotify) {
          break;
        }
      }

      //Draw the line
      XDrawLine(dpy, w, gc, 10, 60, 180, 20);

      //Send the "DrawLine" request to the server
      XFlush(dpy);

      char text[255];
      XEvent e;
      KeySym key;
      int numKeys = 0;
      for(;;) {
          XNextEvent(dpy, &e);
          if(e.type == KeyPress) {
              //With modifier XLookupString will return garbage(?) in text[0] and key as latin1
              if((numKeys = XLookupString(&e.xkey, text, 255, &key, 0))) {
                  printf("lookup returned:\n");
                  for(int i = 0; i < numKeys; i++) {
                      printf("text[%d]=%x\n", i, text[i]);
                  }

                  if(e.xkey.state == ControlMask && key == XK_q) {
                      exitOnCondition(1, "C-Q pressed", 0, dpy, &w, &gc);
                  }
              }
          }
      }

      XFreeGC(dpy, gc);
      XDestroyWindow(dpy,w);
      XCloseDisplay(dpy);   
}

这段代码能在任何系统上正确处理 Ctrl-q 事件吗?

Will this code correctly handle Ctrl-q event on any system?

我可以使用 e.xkey.state 在 XLookupString 之后检查任何键盘布局的 Ctrl 修饰符,即使 Ctrl 被反弹到 CAPS Lock(或其他任何东西)?

Can I use e.xkey.state to check for Ctrl modifier after XLookupString for any keyboard layout even when Ctrl is rebound to CAPS Lock(or anything else)?

为什么 XLookupString 为 Ctrl-q 事件返回一个符号 text[0]==0x11 而不是 text[0]==CtrlModifierCode text[1]=='q'?

Why does XLookupString return one symbol text[0]==0x11 for Ctrl-q event and not text[0]==CtrlModifierCode text[1]=='q'?

推荐答案

XLookupString 返回一个 ISO-8859-1 字符序列(至少根据我的手册;我不知道它是最新的).ISO-8859-1 中没有单独用于ctrl"键的字符代码.严格来说,ctrl-q 组合也没有,但是 传统规定 ctrl + (A...Z) 映射到 1...26 范围.

XLookupString returns a sequence of ISO-8859-1 characters (at least according to the manual I have; I do not know how up-to-date it is). There is no character code in ISO-8859-1 for the "ctrl" key by itself. Strictly speaking, there isn't one for the ctrl-q combination either, but tradition dictates that ctrl + (A...Z) map to the 1...26 range.

这篇关于使用 XLookupString 处理 C-q的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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