I have a node application that runs on raspberries pi that tracks a bunch of UPnP players (Sonos) that I would like to control using a physical remote. I have a pair of air muscles that have small keyboards, as well as volume buttons that I would like to use.
I tried to understand how to read physical strokes on a Linux machine, and come to the conclusion that I need to read events from an input device, which in my case would be:
/dev/input/by-id/usb-Dell_Dell_QuietKey_Keyboard-event-kbd
Finding a device and stuff like that is not a problem; the real problem is how to interpret the data you are reading.
I know that you will get a C structure, for example:
struct input_event { struct timeval time; unsigned short type; unsigned short code; unsigned int value; };
But I'm not sure how I will read this with node. If I could run an external application that would start from predefined keystrokes and then call an HTTP request against my node, this would be my second option: a python script or some kind of native daemon. However, I looked at some hot daemon keys, but none of them worked.
If, of course, it would be nice if I could somehow contain it inside the node.
EDIT: So, I did some tests and made a 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 something like this (for a 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 understand that the first four bytes are a kind of timestamp, and the next 3 bytes can be something like micro / milliseconds.
Another feature is that not all keystrokes display output, but subsequent keystrokes can send twice as much data, and most of the time it starts dumping data that stops after subsequent keystrokes (or after about 20 seconds or so ), I'm not sure how to interpret this. I tried to read the source for this daemon https://github.com/baskerville/shkd/blob/master , but C is not the strongest language, and I cannot determine how it processes it (or if it even needs to be processed). And this demon did not even work for me (compiled it on a raspberry pie).