wilix-team / iohook

Node.js global keyboard and mouse listener.
https://wilix-team.github.io/iohook
MIT License
1.2k stars 291 forks source link

How to get the actual character typed? #214

Open ackvf opened 4 years ago

ackvf commented 4 years ago

I used String.fromCharCode(key.rawcode) to obtain Q Q and 1 1 even though I really typed q Q and + 1. In other words, I get the same character code for lower and capital form of a letter. Notice how keypress library handles that.

While your solution is far superior with regard to giving same output with different layouts, it is useful to also get the actual character typed.

I pressed q = q shiftq = Q image

I pressed + = + shift+ = 1 (Czech Qwerty layout ) image

Richienb commented 4 years ago

@ackvf Instead of String.fromCharCode(key.rawcode) do String.fromCharCode(key.keychar).

ackvf commented 4 years ago

@Richienb keychar is not defined on the object though.

Richienb commented 4 years ago

@ackvf It works for me. Check the version you have installed.

ackvf commented 4 years ago

I have just updated from 0.6.4 to 0.6.5 and the keychar is still undefined.

Richienb commented 4 years ago

@ackvf Try this code:

const iohook = require("iohook")

iohook.start()

iohook.on("keypress", ({ keychar }) => console.log(`Key pressed: ${String.fromCharCode(keychar)}`))
ackvf commented 4 years ago

So, the problem is that I am using keydown instead of keypress, anyway, observe:

iohook-keypress

  1. focused main window, keypress won't fire at all.
  2. focused another terminal, keypress won't fire at all.
  3. focused notepad, keypress starts firing.

The difference between those two for the key a: image

import iohook from 'iohook'

iohook.start()

iohook.on("keypress", key => console.log(`keypress: ${JSON.stringify(key)}`) )
iohook.on("keydown",  key => console.log(`keydown:  ${JSON.stringify(key)}`) )

Is this a bug?

MrMaxime commented 3 years ago

I have the same behaviour of you, and I think it's a bug because a few week ago, I didn't think I had this problem

giladdarshan commented 3 years ago

I'm able to get the unicode keychar in the keydown event similar to the keypress event by modifying a couple of source files and rebuilding Lower case "a" {"shiftKey":false,"altKey":false,"ctrlKey":false,"metaKey":false,"keychar":97,"keycode":30,"rawcode":0,"type":"keydown"} Upper case "A" with shift key {"shiftKey":true,"altKey":false,"ctrlKey":false,"metaKey":false,"keychar":65,"keycode":30,"rawcode":0,"type":"keydown"}

Replace line 364 in input_hook.c with: UniChar keycharBuffer; keycode_to_unicode(event_ref, &keycharBuffer, KEY_BUFFER_SIZE); event.data.keyboard.keychar = keycharBuffer;

Change line 447 in iohook.cc: From: if (event.type == EVENT_KEY_TYPED) { To: if (event.type == EVENT_KEY_TYPED || event.type == EVENT_KEY_PRESSED) {

I tested only on a Mac, but similar changes should apply for Windows by replacing line 226 in input_hook.c file for Windows with: WCHAR keycharBuffer[2]; keycode_to_unicode(kbhook->vkCode, &keycharBuffer, sizeof(buffer)) event.data.keyboard.keychar = keycharBuffer;