python - How to get diff time from kernel input keyboard events(from key pressed to release)? -



python - How to get diff time from kernel input keyboard events(from key pressed to release)? -

i'm trying write python code capture events /dev/input/event* on linux. events want filter event type, event value, event code , time(tv_sec , tv_usec).

problem: eventtype=ev_key , event_code = 0,1,2 (where 0=key_release,1=key_pressed,2=key_repeat), want difftime key_pressed(code 0) , key_released(code 1) (time_pressed - time_released) if key repeated (event code 2).

any thought ?

as starting point, based on a solution treviƱo, here quick , (mostly) dirty way capture keyboard events , study timings:

import struct format = 'llhhi' event_size = struct.calcsize(format) ev_key = 0x01 key_down = 1 key_auto = 2 key_up = 0 devname = "/dev/input/event0" def dt(sec_a, usec_a, sec_b, usec_b): homecoming (sec_a+usec_a/1000000.) - (sec_b+usec_b/1000000) open(devname, "rb") infile: kdtime = {} while true: event = infile.read(event_size) (tv_sec, tv_usec, typ, code, value) = struct.unpack(format, event) if typ == ev_key: if value == key_down: kdtime[code] = (tv_sec, tv_usec) if value == key_up , code in kdtime: print(code, dt(tv_sec, tv_usec, *kdtime[code])) del kdtime[code] # not strictly required

from documentation/input/input.txt events reported kernel as:

struct input_event { struct timeval time; unsigned short type; unsigned short code; unsigned int value; };

struct timeval is turn defined in bits/time.h as:

struct timeval { __time_t tv_sec; /* seconds. */ __suseconds_t tv_usec; /* microseconds. */ };

so corresponding python struct format event llhhi. 1 time have that, have loop read events type ev_key, remember key downwards time, , calculate key pressed time when key code.

please note cannot assume key event match previous key downwards event (think pressing several keys @ once). maintain track of key code , corresponding key pressed time in dictionary. obviously, have adapt needs. said, starting point.

python struct keyboard-events

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -