2015年8月3日 星期一

偵測 keyboard event

偵測 keyboard event

linux/input.h

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/input.h>

int main ()  
{  
  int keys_fd;  
  char ret[2];  
  struct input_event t;  
  
  keys_fd = open ("/dev/input/event15", O_RDONLY);  
  if (keys_fd <= 0)  
    {   
      printf ("open /dev/input/event15 device error!\n");  
      return 0;  
    }   
  
  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;  
}
結果: (記得sudo or su)

key 28 Released
key 32 Pressed
key 32 Released

ref:流星的博客

沒有留言:

張貼留言