当前位置:   article > 正文

[Linux] C获取键盘,鼠标数据_linux c++获取键盘

linux c++获取键盘

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

鼠标检测指令:cat /dev/input/event2 | hexdump

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

struct input_event 
{
      struct timeval time;
      __u16 type;
      __u16 code;
      __s32 value;
};
  1. //==================获取键盘数据====================
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <linux/input.h>
  7. #include <string.h>
  8. int main(void)
  9. {
  10. #define KEY_PATH "/dev/input/event1"
  11. int fd = -1, ret = -1;
  12. struct input_event ev;
  13. // 第1步:打开设备文件
  14. fd = open(KEY_PATH, O_RDONLY);
  15. if (fd < 0)
  16. {
  17. perror("open,error");
  18. return -1;
  19. }
  20. printf("welcome size=%d.\n",sizeof(struct input_event));
  21. while (1)
  22. {
  23. // 第2步:读取event事件包
  24. memset(&ev, 0, sizeof(struct input_event));
  25. ret = read(fd, &ev, sizeof(struct input_event));
  26. if (ret != sizeof(struct input_event))
  27. {
  28. perror("read,error");
  29. close(fd);
  30. return -1;
  31. }
  32. // 第3步:解析event包.
  33. printf("========================================================\n");
  34. printf("[%11u] type: %3d, code: %3d, value: %3d \n",ev.time.tv_sec,ev.type,ev.code,ev.value);
  35. //type: 1:按键同步
  36. //code: 键码['a'=30]
  37. //value:0:按键释放,1:按键按下,2:长按下
  38. }
  39. // 第4步:关闭设备
  40. close(fd);
  41. return 0;
  42. }

  1. //=======获取鼠标数据=========
  2. #include <X11/Xlib.h>
  3. //LDFLAGS := -lX11
  4. int GetDisplayInfo(int *screen_width,int *screen_height)
  5. {
  6. Display *display = XOpenDisplay(NULL);
  7. if (display == NULL)
  8. {
  9. printf("Error: cannot open display\n");
  10. return 1;
  11. }
  12. int screen_num = DefaultScreen(display);
  13. Screen *screen = ScreenOfDisplay(display, screen_num);
  14. *screen_width = WidthOfScreen(screen);
  15. *screen_height = HeightOfScreen(screen);
  16. printf("Screen size: %dx%d pixels\n", WidthOfScreen(screen), HeightOfScreen(screen));
  17. printf("Screen resolution: %dx%d dpi\n", (int) (WidthOfScreen(screen) * 25.4 / DisplayWidthMM(display, screen_num)),
  18. (int) (HeightOfScreen(screen) * 25.4 / DisplayHeightMM(display, screen_num)));
  19. XCloseDisplay(display);
  20. return 0;
  21. }
  22. int get_xy(int fd,struct input_event ts,int *x,int *y)
  23. {
  24. static int nCnt = 0;
  25. read(fd,&ts,sizeof(ts));
  26. if(ts.type == EV_ABS && ts.code == ABS_X)
  27. {
  28. *x = ts.value;
  29. nCnt = (nCnt+1)%3;
  30. return nCnt;
  31. }
  32. if(ts.type == EV_ABS && ts.code == ABS_Y)
  33. {
  34. *y = ts.value;
  35. nCnt = (nCnt+1)%3;
  36. return nCnt;
  37. }
  38. return 0;
  39. }
  40. int main(void)
  41. {
  42. #define MOUSE_PATH "/dev/input/event2"
  43. int fd = -1, ret = -1;
  44. struct input_event ev;
  45. int data_size = sizeof(struct input_event);
  46. // 第1步:打开设备文件[需要权限运行]
  47. fd = open(MOUSE_PATH, O_RDONLY);
  48. if (fd < 0)
  49. {
  50. perror("open,error");
  51. return -1;
  52. }
  53. printf("mouse test [%s],data size=%d.\n",MOUSE_PATH,sizeof(struct input_event));
  54. int screen_width = 0;
  55. int screen_height = 0;
  56. if( GetDisplayInfo(&screen_width,&screen_height)>0)
  57. {
  58. perror("get display info,error");
  59. return -2;
  60. }
  61. while (1)
  62. {
  63. static int raw_x=0;
  64. static int raw_y=0;
  65. int tmp =0;
  66. tmp = get_xy(fd,ev,&raw_x,&raw_y);
  67. if(tmp==2)
  68. {
  69. int curr_x = 0;
  70. int curr_y = 0;
  71. curr_x = raw_x*screen_width/0xFFFF;
  72. curr_y = raw_y*screen_height/0xFFFF;
  73. printf("mousePos: x=%d,y=%d.\n",curr_x,curr_y);
  74. }
  75. }
  76. close(fd);
  77. return 0;
  78. }

方法2:采用SDL2 [simplle directmedia layer]  , 此方法用于GUI项目,事件只针对SDL创建的窗口内有效

  1. #include <stdio.h>
  2. #include <SDL2/SDL.h>
  3. #define SDL_HOR_RES (800)
  4. #define SDL_VER_RES (600)
  5. typedef struct TagMonitor
  6. {
  7. int screen_w;
  8. int screen_h;
  9. SDL_Renderer *render;
  10. SDL_Texture *texture;
  11. } tagMonitor;
  12. // 将屏幕设置成指定ARGB颜色
  13. int update_win(tagMonitor *mt, uint32_t argb)
  14. {
  15. uint32_t fb_data[SDL_HOR_RES * SDL_VER_RES]; // frame buffer data[w*h]
  16. for (size_t i = 0; i < SDL_HOR_RES * SDL_VER_RES; i++)
  17. {
  18. fb_data[i] = argb;
  19. }
  20. SDL_UpdateTexture(mt->texture, NULL, fb_data, mt->screen_w * 4);
  21. SDL_RenderClear(mt->render);
  22. // 设定渲染的目标区域
  23. SDL_Rect destRect;
  24. destRect.x = 0;
  25. destRect.y = 0;
  26. destRect.w = mt->screen_w;
  27. destRect.h = mt->screen_h;
  28. // 复制材质到渲染器对象
  29. if (SDL_RenderCopy(mt->render, mt->texture, NULL, &destRect))
  30. {
  31. printf("Error,%s \n", SDL_GetError());
  32. return -1;
  33. }
  34. // 执行渲染操作
  35. SDL_RenderPresent(mt->render);
  36. return 0;
  37. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/266431?site
推荐阅读
相关标签
  

闽ICP备14008679号