A maneira mais direta seria a leitura de dispositivos de evento expostos em /dev/input/
. Leia o o documento do kernel no subsistema de entrada do Linux, a partir do qual você pode aprender como manipular esses dispositivos em C.
Existem vários pacotes para você testar rapidamente seus dispositivos de entrada. Por exemplo, você pode usar as seguintes ferramentas depois de apt-get install input-utils
no Ubuntu.
lsinput
: enumerar os dispositivos de entrada conectados ao sistema.
$ sudo lsinput
/dev/input/event0
bustype : BUS_HOST
vendor : 0x0
product : 0x3
version : 0
name : "Sleep Button"
phys : "PNP0C0E/button/input0"
bits ev : EV_SYN EV_KEY
/dev/input/event1
bustype : BUS_HOST
vendor : 0x0
product : 0x5
version : 0
name : "Lid Switch"
phys : "PNP0C0D/button/input0"
bits ev : EV_SYN EV_SW
/dev/input/event2
bustype : BUS_HOST
vendor : 0x0
product : 0x1
version : 0
name : "Power Button"
phys : "LNXPWRBN/button/input0"
bits ev : EV_SYN EV_KEY
/dev/input/event3
bustype : BUS_I8042
vendor : 0x1
product : 0x1
version : 43841
name : "AT Translated Set 2 keyboard"
phys : "isa0060/serio0/input0"
bits ev : EV_SYN EV_KEY EV_MSC EV_LED EV_REP
Aqui, /dev/input/event3
é um dispositivo de teclado que você pode querer assistir.
input-events
: leia e mostre eventos do dispositivo de entrada. É preciso um argumento de número de dispositivo, digamos, 3
para /dev/input/event3
.
$ sudo input-events 3
/dev/input/event3
bustype : BUS_I8042
vendor : 0x1
product : 0x1
version : 43841
name : "AT Translated Set 2 keyboard"
phys : "isa0060/serio0/input0"
bits ev : EV_SYN EV_KEY EV_MSC EV_LED EV_REP
waiting for events
09:27:02.961576: EV_MSC MSC_SCAN 50
09:27:02.961576: EV_KEY KEY_M (0x32) released
09:27:02.961576: EV_SYN code=0 value=0
09:27:03.833925: EV_MSC MSC_SCAN 58
09:27:03.833925: EV_KEY KEY_CAPSLOCK (0x3a) released
09:27:03.833925: EV_SYN code=0 value=0
09:27:09.344765: EV_MSC MSC_SCAN 30
09:27:09.344765: EV_KEY KEY_A (0x1e) pressed
09:27:09.344765: EV_SYN code=0 value=0
09:27:09.446881: EV_MSC MSC_SCAN 30
09:27:09.446881: EV_KEY KEY_A (0x1e) released
09:27:09.446881: EV_SYN code=0 value=0
09:27:09.548389: EV_MSC MSC_SCAN 48
09:27:09.548389: EV_KEY KEY_B (0x30) pressed
09:27:09.548389: EV_SYN code=0 value=0
09:27:09.640385: EV_MSC MSC_SCAN 48
09:27:09.640385: EV_KEY KEY_B (0x30) released
09:27:09.640385: EV_SYN code=0 value=0
09:27:09.781365: EV_MSC MSC_SCAN 46
09:27:09.781365: EV_KEY KEY_C (0x2e) pressed
09:27:09.781365: EV_SYN code=0 value=0
09:27:09.853143: EV_MSC MSC_SCAN 46
09:27:09.853143: EV_KEY KEY_C (0x2e) released
09:27:09.853143: EV_SYN code=0 value=0
...