Linux查看键鼠输入

文章目录

本文简单介绍几种在linux下查看键鼠输入的方法

通过打开input设备来读取输入

程序如下,使用时需要看情况修改input设备路径

c 复制代码
#include <fcntl.h>
#include <linux/input.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define DEV_PATH "/dev/input/event1" // difference is possible
int main()
{
    int keys_fd;
    char ret[2];
    struct input_event t;
    keys_fd = open(DEV_PATH, O_RDONLY);
    if (keys_fd <= 0) {
        printf("open /dev/input/event1 device error!\n");
        return -1;
    }
    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;
}

通过第三方程序获取

xev

xinput

evtest

参考

Linux捕捉鼠标事件和键盘事件的方法

相关推荐
Hello World . .8 分钟前
Linux:Linux命令行音视频播放器
linux·音视频
YYYing.16 分钟前
【Linux/C++网络篇(二) 】TCP并发服务器演进史:从多进程到Epoll的进化指南
linux·服务器·网络·c++·tcp/ip
SPC的存折24 分钟前
10、Ansible 生产级故障排查与运维最佳实践
linux·运维·ansible
aP8PfmxS234 分钟前
Lab3-page tables && MIT6.1810操作系统工程【持续更新】
java·linux·jvm
林姜泽樾38 分钟前
linux入门第十八章,IP、主机名、域名解析
linux·服务器·tcp/ip
深念Y38 分钟前
从CH341A编程器、SPI Flash到Linux+STM32理解
linux·stm32·flash·bios·固件·编程器·闪存
RisunJan38 分钟前
Linux命令-ncftp(增强的的FTP工具)
linux·运维
Shingmc33 小时前
【Linux】线程互斥与同步
linux
Vect__9 小时前
深刻理解进程、线程、程序
linux
末日汐11 小时前
传输层协议UDP
linux·网络·udp