How to get key name with AutoHotkey

1

0

I want to switch input languages by using Henkan and KanaHira keys, these are implemented Japanese keyboard.

    vk1Csc079::
      PostMessage, 0x50, 0, 0x41E041E,, A ; 0x50 is WM_INPUTLANGCHANGEREQUEST
    Return

    vkF2sc070::
      PostMessage, 0x50, 0, 0x4110411,, A ; 0x50 is WM_INPUTLANGCHANGEREQUEST
    Return

vk1Csc079 is code for Henkan and vkF2sc070 is for KanaHira. With this code I could change keyboard Japanese to Thai, but I couldn't change Thai to Japanese backwards.

I noticed that because in Thai there are another key mapping so the KanaHira doesn't exist anymore after switching.

So I want to know what key code should I write for the function to enable it in Thai keyboard. Does anyone know about it?

Key Histories

Window: C:\Users\ironsand\Desktop\thai_keyboard.ahk - AutoHotkey v1.1.23.05
Keybd hook: no
Mouse hook: no
Enabled Timers: 0 of 0 ()
Interrupted threads: 0
Paused threads: 0 of 0 (0 layers)
Modifiers (GetKeyState() now) = 
Modifiers (Hook's Logical) = 
Modifiers (Hook's Physical) = 
Prefix key is down: no

NOTE: Only the script's own keyboard events are shown
(not the user's), because the keyboard hook isn't installed.

NOTE: To disable the key history shown below, add the line "#KeyHistory 0" anywhere in the script.  The same method can be used to change the size of the history buffer.  For example: #KeyHistory 100  (Default is 40, Max is 500)

The oldest are listed first.  VK=Virtual Key, SC=Scan Code, Elapsed=Seconds since the previous event.  Types: h=Hook Hotkey, s=Suppressed (blocked), i=Ignored because it was generated by an AHK script, a=Artificial, #=Disabled via #IfWinActive/Exist, U=Unicode character (SendInput).

VK  SC  Type    Up/Dn   Elapsed Key     Window
-------------------------------------------------------------------------------------------------------------
Press [F5] to refresh.

ironsand

Posted 2016-05-16T12:27:29.383

Reputation: 1 757

After you switch the keyboard to Thai, can you start AutoHotkey, right-click the tray icon > Open > View > Key History, press the physical key that would normally map to KanaHira, then refresh in order to see the VK that now corresponds to that key? If so, you could replace vkF2sc070 with the key code generated from the same physical key in the Thai layout – JJohnston2 – 2016-05-18T03:04:41.233

I tried that but the key history and script info doesn't show me the key code. Am I doing something wrong? – ironsand – 2016-05-18T08:30:18.590

You can define a hotkey for switching keyboards via Control Panel / Region and Language / Keyboards and Languages / Change keyboards / Advanced key settings. However, the hotkeys permitted here are of limited variety. – harrymc – 2016-05-19T10:08:17.563

@harrymc yeah, that's the reason I want to change it by Auto hotkey. – ironsand – 2016-05-19T10:54:14.453

For the comment by @JJohnston2 : Did you press F5 after pressing the key ? – harrymc – 2016-05-19T11:06:14.433

Yes, and I also tried from menu item. – ironsand – 2016-05-19T12:00:54.493

If you can see normal keys in Key History, but not this key, then I think it might be unusable in AutoHotkey. – harrymc – 2016-05-19T15:07:29.880

@harrymc I couldn't get normal key neither and I found out that I must add #InstallKeybdHook to get key history. Finally I got the code of the key vkFFsc070. Thanks!! – ironsand – 2016-05-20T19:05:24.760

@JJohnston2: You should answer this post with more details. – harrymc – 2016-05-20T19:09:14.063

Answers

1

There are certain functions that will force a keyboard hook to be installed and others that will not. Your script doesn't have any that mandate the keyboard hook, therefore it is not globally running/available

Note the top of your log file... keyboard hook = no....

Window: C:\Users\ironsand\Desktop\thai_keyboard.ahk - AutoHotkey v1.1.23.05
Keybd hook: no
Mouse hook: no
Enabled Timers: 0 of 0 ()

And the second note...

NOTE: Only the script's own keyboard events are shown
(not the user's), because the keyboard hook isn't installed.

In order to force a keyboard hook and see keystrokes for all key presses, you should be able to add the #InstallKeybdHook or #UseHook On directive at the top of your script. After doing this, you should be able to go to key history and see that the keyboard hook is enabled.

Once the keyboard hook is enabled and you have the key history window up, press the key you need to get the key history for. It will not appear in the window however until you press F5, as noted by @harrymc.

After writing this, I saw that there were more comments and that you have already figured all of this out. I am going to post any way for whoever else may happen along.

JJohnston2

Posted 2016-05-16T12:27:29.383

Reputation: 1 341