linux下獲取按鍵響應事件

2021-07-13 01:10:52 字數 3674 閱讀 2756

linux下獲取按鍵響應事件

1、問題

通過乙個死迴圈將讀取鍵盤對應的裝置檔案將觸發鍵盤事件在螢幕上列印出來,按esc退出程式

**是在unbuntu10.04編譯執行通過的

2、input_event描述

在linux核心中,input裝置用input_dev結構體描述,使用input子系統實現輸入裝置驅動的時候,驅動的核心工作就是向系統報告按鍵、觸控螢幕、鍵盤、滑鼠等輸入事件(event,通過input_event結構體描述),不再需要關心檔案操作介面,因為input子系統已經完成了檔案操作介面

linux/input.h 這個檔案定義了event事件的結構體,api和標準按鍵的編碼等;

struct input_event ;

type,指事件型別,常見的事件型別有:

ev_key, 按鍵事件,如鍵盤的按鍵(按下哪個鍵),滑鼠的左鍵右鍵(是非擊下)等;

ev_rel, 相對座標,主要是指滑鼠的移動事件(相對位移);

ev_abs, 絕對座標,主要指觸控螢幕的移動事件

code:事件的**.

如果事件的型別**是ev_key,該**code為裝置鍵盤**.0~127為鍵盤上的按鍵**

關於按鍵**的定義,部分如下所示:

#define key_reserved     0

#define key_esc  1

#define key_1    2

#define key_2    3

#define key_3    4

#define key_4    5

#define key_5    6

#define key_6    7

#define key_7    8

#define key_8    9

#define key_9    10

#define key_0    11

#define key_minus    12

#define key_equal    13

#define key_backspace    14

#define key_tab  15

#define key_q    16

#define key_w    17

#define key_e    18

#define key_r    19

#define key_t    20

value:

事件的值.如果事件的型別**是ev_key,當按鍵按下時值為1,鬆開時值為0;如果事件的型別**是ev_rel,value的正數值和負數值分別代表兩個不同方向的值.

3、相關**

key_simulator.c

#include  

#include  

#include  

#include  

#include  

int main ()  

while (1

)    

}  }  

close (keys_fd);  

return

0;  

}  

#include

#include

#include

#include

#include

int main ()

while (1)}}

close (keys_fd);

return 0;

}

4、注意問題

1)不同的型別計算機,裝置對應event資訊會有所不同

可以用cat獲得的裝置對應event資訊:

# cat /proc/bus/input/devices

i: bus=0019 vendor=0000 product=0001 version=0000

n: name="power button"

p: phys=lnxpwrbn/button/input0

s: sysfs=/devices/lnxsystm:00/lnxpwrbn:00/input/input0

u: uniq=

h: handlers=kbd event0

b: ev=3

b: key=100000 0 0 0

i: bus=0017 vendor=0001 product=0001 version=0100

n: name="macintosh mouse button emulation"

p: phys=

s: sysfs=/devices/virtual/input/input1

u: uniq=

h: handlers=mouse0 event1

b: ev=7

b: key=70000 0 0 0 0 0 0 0 0

b: rel=3

i: bus=0011 vendor=0001 product=0001 version=ab41

n: name="at translated set 2 keyboard"

p: phys=isa0060/serio0/input0

s: sysfs=/devices/platform/i8042/serio0/input/input2

u: uniq=

h: handlers=kbd event2

b: ev=120013

b: key=4 2000000 3803078 f800d001 feffffdf ffefffff ffffffff fffffffe

b: msc=10

b: led=7

i: bus=0011 vendor=0002 product=0005 version=0000

n: name="imps/2 generic wheel mouse"

p: phys=isa0060/serio1/input0

s: sysfs=/devices/platform/i8042/serio1/input/input3

u: uniq=

h: handlers=mouse1 event3

b: ev=7

b: key=70000 0 0 0 0 0 0 0 0

b: rel=103

i line:這行包含身份資訊,顯示了 bus type是 3 (usb), vendor, product, version等資訊。

n line:這行包含了名字資訊。

p line:這行包含了物理裝置資訊。

h line: 這行包含了與裝置關聯的 handler drivers。

b line: 這些行包含了顯示裝置能力的一些位域 (bitfield)。

我的鍵盤對應的事件型別是event2

2)open /dev/input/event2 device error

需要使用sudo或者改變裝置的屬性

如下:

sudo ./key_simulator

或者 chmod 777 /dev/input/event2

出處:

獲取滑鼠按鍵事件

獲取滑鼠按鍵事件 方法一 最徹底有效的 window.oncontextmenu function 方法二 有缺陷的,不能少alert window.onmousedown function 說明 event.button 0 沒按鍵 event.button 1 按左鍵 event.button ...

Linux按鍵響應測試

在linux核心中,input裝置用input dev結構體描述,使用input子系統實現輸入裝置驅動的時候,驅動的核心工作就是向系統報告按鍵 觸控螢幕 鍵盤 滑鼠等輸入事件 event,通過input event結構體描述 linux input.h 這個檔案定義了event事件的結構體,api和...

pygame按鍵持續按下響應

在寫乙個pygame 的乙個小的飛機大戰程式但是碰到乙個小問題,僅此記錄 通過鍵盤事件操控飛機的左右移動 elif event.type keydown 檢測按鍵是否是a或者left ifevent.key k a or event.key k left plane temp.move left 雖...