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

KeyTyped event wasn't emitted for @ on German keyboard layout #162

Open
fleischverpackung opened this issue Jun 9, 2023 · 10 comments
Open

Comments

@fleischverpackung
Copy link

Hi,

I encountered a problem with Sharphook, when contacting its creator, he suspected it could be a problem with your (underlying) library. It is related to multi-button presses and special characters. I am sitting a few dependencies down.. so I really don't know what is going on on this level. But I see nobody has reported this yet.

TolikPylypchuk/SharpHook#47

"Hi! Thanks for asking! I've added the German layout and tried running the sample program, and indeed the KeyTyped event wasn't emitted for @. I don't really know why. I looked though the code of libuiohook - it tries to get the Unicode character for the pressed key, and I guess it fails for some reason. I will leave this issue open for now, but I don't know what I can do about it."

@fleischverpackung fleischverpackung changed the title KeyTyped event wasn't emitted for @ KeyTyped event wasn't emitted for @ on German keyboard layout Jun 9, 2023
@kwhat
Copy link
Owner

kwhat commented Jun 9, 2023

I presume this is on Windows?

@fleischverpackung
Copy link
Author

Yes, Windows.

@kwhat
Copy link
Owner

kwhat commented Jun 9, 2023

Windows 10? 11?
32 or 64 bit program?
32 or 64 bit Windows?

This problem comes from the this incredibly ugly bit of code. For whatever reason, this method was unable to determine that the key/modifier combination should have produced an '@'.

This method does some fancy stuff like find the language dll in the registry, loads it up and asks it for a char. If the registry doesn't have the correct dll location you should get an error message and would have likely broken much more than '@'. If you read a little further up, this code was adapted from some code written by Marc-André Moreau. One of the things that was changed was removing the need for _ptr64 in a truly insane and probably buggy dynamic implementation. If there is a fix to your problem this is likely it. IF I can duplicate the issue, I'll take a look and see if this was caused by the _ptr64 stuff as I really only ever use MSVC on Windows these days.

@fleischverpackung
Copy link
Author

fleischverpackung commented Jun 12, 2023 via email

@fleischverpackung
Copy link
Author

fleischverpackung commented Jun 14, 2023 via email

@TolikPylypchuk
Copy link
Contributor

In my fork I've managed to improve getting the characters for the key typed events on Windows by removing the obscure code which gets the characters and using ToUnicodeEx instead. I'm not 100% sure that it works correctly, but I've tested it on my machine with various keyboard layouts, and it seemed to work.

Here's the commit with the relevant changes: fb88601

@fleischverpackung
Copy link
Author

Hi! Thanks a lot for addressing this issue. Should I post again when I can test? Could take some time.

@TolikPylypchuk
Copy link
Contributor

Sure, any bit of feedback is appreciated!

The fix is available in version 5.0.0 of SharpHook BTW.

@kwhat
Copy link
Owner

kwhat commented Aug 11, 2023

I don't believe ToUnicodeEx will work in a number of situations. I seem to remember starting there a long, long time ago and then moving to the code outlined here. The code was modified from using #define KBD_LONG_POINTER __ptr64 to doing a bunch of hacky things in memory to pad the pointer at runtime. I wrote all this code on a flight from Sydney to Los Angeles about 10 years ago and there are probably several bugs in it but that code makes my face melt and I've avoided revisiting it. There is probably a bug report either against this project or JNativeHook that talks about why this solution was added. You can find one of the changes here. It looks like the change was made in 2.2 and back ported. I will have to go dig though the history and see if I can figure it out at some point.

I am still dealing with a bunch of feature dev for 1.3 and Wayland support needs to happen next so I am probably not going to have the time nor desired to look at this for a while. If ToUnicodeEx works for you, please continue to use it and do let me know how to goes.

@TolikPylypchuk
Copy link
Contributor

I've just looked at the change in JNativeHook which you've linked, and I saw this comment:

/* FIXME ToUnicode needs to be replaced because it doesn't work well with dead keys */
if (ToUnicode(kbhook->vkCode, kbhook->scanCode, keymap, keytxt, sizeof(keytxt) / sizeof(WCHAR), 0) == 1) {
    // ...
}

I used ToUnicodeEx and it actually worked correctly with dead keys - it did produce characters like à and â. I don't know if there were other issues with this approach, but at least one aspect which didn't work now works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants