Strange behavior when using GetKey

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
TheBeginner
Posts: 91
Joined: 19 Oct 2018, 12:05

Strange behavior when using GetKey

Post by TheBeginner » 29 May 2021, 15:17

[Moderator's note: Topic moved from Bug Reports.]

Have encountered a strange behavior when using GetKey... , that seems very much like a bug

*** Hopefully this time it's a real bug, tried my best to make sure (long thread on discord)
https://discord.com/channels/115993023636176902/743668704054607893/848262692326080513

Code: Select all

9::
    key := "ы"
    name := GetKeyName(key)
    vk := GetKeyVK(key)
    sc := GetKeySC(key)

    MsgBox, % Format("Name:`t{}`nVK:`t{:X}`nSC:`t{:X}", name, vk, sc)
Return
How to reproduce
Run your script with the English keyboard layout then press 9


1st press 9 key - English layout --> 0
2nd press 9 key - English layout --> 0
---- switched to Russian
3rd press 9 key - Russian layout --> 0
4th press 9 key - Russian layout --> 53 1f

Notice that only in the second try in the Russian layout Does the call produce a result, although this example is with the Russian layout it's the same with other languages even English
for some reason the first attempt to call the function after changing a keyboard layout produces the previous attempt result, while they second attempt produces the current key result

Even if you change the character to an English character such as "a"

Code: Select all

key := "a"
After a keyboard layout change alt+shift only the second attempt will produce the updated code

1 - English layout --> works
2 - English layout --> works
switched to Russian
3 - Russian layout --> works but retrieves the previously layout code
4 - Russian layout --> new code
switched to English
5 - English layout --> works but retrieves the previously layout code
6 - English layout --> works


Link to a short video showing the problem
https://discord.com/channels/115993023636176902/743668704054607893/848268008741011466

lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Strange behavior when using GetKey

Post by lexikos » 31 May 2021, 16:59

Nope.

Each thread has its own active keyboard layout, and windows of different programs belong to different threads. Notice that with GetKey' you do not specify a window or keyboard layout - as with hotkeys, it uses the script's keyboard layout. The behaviour on Windows 10 depends on the system setting "Let me use a different input method for each app window":
  • If it's turned off, the script's keyboard layout does not change until you focus one of its windows, such as MsgBox. If you use ToolTip, you will only get key translations for the default/initial keyboard layout, unless you open the script's main window.
  • If it's turned on, the results of GetKey' will only change if you specifically change the input language while one of the script's windows is active (or you call ActivateKeyboardLayout).
GetKey' was added primarily to avoid scripts having to replicate AutoHotkey's internal list of key names, which are unique to AutoHotkey. If you just want to translate characters, you can call VkKeyScanEx via DllCall. This allows you to specify which keyboard layout you want to use for translation.

TheBeginner
Posts: 91
Joined: 19 Oct 2018, 12:05

Re: Strange behavior when using GetKey

Post by TheBeginner » 01 Jun 2021, 03:38

thanks @lexikos

I did try and call VkKeyScanEx with the keyboard layout, couldn't make it work it was producing even worse results than just calling GetKeyXX ,
Getting a -1 on characters that are retrieved correctly with GetKeyXX, sending the wrong keyboard key when sending {vkXX}

I don't want to post any code here since I thought it was a bug,
but if you're feeling generous
https://www.autohotkey.com/boards/viewtopic.php?f=76&t=91162

I've been struggling with this for more than a week with no success, so any help will be appreciated

Post Reply

Return to “Ask for Help (v1)”