The linux system gets the mouse press and lift event
1. Principle
Read the device file of the Linux input device, and determine whether the current mouse is pressed or raised by analyzing the content of the device file.
2. Implementation method/step
1. Determine the device file corresponding to the mouse
The device files of the mouse are generally stored in /dev/input/eventX, which "event" is, different PCs are different, as shown in the figure: There are 5 event device files in my PC
You can use the cat command to test, enter in the command line window: cat /dev/input/event2.
After the command is executed, if there is data output on the screen when the mouse is pressed or moved, it proves that the mouse uses this device file. Otherwise, try another one until the correct device file is found.
2. Open the device file
int keys_fd;
keys_fd = open ("/dev/input/event2", O_RDONLY);
3. Read the device file
struct input_event t;
read (keys_fd, &t, sizeof(t))
The data is stored in the structure variable t
4. Parse the device file
First, determine whether the device is a mouse according to the type and code of the device file, and then determine whether to press down or lift up according to the value.
5. Close the device file
close (keys_fd);
Three, linux input_event structure description
The input_event structure is defined in linux/input.h, and the structure is defined as follows:
struct input_event {
struct timeval time; /* Key time*/
__u16 type; /* Type, defined below*/
__u16 code; /* Key name*/
__s32 value; /* Whether to press or lift*/
};
struct timeval
{
__time_t tv_sec; /* Seconds */
__suseconds_t tv_usec; /* Micro seconds */
};
Detailed description of members:
Time: Key time
tv_sec is the number of seconds from the first year of the computer to when the struct timeval is created, and tv_usec is the number of microseconds, that is, the fraction after the second. For example, the current tv_sec is 1596095000 and tv_usec is 128888, that is, the current time from the computer in the first year is 1596095000 seconds, 128888 microseconds.
type: event type
EV_KEY, keyboard
EV_REL, relative coordinate
EV_ABS, absolute coordinate
code: the code of the event
If the type code of the event is EV_KEY, the code code is the device keyboard code. Code values 0~127 are the keys on the keyboard
Key codes, 0x110~0x116 are the codes for the buttons on the mouse, among which 0x110 (BTN_LEFT) is the left mouse button, 0x111 (BTN_RIGHT) is the right mouse button, and 0x112 (BTN_MIDDLE) is the middle mouse button. For the meaning of other codes, please refer to include/linux/ input.h.
value: the value of the event
If the type code of the event is EV_KEY, the value is 1 when the key is pressed, and 0 when the key is released.
tips:
1970.1.1 is a special day for magic horses? It turned out that 1970.1.1 was regarded as the first year of the computer, the earliest UNIX
The operating system takes into account the age of the computer and the time limit of the application, taking January 1, 1970 as the UNIX TIME
The epoch time (start time) of Java, databases, and many sophisticated instruments also naturally follow this constraint.
Fourth, the reference code for function realization
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>
void mouseDownUpEvent()
{
int keys_fd;
struct input_event t;
int mm = 0;
/* Open the device file, the file path is /dev/input/event2 */
keys_fd = open ("/dev/input/event2", O_RDONLY);
if (keys_fd < 0)
{
qDebug ("open /dev/input/event3 device error!\n");
}
while(1)
{
if(mm < 8)
{
if (read (keys_fd, &t, sizeof(t)))
{
/* Press and lift the left mouse button*/
/* EV_KEY means mouse or keyboard device, code 0x110 means left mouse button*/
if ((EV_KEY == t.type)&&(0x110 == t.code))
{
mm++;
qDebug()<<" t.value:"<< t.value;
qDebug()<<" t.code:"<< t.code;
/* The value of value is 1 means pressing, 0 means lifting*/
if (0 == t.value)
{
qDebug()<<"left Released";
}else if(1 == t.value){
qDebug()<<"left Pressed";
}
}
/* Right mouse button press and lift operation*/
/* code is 0x111 means the right mouse button*/
if ((EV_KEY == t.type)&&(0x111 == t.code))
{
mm++;
qDebug()<<" t.value:"<< t.value;
qDebug()<<" t.code:"<< t.code;
if (0 == t.value)
{
qDebug()<<"right Released";
}else if(1 == t.value){
qDebug()<<"right Pressed";
}
}
}
}else
{
break;
}
}
close (keys_fd);
}