How to simulate button input and mouse simulation in linux

Source: Internet
Author: User
In linux, how does one simulate key input and mouse input? Linux general technology-Linux programming and kernel information. The following is a detailed description. View the type of/dev/input/eventX events, cat/proc/bus/input/devices

The device has its own special key code, I need to be some standard keys, such as 0-9, X-Z analog into a standard button, such as KEY_0, KEY-Z, so need to use the key simulation, the specific method is to operate the/dev/input/event1 file and write an input_event struct to it to simulate the input of the key.

Linux/input. h is defined. This file also defines the encoding of standard keys.

Struct input_event {

Struct timeval time; // key time

_ 2010type; // type, which is defined below

_ Code; // the buttons to simulate

_ S32 value; // whether to press or release

};

Code:

Event code. If the event type code is EV_KEY, the code is the device keyboard code. The code is 0 ~ 127 is the key code on the keyboard, 0x110 ~ 0x116 indicates the code on the mouse. 0x110 (BTN _ LEFT) indicates the LEFT mouse button, 0x111 (BTN_RIGHT) indicates the right mouse button, and 0x112 indicates the BTN _ MIDDLE) in the left-side navigation pane. for other code meanings, see include/linux/input. h file. if the event type code is EV_REL, the code value indicates the trajectory type. for example, it indicates the x-axis direction of the mouse (Code: 0x00) and the y-axis direction of the mouse (Code: 0x01 ), indicates the direction of the wheel in the mouse (Code: 0x08 ).

Type:

EV_KEY, keyboard

EV_REL, Relative Coordinate

EV_ABS, absolute Coordinate

Value:

Event value. if the event type code is EV_KEY, when the press key value is 1, the release value is 0; if the event type code is EV _ REL, the positive and negative values of value represent two values in different directions.

/*

* Event types

*/

# Define EV_SYN 0x00

# Define EV_KEY 0x01 // press the key

# Define EV_REL 0x02 // Relative Coordinate (trackball)

# Define EV_ABS 0x03 // absolute Coordinate

# Define EV_MSC 0x04 // others

# Define EV_SW 0x05

# Define EV_LED 0x11 // LED

# Define EV_SND 0x12 // sound

# Define EV_REP 0x14 // repeat

# Define EV_FF 0x15

# Define EV_PWR 0x16

# Define EV_FF_STATUS 0x17

# Define EV_MAX 0x1f

# Define EV_CNT (EV_MAX + 1)

1. Analog button Input

// 0 indicates release, 1 indicates Press, 2 indicates always press

// 0 for EV_KEY for release, 1 for keypress and 2 for autorepeat.

Void simulate_key (int fd, int value)

{

Struct input_event event;

Event. type = EV_KEY;

// Event. code = KEY_0; // button to simulate

Event. value = value; // whether to press, release, or repeat

Gettimeofday (& event. time, 0 );

If (write (fd, & event, sizeof (event) <0 ){

Dprintk ("simulate key error ~~~ \ N ");

Return;

}

}

2. Simulate mouse input (trackball)

Void simulate_mouse (int fd, char buf [4])

{

Int rel_x, rel_y;

Static struct input_event event, ev;

// Buf [0] And buf [2]. If the value is smaller than 0, it is left-shifted. If the value is greater than 0, it is right-shifted.

// Buf [1], buf [3]. If it is smaller than 0, it is moved down. If it is greater than 0, it is moved up.

Dprintk ("mouse touch: x1 = % d, y1 = % d, x2 = % d, y2 = % d \ n", buf [0], buf [1], buf [2], buf [3]);

Rel_x = (buf [0] + buf [2])/2;

Rel_y =-(buf [1] + buf [3])/2; // opposite to our mouse, so reverse

Event. type = EV_REL;

Event. code = REL_X;

Event. value = rel_x;

Gettimeofday (& event. time, 0 );

If (write (fd, & event, sizeof (event ))! = Sizeof (event ))

Dprintk ("rel_x error ~~~ : % S \ n ", strerror (errno ));

Event. code = REL_Y;

Event. value = rel_y;

Gettimeofday (& event. time, 0 );

If (write (fd, & event, sizeof (event ))! = Sizeof (event ))

Dprintk ("rel_y error ~~~ : % S \ n ", strerror (errno ));



// Be sure to refresh the empty

Write (fd, & ev, sizeof (ev ));

}

How to open the mouse and keyboard file:

Int fd_kbd; // dev/input/event1

Int fd_mouse; // dev/input/mouse2

Fd_kbd = open ("/dev/input/event1", O_RDWR );

If (fd_kbd <= 0 ){

Printf ("error open keyboard: % s \ n", strerror (errno ));

Return-1;

}

Fd_mouse = open ("/dev/input/event3", O_RDWR); // if not, try/dev/input/mice.

If (fd_mouse <= 0 ){

Printf ("error open mouse: % s \ n", strerror (errno ));

Return-2;

}

}

/Dev/input/mice is the abstraction of the mouse, which indicates the mouse, maybe/dev/input/mouse,/dev/input/mouse1, or empty,

This file will always exist.

Here, you may ask, how do I know what the/dev/input/eventX events are, whether it is a mouse, a keyboard, or something else,

EventX represents the events of all input devices (input core), such as pressing keys, moving the mouse, or remote control,

You can view the device events of each eventX in cat/proc/bus/input/devices.

PS: For GTK usage, refer to the gtk_main_do_event function.

Static void simulate_key (GtkWidget * window, int keyval, int press)

{

GdkEvent * event;

GdkEventType type;

If (press)

Type = GDK_KEY_PRESS;

Else

Type = GDK_KEY_RELEASE;

Event = gdk_event_new (type );

// Event-> key. send_event = TRUE;

Event-> key. window = window-> window; // you must set it as the main window.

Event-> key. keyval = keyval;

// FIXME: you must add this parameter, or else it is prone to errors.

G_object_ref (event-> key. window );

Gdk_threads_enter ();

// FIXME: Remember to use this to send events

Gtk_main_do_event (event );

Gdk_threads_leave ();

Gdk_event_free (event );

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.