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捕捉鼠标事件和键盘事件的方法

相关推荐
chinesegf1 小时前
ubuntu中虚拟环境的简单创建和管理
linux·运维·ubuntu
java_logo1 小时前
2025 年 11 月最新 Docker 镜像源加速列表与使用指南
linux·运维·docker·容器·运维开发·kylin
一碗面4212 小时前
Linux下的网络模型
linux·网络模型
HIT_Weston3 小时前
103、【Ubuntu】【Hugo】搭建私人博客:搜索功能(四)
linux·运维·ubuntu
旖旎夜光3 小时前
Linux(11)(中)
linux·网络
txinyu的博客3 小时前
前置声明与 extern
linux·c++
有泽改之_5 小时前
ssh命令使用
linux·运维·ssh
梁洪飞6 小时前
noc 片上网络
linux·arm开发·嵌入式硬件·arm
颜子鱼8 小时前
Linux驱动-INPUT子系统
linux·c语言·驱动开发
Lueeee.8 小时前
llseek 定位设备驱动实验
linux·驱动开发