Produce keyboard Events key hits

人盡茶涼 提交于 2019-12-03 08:44:33

Here's a simple example using libxdo (from xdotool). (Caveat: I am the xdotool author)

 /* File: testkey.c
 *
 * Compile with:
 * gcc -lxdo testkey.c
 *
 * Requires libxdo (from xdotool project)
 */

#include <xdo.h>

int main() {
  xdo_t *xdo = xdo_new(NULL);
  xdo_keysequence(xdo, CURRENTWINDOW, "A", 0);
  return  0;
}

Take a look at xsendkey. The sources are included and are short, so you extract the necessary parts from it into your program.

Although this is not C, you can produce key hits in Java very easily:

import java.awt.Robot;
import java.awt.AWTException;
import java.awt.event.KeyEvent;


public class key
{
    public static void main(String args[])
    {
        try {
            Robot r = new Robot();
            r.delay(2000);
            r.keyPress(KeyEvent.VK_W);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
};

Have a look at Swinput.

Swinput can fake a mouse and a keyboard by using the Linux Input System. The swinput modules read from a device and fakes hardware event (mouse motion, key presses etc) as commands written on the devices.

Madni

Get Fake Key Events by Xdotool

//Compile As:  gcc button.c -lX11 

#include < X11/Xlib.h >
#include < X11/Xutil.h >
#include < stdio.h >
#include < X11/extensions/XTest.h >

void press_button()
{   
    Display *d;
    d = XOpenDisplay(NULL);
        if(d == NULL)
        {
            //fprintf(stderr, "Errore nell'apertura del Display !!!\n");
            //exit(0);
        }
    system("xdotool key Shift+a");
    XFlush(d);
    XCloseDisplay(d);
}

int main() {
    press_button();
    return 0;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!