微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用X11显示获取UTF-8input

我一直在尝试,并在互联网上阅读大量的资源,试图find一种方法来从X显示获得UTF-8键盘(组合)input。 但我无法做到这一点。

我试过这个链接的示例代码(例如11-4),但没有成功。

我也写了一个简单的例子(下面),试图使其工作。 我简单的testing案例是打印一个“é”,这是通过input急性,然后是e。

哪里不对?

Windows Phone 8上的ARM NEON程序集无法正常工作

IE8不显示XML,但给一个popup窗口说:“你想保存这个文件吗?”为什么?

sliksvn显示popup窗口,“驱动器中没有磁盘”

在IIS中TLS相互身份validation,无需重新协商

如何在Windows中与Vagrant同步文件夹?

谢谢,

这是我的例子:

#include <X11/Xlib.h> #include <X11/Xutil.h> #include <X11/Xresource.h> #include <X11/Xlocale.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc,char ** argv) { int screen_num,width,height; unsigned long background,border; Window win; XEvent ev; display *dpy; XIM im; XIC ic; char *Failed_arg; Ximstyles *styles; Ximstyle xim_requested_style; /* First connect to the display server,as specified in the disPLAY environment variable. */ if (setlocale(LC_ALL,"") == NULL) { return 9; } if (!XSupportsLocale()) { return 10; } if (XSetLocaleModifiers("") == NULL) { return 11; } dpy = XOpendisplay(NULL); if (!dpy) { fprintf(stderr,"unable to connect to display"); return 7; } /* these are macros that pull useful data out of the display object */ /* we use these bits of info enough to want them in their own variables */ screen_num = DefaultScreen(dpy); background = BlackPixel(dpy,screen_num); border = WhitePixel(dpy,screen_num); width = 400; /* start with a small window */ height = 200; win = XCreateSimpleWindow(dpy,DefaultRootwindow(dpy),/* display,parent */ 0,/* x,y: the window manager will place the window elsewhere */ width,height,/* width,height */ 2,border,/* border width & colour,unless you have a window manager */ background); /* background colour */ /* tell the display server what kind of events we would like to see */ XSelectInput(dpy,win,ButtonPressMask|StructureNotifyMask|KeyPressMask|keyreleaseMask|KeymapStateMask); /* okay,put the window on the screen,please */ XMapWindow(dpy,win); im = XOpenIM(dpy,NULL,NULL); if (im == NULL) { fputs("Could not open input methodn",stdout); return 2; } Failed_arg = XGetIMValues(im,XNQueryInputStyle,&styles,NULL); if (Failed_arg != NULL) { fputs("XIM Can't get stylesn",stdout); return 3; } int i; for (i = 0; i < styles->count_styles; i++) { printf("style %dn",styles->supported_styles[i]); } ic = XCreateIC(im,XNInputStyle,XIMPreeditnothing | Ximstatusnothing,XNClientwindow,NULL); if (ic == NULL) { printf("Could not open ICn"); return 4; } XSetICFocus(ic); /* as each event that we asked about occurs,we respond. In this * case we note if the window's shape changed,and exit if a button * is pressed inside the window */ while(1) { XNextEvent(dpy,&ev); switch(ev.type){ case KeymapNotify: XRefreshKeyboardMapping(&ev.xmapping); break; case KeyPress: { int count = 0; KeySym keysym = 0; char buf[20]; Status status = 0; count = Xutf8LookupString(ic,(XKeypressedEvent*)&ev,buf,20,&keysym,&status); printf("count: %dn",count); if (status==XBufferOverflow) printf("BufferOverflown"); if (count) printf("buffer: %sn",buf); if (status == XLookupKeySym || status == XLookupBoth) { printf("status: %dn",status); } printf("pressed KEY: %dn",keysym); } break; case keyrelease: { int count = 0; KeySym keysym = 0; char buf[20]; Status status = 0; count = XLookupString((XKeyEvent*)&ev,NULL); if (count) printf("in release buffer: %sn",buf); printf("released KEY: %dn",keysym); } break; case ConfigureNotify: if (width != ev.xconfigure.width || height != ev.xconfigure.height) { width = ev.xconfigure.width; height = ev.xconfigure.height; printf("Size changed to: %d by %d",height); } break; case ButtonPress: XClosedisplay(dpy); return 0; } fflush(stdout); } }

为什么GetLogPen使用自定义笔失败?

Windows 7上个别程序的峰值计数器

获取Mercurial进程内挂接在Windows上运行

主窗体窗体中的forms

fread()不写入缓冲区

你必须这样做:

if (XFilterEvent(&ev,win)) continue;

在你的事件循环。 这运行输入法的机器,没有它你会得到原始的X事件。 例如,当您按死音调键后跟一个字母键,并且不要调用XFilterEvent ,您将照常获得两个KeyPress事件。 但是,如果你打电话,你会得到三个事件。 有两个原始事件, XFilterEvent(&ev,win)返回True 。 然后有一个事件通过输入法合成,为此XFilterEvent(&ev,win)返回False 。 这是包含重音字符的第三个事件。

如果您想同时使用原始事件和输入法合成的事件,那么您当然可以自己处理原始事件而不是continue 。

注意你需要buf[count] = 0; 为了正确地打印buf (或明确地使用长度), Xutf8LookupString不会null结束它的输出

最后,正如评论中提到的那样,对于X11的最新版本,您需要指定对XSetLocaleModifiers的修改,如XSetLocaleModifiers("@im=none") ,否则将不会生成额外的事件。

这里是一个更正的代码版本:

#include <X11/Xlib.h> #include <X11/Xutil.h> #include <X11/Xresource.h> #include <X11/Xlocale.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc,"") == NULL) { return 9; } if (!XSupportsLocale()) { return 10; } if (XSetLocaleModifiers("@im=none") == NULL) { return 11; } dpy = XOpendisplay(NULL); if (!dpy) { fprintf(stderr,ButtonPressMask|StructureNotifyMask|KeyPressMask|keyreleaseMask); /* okay,(int)styles->supported_styles[i]); } ic = XCreateIC(im,&ev); if (XFilterEvent(&ev,win)) continue; switch(ev.type){ case MappingNotify: XRefreshKeyboardMapping(&ev.xmapping); break; case KeyPress: { int count = 0; KeySym keysym = 0; char buf[20]; Status status = 0; count = Xutf8LookupString(ic,count); if (status==XBufferOverflow) printf("BufferOverflown"); if (count) printf("buffer: %.*sn",count,(int)keysym); } break; case keyrelease: { int count = 0; KeySym keysym = 0; char buf[20]; Status status = 0; count = XLookupString((XKeyEvent*)&ev,NULL); if (count) printf("in release buffer: %.*sn",(int)keysym); } break; case ConfigureNotify: if (width != ev.xconfigure.width || height != ev.xconfigure.height) { width = ev.xconfigure.width; height = ev.xconfigure.height; printf("Size changed to: %d by %d",height); } break; case ButtonPress: XClosedisplay(dpy); return 0; } fflush(stdout); } }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐