View Full Version : [Linux] Generare Eventi Tastiera
phoenixbf
12-02-2007, 22:33
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...
ilsensine
13-02-2007, 08:54
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.
phoenixbf
13-02-2007, 15:45
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.
ok, uinput ce l'ho...
Potresti farmi un esempio? in codice C?
anche un esempio stupido.
ilsensine
13-02-2007, 16:21
#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);
}
phoenixbf
13-02-2007, 16:30
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...
ilsensine
13-02-2007, 16:33
L'ho scritto nel codice...linux/input.h
ilsensine
13-02-2007, 16:57
ad esempio per combinazioni?
tipo per generare un CTRL + KEY_A...
Guarda l'esempio sulla "A"; prima generi gli eventi di pressione per ctrl e key_a, poi quelli di rilascio per key_a e ctrl.
phoenixbf
13-02-2007, 16:58
Guarda l'esempio sulla "A"; prima generi gli eventi di pressione per ctrl e key_a, poi quelli di rilascio per key_a e ctrl.
tutto perfettamente chiaro, grazie ancora ;)
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.