c - al 发送假键盘事件、空白问题

标签 c linux gtk xlib

我正在使用 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/

相关文章:

c - *** 检测到堆栈崩溃 ***//尝试打印一个字符

linux - 从 url ssh 无效克隆 gitkraken 中的存储库

linux - Linux TAR 排除是如何工作的?

gtk - 在 GtkBuilder XML 的 GtkDialog 中使用预定义的响应 ID?

python - 在 PyGobject 中绘图 (python3)

c - 如何在 C 中创建高效的选取框函数

c - 通过 rsh 运行守护进程

linux - kubelet 失败,kubelet cgroup 驱动程序 : "cgroupfs" is different from docker cgroup driver: "systemd"

多线程和 gtk2hs

c - 三元(条件)表达式的类型错误