This article was reproduced from: https://my.oschina.net/u/157503/blog/91548
1. Questions
A dead loop will read the keyboard corresponding to the device file will trigger the keyboard event to print out on the screen, press ESC to exit the program
The code is executed by the unbuntu10.04 compiler.
2, input_event description
In the Linux kernel, the input device is described by the INPUT_DEV structure, and when the input subsystem is used to drive the inputs, the core of the drive is to report the input events (event) of keystrokes, touch screens, keyboards, and mice to the system via Input_ Event struct description), no longer need to care about the file operation interface, because the input subsystem has completed the file operation interface
Linux/input.h This file defines the structure of event events, the coding of API and standard keys, etc.
struct Input_event {
struct Timeval time; Key Time
__U16 type; Event Type
__U16 Code; What keys to simulate
__S32 value;//is pressed or released
};
Type, which is the type of event, and the common types of events are:
Ev_key, key events, such as keyboard keys (press which key), the right mouse button (non-knockdown), etc.;
Ev_rel, relative coordinates, mainly refers to the movement of the mouse event (relative displacement);
Ev_abs, Absolute coordinates, mainly refers to the mobile event of the touch screen
Code: The codes for the event.
If the type code for the event is Ev_key, the code is the device keyboard code. 0~127 as the key code on the keyboard
The definition of the key code is shown in the following section:
#define KEY_RESERVED 0
#define KEY_ESC 1
#define KEY_1 2
#define KEY_2 3
#define KEY_3 4
#define KEY_4 5
#define KEY_5 6
#define KEY_6 7
#define KEY_7 8
#define KEY_8 9
#define KEY_9 10
#define KEY_0 11
#define Key_minus 12
#define Key_equal 13
#define KEY_BACKSPACE 14
#define KEY_TAB 15
#define KEY_Q 16
#define KEY_W 17
#define KEY_E 18
#define KEY_R 19
#define KEY_T 20
Value
The value of the event. If the type code of the event is Ev_key, when the key presses the value to 1, the release value is 0, and if the type code of the event is Ev_rel,value, the positive and negative values represent two different directions respectively.
3. Related Code
Key_simulator.c
#include
#include
#include
#include
#include
int main ()
{
int keys_fd;
Char ret[2];
struct Input_event t;
KEYS_FD = open ("/dev/input/event2", o_rdonly);
if (keys_fd <= 0)
{
printf ("Open/dev/input/event2 device error!\n");
return 0;
}
while (1)
{
if (read (KEYS_FD, &t, sizeof (t)) = = sizeof (t))
{
if (T.type = = Ev_key)
if (T.value = = 0 | | t.value = = 1)
{
printf ("Key%d%s\n", T.code,
(T.value)? "Pressed": "released");
if (T.CODE==KEY_ESC)
Break
}
}
}
Close (KEYS_FD);
return 0;
}
4, attention to problems
1) Different types of computers, device corresponding event information will be different
You can use cat-acquired devices to match event information:
# cat/proc/bus/input/devices
i:bus=0019 vendor=0000 product=0001 version=0000
N:name= "Power Button"
P:phys=lnxpwrbn/button/input0
S:sysfs=/devices/lnxsystm:00/lnxpwrbn:00/input/input0
u:uniq=
H:HANDLERS=KBD event0
B:ev=3
b:key=100000 0 0 0
i:bus=0017 vendor=0001 product=0001 version=0100
N:name= "Macintosh mouse button emulation"
p:phys=
S:sysfs=/devices/virtual/input/input1
u:uniq=
H:handlers=mouse0 event1
B:ev=7
b:key=70000 0 0 0 0 0 0 0 0
B:rel=3
i:bus=0011 vendor=0001 product=0001 version=ab41
N:name= "at translated Set 2 keyboard"
P:phys=isa0060/serio0/input0
S:sysfs=/devices/platform/i8042/serio0/input/input2
u:uniq=
H:HANDLERS=KBD Event2
b:ev=120013
b:key=4 2000000 3803078 f800d001 feffffdf ffefffff ffffffff Fffffffe
b:msc=10
B:led=7
i:bus=0011 vendor=0002 product=0005 version=0000
N:name= "IMPS/2 Generic Wheel Mouse"
P:phys=isa0060/serio1/input0
S:sysfs=/devices/platform/i8042/serio1/input/input3
u:uniq=
H:handlers=mouse1 Event3
B:ev=7
b:key=70000 0 0 0 0 0 0 0 0
b:rel=103
I Line: This row contains identity information, which shows that the bus type is 3 (USB), vendor, product, version, and other information.
N Line: This row contains the name information.
P Line: This row contains information about the physical device.
H Line: This row contains the handler drivers associated with the device.
Line B: These lines contain some bit fields (Bitfield) that display the capabilities of the device.
The type of event my keyboard corresponds to is Event2
2) Open/dev/input/event2 Device error
Need to use sudo or change the properties of the device
As follows:
sudo./key_simulator
Or
chmod 777/dev/input/event2
This article welcome reprint, reprint please indicate the author and source
Meteor
Source: Http://blog.sina.com.cn/staratsky
Get button response event "Go" under Linux