当前位置: 首页 > news >正文

[Linux] C获取键盘输入值

检测指令:cat /dev/input/event1 | hexdump

当键盘有输入时,会有对应的一堆16进制输出。它其实对应着input_event结构体【24字节】。

struct input_event 
​​​​​​​{struct timeval time;__u16 type;__u16 code;__s32 value;
};


#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <string.h>int main(void)
{#define KEY_PATH	"/dev/input/event1"int fd = -1, ret = -1;struct input_event ev;// 第1步:打开设备文件fd = open(KEY_PATH, O_RDONLY);if (fd < 0){perror("open,error");return -1;}printf("welcome size=%d.\n",sizeof(struct input_event));while (1){// 第2步:读取event事件包memset(&ev, 0, sizeof(struct input_event));ret = read(fd, &ev, sizeof(struct input_event));if (ret != sizeof(struct input_event)){perror("read,error");close(fd);return -1;}// 第3步:解析event包.printf("========================================================\n");printf("[%11u] type: %3d, code: %3d, value: %3d \n",ev.time.tv_sec,ev.type,ev.code,ev.value);//type: 1:按键同步//code: 键码['a'=30]//value:0:按键释放,1:按键按下,2:长按下}// 第4步:关闭设备close(fd);	return 0;
}

http://www.lryc.cn/news/132181.html

相关文章:

  • 探索Python编程世界:开启你的代码之旅
  • 金融术语总结
  • Linux驱动开发(Day5)
  • [机器学习]特征工程:主成分分析
  • Python爬虫实战案例——第一例
  • 一、openlayer开发介绍
  • 利用Jackson封装常用的JsonUtil工具类
  • 阿里云2核4G服务器配置汇总表_轻量和ECS
  • 攻防世界-ics-06
  • 人工智能轨道交通行业周刊-第56期(2023.8.14-8.20)
  • ruoyi-vue-pro yudao 项目报表设计器 积木报表模块启用及相关SQL脚本
  • 【第三阶段】kotlin中使用带let的安全调用
  • JavaScript 快速入门手册
  • FreeMarker系列--list的用法(长度,遍历,下标,嵌套,排序)
  • 【观察】戴尔科技:构建企业创新“韧性”,开辟数实融合新格局
  • 数据管理平台
  • 手搓大语言模型 使用jieba分词制作词表,词表大小几十万 加强依赖性
  • 【校招VIP】java语言类和对象之map、set集合
  • windows服务器下java程序健康检测及假死崩溃后自动重启应用、开机自动启动
  • 七夕特辑(一)浪漫表白方式 用神经网络生成一首情诗
  • springboot的 spring.redis.lettuce的max-active、max-idle、min-idle的搭配
  • 盒子模型样式
  • 动态规划入门之线性动态规划
  • 基于HTML+CSS+Echarts大屏数据可视化集合共99套
  • Leetcode 0814周总结
  • 华为网络篇 OSPF的Silent-Interface-33
  • longtext,bigint是什么数据类型
  • Hive无法启动的解决方案
  • 华为云零代码新手教学-体验通过Astro Zero快速搭建微信小程序
  • 【前端】快速掌握HTML+CSS核心知识点