linux - Would it be possible to read out physical keyboard strokes in node.js? -
i have node application runs on raspberry pi keeps track of bunch of upnp-players (sonos), able control through physical remote. have couple of airmouses, has small keyboards volume buttons use.
i have tried grip on how read out physical key strokes on linux machine, , come conclusion need read events input device, in case be:
/dev/input/by-id/usb-dell_dell_quietkey_keyboard-event-kbd
how find device , stuff not problem, real issue how interpret data read it.
i know receive c struct, this:
struct input_event { struct timeval time; unsigned short type; unsigned short code; unsigned int value; };
but i'm not sure how go reading node. if run external app triggered pre-defined keystrokes, , invoke http-request against node, second option, python script or native daemon. have looked @ hotkey-daemons, none of them worked.
if of course nice if contain within node somehow.
edit: did testing, , made simple snippet:
var fs = require('fs'); var buffer = new buffer(16); fs.open('/dev/input/by-id/usb-hjt_air_mouse-event-kbd', 'r', function (err, fd) { while (true) { fs.readsync(fd, buffer, 0, 16, null); console.log(buffer) } });
this outputs (for space):
<buffer a4 3e 5b 51 ab cf 03 00 04 00 04 00 2c 00 07 00> <buffer a4 3e 5b 51 c3 cf 03 00 01 00 39 00 01 00 00 00> <buffer a4 3e 5b 51 cb cf 03 00 00 00 00 00 00 00 00 00> <buffer a4 3e 5b 51 ba 40 06 00 04 00 04 00 2c 00 07 00> <buffer a4 3e 5b 51 cd 40 06 00 01 00 39 00 00 00 00 00> <buffer a4 3e 5b 51 d2 40 06 00 00 00 00 00 00 00 00 00>
i realize first 4 bytes sort of timestamp, , following 3 bytes micro/millisecond thing.
another odd thing not keypresses produces output, subsequent press might sent twice data, , of time starts blasting out data stop after subsequent keypresses (or after 20 seconds or so). i'm not sure how interpret that. have tried read source daemon https://github.com/baskerville/shkd/blob/master c not strongest language , can't identify how handles (or if should handled). , daemon didn't work me (compiled on raspberry pi).
well, let's have @ struct.
struct input_event { struct timeval time; unsigned short type; unsigned short code; unsigned int value; };
a struct timeval
has structure:
struct timeval { __time_t tv_sec; /* seconds. */ __suseconds_t tv_usec; /* microseconds. */ };
the definition of time types are
typedef signed long time_t; typedef signed long suseconds_t;
a signed long
4 bytes (well, not if follow spec, in practice, is), first 8 bytes typestamp. next, have type , code. both short
, in practice, they're 2 bytes each. there's value left, , that's int again, 4 bytes. also, compiler theoretically add padding between fields here, i'm pretty sure won't.
so, first chop bytes you've read chunks of 4+4+2+2+4=16
bytes. each of chunks event. fits sample data. next, extract values buffer (as little endian values because you're on arm system – on normal pc, you'd need big endian) , interpret values. instructions on how that, read http://www.mjmwired.net/kernel/documentation/input/event-codes.txt. values of constants aren't written down there, can find using grep -r name_of_constant /usr/include
.
let's chop up
<buffer a4 3e 5b 51 ab cf 03 00 04 00 04 00 2c 00 07 00>
as example.
<buffer a4 3e 5b 51 ab cf 03 00 04 00 04 00 2c 00 07 00> | tv_sec | tv_usec |type |code | value |
tv_sec
in hex 0x515b3ea4
(reversed order because it's little endian), 1364934308
in decimal. simple unix time converter reports means 02.04.2013 - 22:25:08
. looks good!
tv_usec
0x0003cfab=249771
, actually, event happened 249771
microseconds after time.
type 0x0004=4
. /usr/include/linux/input.h
tells ev_msc
.
given type, can see the code, 0x0004=4
, means msc_scan
.
the value 0x0007002c
. turns in input.h
. hmm.
Comments
Post a Comment