我正在使用 gtk 和 xlib(xtst) 创建一个程序来将假按键发送到应用程序,我创建了这个循环来将按键发送到事件窗口:
Display *dis;
dis = XOpenDisplay(NULL);
KeyCode modcode = 0; //init value
int i;
char hello[]="hello world";
char temp[1];
int size=sizeof(hello);
sleep(2);
for (i=0;i<size;i++)
{
temp[0]=hello[i];
temp[1]='\0'; //string terminator
g_print("%s\n",temp); //works fine, whitespace is printed
modcode = XKeysymToKeycode(dis, XStringToKeysym(temp));
XTestFakeKeyEvent(dis, modcode, False, 0);
XFlush(dis);
// sleep(1);
XTestFakeKeyEvent(dis, modcode, True, 0);
XFlush(dis);
XTestFakeKeyEvent(dis, modcode, False, 0);
XFlush(dis);
}
问题是它打印“helloworld”,它无法处理空格或任何特殊字符
谢谢
最佳答案
Display *dis;
dis = XOpenDisplay(NULL);
KeyCode modcode = 0; //init value
int i;
char hello[]="hello world";
char temp[2]; //Size is 2!!!
int size=sizeof(hello);
sleep(2);
for (i=0;i<size;i++)
{
temp[0]=hello[i];
temp[1]='\0'; //string terminator
g_print("%s\n",temp); //works fine, whitespace is printed
if (temp[0] == ' ')
modcode = XK_KP_Space
else
modcode = XKeysymToKeycode(dis, XStringToKeysym(temp));
XTestFakeKeyEvent(dis, modcode, False, 0);
XFlush(dis);
// sleep(1);
XTestFakeKeyEvent(dis, modcode, True, 0);
XFlush(dis);
XTestFakeKeyEvent(dis, modcode, False, 0);
XFlush(dis);
}
您将 2 个值存储到一个值数组中。将 temp[1] 更改为 temp[2](如示例中所示)。
关于c - al 发送假键盘事件、空白问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31024663/