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

相关推荐
小白同学_C6 小时前
Lab4-Lab: traps && MIT6.1810操作系统工程【持续更新】 _
linux·c/c++·操作系统os
今天只学一颗糖6 小时前
1、《深入理解计算机系统》--计算机系统介绍
linux·笔记·学习·系统架构
不做无法实现的梦~7 小时前
ros2实现路径规划---nav2部分
linux·stm32·嵌入式硬件·机器人·自动驾驶
默|笙9 小时前
【Linux】fd_重定向本质
linux·运维·服务器
陈苏同学10 小时前
[已解决] Solving environment: failed with repodata from current_repodata.json (python其实已经被AutoDL装好了!)
linux·python·conda
“αβ”10 小时前
网络层协议 -- ICMP协议
linux·服务器·网络·网络协议·icmp·traceroute·ping
不爱学习的老登11 小时前
Windows客户端与Linux服务器配置ssh无密码登录
linux·服务器·windows
小王C语言12 小时前
进程状态和进程优先级
linux·运维·服务器
xlp666hub12 小时前
【字符设备驱动】:从基础到实战(下)
linux·面试
弹幕教练宇宙起源13 小时前
cmake文件介绍及用法
android·linux·c++