我找到了一个example (接受的答案)UTF-8 输入,但我想知道这个例子是否正确。 XGetICValues
的结果可以传递给 XSelectInput
:
unsigned long mask = 0;
if (!XGetICValues(ic, XNFilterEvents, &mask, NULL))
{
XSelectInput(dpy, win, other_masks | mask);
}
您能解释一下上述代码的含义吗?是否必须获得正确的 UTF-8 输入?执行上述代码后是否还应该调用 XFilterEvent
?
最佳答案
Should I also call XFilterEvent after executing the above code ?
我怀疑您应该:为了正确处理 UTF-8 输入,它不仅涉及获取事件掩码和选择输入事件,还可能过滤这些事件以正确处理多字节或特殊输入序列。
XFilterEvent
应该在事件循环中使用。它将有助于管理输入法以正确解释多字节或特殊按键序列。
正如“XLIB Programming Manual / Filtering Events”中提到的,它是在XNextEvent()
之后调用的。 .
#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)
{
// Your initialization code
while(1) {
XNextEvent(dpy, &ev);
if (XFilterEvent(&ev, win)) // Filter the event through the input method
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, (XKeyPressedEvent*)&ev, buf, 20, &keysym, &status);
printf("count: %d\n", count);
if (status == XBufferOverflow)
printf("BufferOverflow\n");
if (count)
printf("buffer: %.*s\n", count, buf);
if (status == XLookupKeySym || status == XLookupBoth) {
printf("status: %d\n", status);
}
printf("pressed KEY: %d\n", (int)keysym);
}
break;
case KeyRelease:
// Rest of your code for other cases
}
fflush(stdout);
}
}
如果XFilterEvent
返回True
,表明该事件已被输入法过滤处理,则执行continue
语句跳过循环的其余部分并处理下一个事件。
+--------------------+ XNFilterEvents +--------------+
| Input Context (IC) |------------------------->| Event Mask |
+--------------------+ +--------------+
| |
XGetICValues Retrieved Mask
| |
V V
+--------------------+ other_masks | mask +--------------+
| XSelectInput |------------------------->| Event Input |
+--------------------+ +--------------+
| |
| +--------------+
| | X Event |
| +--------------+
| |
V V
+--------------------+ XFilterEvent +--------------+
| Event Loop |------------------------->| Event Filter |
+--------------------+ (optional) +--------------+
- Input Context (IC)查询以获取事件掩码,
XSelectInput
需要该事件掩码来了解要监听哪些事件。 -
XSelectInput
使用其他掩码和检索到的掩码的组合来调用,以指定应为指定窗口报告的事件。 - 在事件循环中,调用
XFilterEvent
来过滤事件队列中的相关输入事件,在处理多字节或特殊输入序列时特别有用。此步骤被标记为可选,因为它基于处理特殊输入序列的要求。
关于linux - UTF-8 输入和使用 XGetICValues,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77358370/