iohook icon indicating copy to clipboard operation
iohook copied to clipboard

How to get the actual character typed?

Open ackvf opened this issue 5 years ago • 8 comments
trafficstars

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

ackvf avatar Jan 25 '20 18:01 ackvf

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

Richienb avatar Feb 14 '20 03:02 Richienb

@Richienb keychar is not defined on the object though.

ackvf avatar Feb 17 '20 14:02 ackvf

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

Richienb avatar Feb 17 '20 22:02 Richienb

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

ackvf avatar Feb 18 '20 16:02 ackvf

@ackvf Try this code:

const iohook = require("iohook")

iohook.start()

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

Richienb avatar Feb 21 '20 03:02 Richienb

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?

ackvf avatar Feb 24 '20 16:02 ackvf

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

MrMaxime avatar May 06 '21 07:05 MrMaxime

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;

giladdarshan avatar Jul 01 '21 22:07 giladdarshan