|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Apr 2006
Messaggi: 3461
|
[Linux] Generare Eventi Tastiera
ciao a tutti,
In C (sotto linux) vorrei sapere un metodo semplice per generare eventi tastiera, a parte le lettere, deve funzionare anche con i vari CTRL, ALT, ecc...
__________________
Alienware M17xR3 // Intel Core i7 Processor 2670QM (2.20Ghz, 6MB, 4C); LCD 17.3in 120Hz w/ 3D Bundle WideFHD (1920 x 1080) WLED; RAM 8 Gb 1333MHz DDR3 Dual Channel; 1,5GB GDDR5 NVIDIA GeForce GTX 560M |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Puoi usare il driver uinput e scrivere un programma utente che "inietta" nel layer input del kernel gli eventi che desideri. Puoi generare qualsiasi tipo di evento tastiera, mouse, touchpad ecc.
L'ho utilizzato in passato per scrivere un driver tastiera in userspace, per un dispositivo particolare.
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 |
![]() |
![]() |
![]() |
#3 | |
Senior Member
Iscritto dal: Apr 2006
Messaggi: 3461
|
Quote:
Potresti farmi un esempio? in codice C? anche un esempio stupido.
__________________
Alienware M17xR3 // Intel Core i7 Processor 2670QM (2.20Ghz, 6MB, 4C); LCD 17.3in 120Hz w/ 3D Bundle WideFHD (1920 x 1080) WLED; RAM 8 Gb 1333MHz DDR3 Dual Channel; 1,5GB GDDR5 NVIDIA GeForce GTX 560M |
|
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Codice:
#include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #include <linux/input.h> #include <linux/uinput.h> static void inputEvent(int fd, unsigned short key, int value); static int uinput_init(const char *devname) { struct uinput_user_dev udev; int fd = open(devname, O_RDWR); if (fd<0) return -1; // intendiamo generare eventi EV_KEY, come una tastiera. // Il kernel utilizza questa informazione per decidere a quali "slave" // reindirizzare i nostri dati. ioctl(fd, UI_SET_EVBIT, EV_KEY); // Bisogna registrare ciascuna key che possiamo generare. // V. linux/input.h per le key disponibili. // NB Puoi _solo_ generare le key che dichiari qui. Probabilmente vorrai // utilizzare un array per aiutarti nell'inserimento, se devi emulare molti tasti. // // Come esempio, registriamo i tasti shift sinistro e a. ioctl(fd, UI_SET_KEYBIT, KEY_LEFTSHIFT); ioctl(fd, UI_SET_KEYBIT, KEY_A); memset(&udev, 0, sizeof(udev)); // Un nome per il sysfs... strncpy(udev.name, "my keybd", sizeof(udev.name)); // nb questa write e' obbligatoria, prima di UI_DEV_CREATE write(fd, &udev, sizeof(udev)); // Rendiamo operativo il device if (ioctl(fd, UI_DEV_CREATE)==0) return fd; close(fd); return -1; } int main() { int fd = uinput_init("/dev/input/uinput"); if (fd<0) { perror("uinput"); return -1; } // Un evento e' composto dal key che lo genera, e un valore. Per i tasti, il valore puo' // essere 1 (tasto premuto) o 0 (tasto rilasciato). // Questo ad esempio genera una 'a': inputEvent(fd, KEY_A, 1); inputEvent(fd, KEY_A, 0); // Questo invece genera una 'A': inputEvent(fd, KEY_LEFTSHIFT, 1); inputEvent(fd, KEY_A, 1); inputEvent(fd, KEY_A, 0); inputEvent(fd, KEY_LEFTSHIFT, 0); close(fd); return 0; } static void insertEvent(int fd, unsigned short type, unsigned short code, int value) { struct input_event e; memset(&e, 0, sizeof(e)); e.type = type; e.code = code; e.value = value; write(fd, &e, sizeof(e)); } static void inputEvent(int fd, unsigned short key, int value) { // Ogni evento consiste in due pacchetti input_event: l'evento vero e proprio, e un sync. insertEvent(fd, EV_KEY, key, value); insertEvent(fd, EV_SYN, SYN_REPORT, 0); }
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 Ultima modifica di ilsensine : 13-02-2007 alle 16:23. |
![]() |
![]() |
![]() |
#5 |
Senior Member
Iscritto dal: Apr 2006
Messaggi: 3461
|
grazie mille!!!!
dove posso trovare l'elenco completo degli eventi keyboard? immagino in uno di quegli header... ad esempio per combinazioni? tipo per generare un CTRL + KEY_A...
__________________
Alienware M17xR3 // Intel Core i7 Processor 2670QM (2.20Ghz, 6MB, 4C); LCD 17.3in 120Hz w/ 3D Bundle WideFHD (1920 x 1080) WLED; RAM 8 Gb 1333MHz DDR3 Dual Channel; 1,5GB GDDR5 NVIDIA GeForce GTX 560M Ultima modifica di phoenixbf : 13-02-2007 alle 16:34. |
![]() |
![]() |
![]() |
#6 |
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
L'ho scritto nel codice...linux/input.h
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 |
![]() |
![]() |
![]() |
#7 | |
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Quote:
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 |
|
![]() |
![]() |
![]() |
#8 | |
Senior Member
Iscritto dal: Apr 2006
Messaggi: 3461
|
Quote:
![]()
__________________
Alienware M17xR3 // Intel Core i7 Processor 2670QM (2.20Ghz, 6MB, 4C); LCD 17.3in 120Hz w/ 3D Bundle WideFHD (1920 x 1080) WLED; RAM 8 Gb 1333MHz DDR3 Dual Channel; 1,5GB GDDR5 NVIDIA GeForce GTX 560M |
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 17:58.