Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the actual character typed? #214

Open
ackvf opened this issue Jan 25, 2020 · 8 comments
Open

How to get the actual character typed? #214

ackvf opened this issue Jan 25, 2020 · 8 comments
Assignees
Labels
investigating Currently trying to investigate and reproduce issue
Milestone

Comments

@ackvf
Copy link

ackvf commented Jan 25, 2020

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
Copy link

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

@ackvf
Copy link
Author

ackvf commented Feb 17, 2020

@Richienb keychar is not defined on the object though.

@Richienb
Copy link

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

@ackvf
Copy link
Author

ackvf commented Feb 18, 2020

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

@Richienb
Copy link

@ackvf Try this code:

const iohook = require("iohook")

iohook.start()

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

@ackvf
Copy link
Author

ackvf commented Feb 24, 2020

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
Copy link

MrMaxime commented May 6, 2021

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

@ash0x0 ash0x0 self-assigned this Jun 11, 2021
@ash0x0 ash0x0 added the investigating Currently trying to investigate and reproduce issue label Jun 11, 2021
@ash0x0 ash0x0 added this to the Todo milestone Jun 15, 2021
@giladdarshan
Copy link

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigating Currently trying to investigate and reproduce issue
Projects
None yet
Development

No branches or pull requests

5 participants