Would It Be Possible to Read Out Physical Keyboard Strokes in Node.Js

Would it be possible to read out physical keyboard strokes in node.js?

Well, let's have a look at that struct.

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

A struct timeval has this structure:

struct timeval
{
__time_t tv_sec; /* Seconds. */
__suseconds_t tv_usec; /* Microseconds. */
};

The definition of those time types are

typedef signed long time_t;
typedef signed long suseconds_t;

A signed long is 4 bytes (well, not if you just follow the spec, but in practice, it is), so the first 8 bytes are a typestamp. Next, you have a type and a code. Both are short, so in practice, they're 2 bytes each. Now there's just the value left, and that's an int again, which will be four bytes. Also, a compiler could theoretically add padding between the fields here, but I'm pretty sure he won't.

So, first chop the bytes you've read into chunks of 4+4+2+2+4=16 bytes. Each of those chunks is an event. This fits your sample data. Next, extract the values from the buffer (as little endian values because you're on an ARM system – on a normal PC, you'd need big endian) and interpret the values. For instructions on how to do that, read http://www.mjmwired.net/kernel/Documentation/input/event-codes.txt. The values of the constants aren't written down there, but you can usually find those 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 an 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 is 0x515b3ea4 (reversed order because it's little endian), which is 1364934308 in decimal. A simple unix time converter reports that this means 02.04.2013 - 22:25:08. Looks good!

tv_usec is 0x0003cfab=249771, so actually, the event happened 249771 microseconds after that time.

Type is 0x0004=4. /usr/include/linux/input.h tells us that this is a EV_MSC.

Given the type, we can also see the the code, 0x0004=4, means MSC_SCAN.

The value is 0x0007002c. Turns up nowhere in input.h. Hmm.

detect keyboard layout with javascript

Keyboard layouts do not have standardized identifiers. They have names assigned to them by the layout creator. They may have a language association as one defined property.

The Keyboard layouts should not be confused with language or locale identifiers. In particular there is no single “English layout” but dozens of layouts that may be used for English.

I don’t think systems normally make their current layout settings readable via JavaScript.

So whatever problem you are trying solve by detecting the keyboard layout, a different approach is needed.

How to find out what character key is pressed?

"Clear" JavaScript:

function myKeyPress(e){
var keynum;

if(window.event) { // IE
keynum = e.keyCode;
} else if(e.which){ // Netscape/Firefox/Opera
keynum = e.which;
}

alert(String.fromCharCode(keynum));
}
<input type="text" onkeypress="return myKeyPress(event)" />


Related Topics



Leave a reply



Submit