Get current keyboard layout

Post your working scripts, libraries and tools for AHK v1.1 and older
XShayanX
Posts: 83
Joined: 16 Sep 2018, 04:48

Get current keyboard layout

25 Mar 2019, 13:48

I found a topic on the archived forums for a script to get the current keyboard layout:
https://autohotkey.com/board/topic/43043-get-current-keyboard-layout/
User, YMP suggested to use this code:

Code: Select all

F11::
  SetFormat, Integer, H
  WinGet, WinID,, A
  ThreadID:=DllCall("GetWindowThreadProcessId", "UInt", WinID, "UInt", 0)
  InputLocaleID:=DllCall("GetKeyboardLayout", "UInt", ThreadID, "UInt")
  MsgBox, %InputLocaleID%
Return
But as user, Ironhide mentioned, it doesn't work in a console window, so here's my solution,
Instead of getting the Thread Process ID and storing it in a variable then using it on the GetKeyboardLayout DllCall function, just use 0 for threadID, which means the current thread,
As suggested here: https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getkeyboardlayout
The code will look like this:

Code: Select all

F11::
  SetFormat, Integer, H
  InputLocaleID:=DllCall("GetKeyboardLayout", "UInt", 0, "UInt")
  MsgBox, %InputLocaleID%
Return
If you want to have the layout in decimal format you can delete this line: SetFormat, Integer, H
The code will look like this:

Code: Select all

F11::
  InputLocaleID:=DllCall("GetKeyboardLayout", "UInt", 0, "UInt")
  MsgBox, %InputLocaleID%
Return
For changing the keyboard layout check this:
https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-loadkeyboardlayouta
https://docs.microsoft.com/en-us/windows/desktop/Intl/language-identifier-constants-and-strings
XShayanX
Posts: 83
Joined: 16 Sep 2018, 04:48

Re: Get current keyboard layout

28 Mar 2019, 10:16

SMH! I spent so much time on this and I was wrong, my script shows the previous layout ID and only on the second try of the hotkey it will show the current layout id.

One important thing I learnt:
DllCalls are Case Sensitive, for example I was trying to get ThreadID for explorer.exe and it would always return 0, what I should had done was typing, Explorer.EXE.

Here's the script that works (finally!):

Code: Select all

F11:: 
	WinGet, WinID,, A
	ThreadID:=DllCall("GetWindowThreadProcessId", "UInt", WinID, "UInt", 0)
	InputLocaleID:=DllCall("GetKeyboardLayout", "UInt", ThreadID, "UInt")
	If(!InputLocaleID){
		WinActivate, ahk_class WorkerW
		WinGet, WinID2,, ahk_class WorkerW
		ThreadID:=DllCall("GetWindowThreadProcessId", "UInt", WinID2, "UInt", 0)
		WinActivate, ahk_id %WinID%
		InputLocaleID:=DllCall("GetKeyboardLayout", "UInt", ThreadID, "UInt")
	}
Return
Last edited by XShayanX on 29 Mar 2019, 06:07, edited 2 times in total.
wi1k1n
Posts: 1
Joined: 21 Oct 2021, 08:28
Contact:

Re: Get current keyboard layout

21 Oct 2021, 08:34

@XShayanX, Thanks a lot for this huge work!
User avatar
submeg
Posts: 326
Joined: 14 Apr 2017, 20:39
Contact:

Re: Get current keyboard layout

24 Oct 2021, 20:53

@XShayanX, could this be used to switch between keyboard layouts? Say between QWERTY and Dvorak?
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
XShayanX
Posts: 83
Joined: 16 Sep 2018, 04:48

Re: Get current keyboard layout

30 Oct 2021, 09:49

wi1k1n wrote:
21 Oct 2021, 08:34
@XShayanX, Thanks a lot for this huge work!
You're welcome!
submeg wrote:
24 Oct 2021, 20:53
@XShayanX, could this be used to switch between keyboard layouts? Say between QWERTY and Dvorak?
Yes, of course! Each layout has it's own "identifier".
tuangd
Posts: 5
Joined: 14 Aug 2020, 05:06

Re: Get current keyboard layout

10 Oct 2022, 09:59

XShayanX wrote:
28 Mar 2019, 10:16
SMH! I spent so much time on this and I was wrong, my script shows the previous layout ID and only on the second try of the hotkey it will show the current layout id.

One important thing I learnt:
DllCalls are Case Sensitive, for example I was trying to get ThreadID for explorer.exe and it would always return 0, what I should had done was typing, Explorer.EXE.

Here's the script that works (finally!):

Code: Select all

F11:: 
	WinGet, WinID,, A
	ThreadID:=DllCall("GetWindowThreadProcessId", "UInt", WinID, "UInt", 0)
	InputLocaleID:=DllCall("GetKeyboardLayout", "UInt", ThreadID, "UInt")
	If(!InputLocaleID){
		WinActivate, ahk_class WorkerW
		WinGet, WinID2,, ahk_class WorkerW
		ThreadID:=DllCall("GetWindowThreadProcessId", "UInt", WinID2, "UInt", 0)
		WinActivate, ahk_id %WinID%
		InputLocaleID:=DllCall("GetKeyboardLayout", "UInt", ThreadID, "UInt")
	}
Return
Hi @XShayanX
I am having a similar problem with the layout ID and how to get the correct keyboard layout. My problem was, sometimes it reports the other keyboard layout as I was switching between two layouts.
Following your example, it's kinda working. But because I migrated to AHK V2, I want to understand why you call WinActivate.
Would you mind explaining the code in the if block?
Thank you so much.
XShayanX
Posts: 83
Joined: 16 Sep 2018, 04:48

Re: Get current keyboard layout

10 Oct 2022, 11:41

tuangd wrote:
10 Oct 2022, 09:59
XShayanX wrote:
28 Mar 2019, 10:16
SMH! I spent so much time on this and I was wrong, my script shows the previous layout ID and only on the second try of the hotkey it will show the current layout id.

One important thing I learnt:
DllCalls are Case Sensitive, for example I was trying to get ThreadID for explorer.exe and it would always return 0, what I should had done was typing, Explorer.EXE.

Here's the script that works (finally!):

Code: Select all

F11:: 
	WinGet, WinID,, A
	ThreadID:=DllCall("GetWindowThreadProcessId", "UInt", WinID, "UInt", 0)
	InputLocaleID:=DllCall("GetKeyboardLayout", "UInt", ThreadID, "UInt")
	If(!InputLocaleID){
		WinActivate, ahk_class WorkerW
		WinGet, WinID2,, ahk_class WorkerW
		ThreadID:=DllCall("GetWindowThreadProcessId", "UInt", WinID2, "UInt", 0)
		WinActivate, ahk_id %WinID%
		InputLocaleID:=DllCall("GetKeyboardLayout", "UInt", ThreadID, "UInt")
	}
Return
Hi @XShayanX
I am having a similar problem with the layout ID and how to get the correct keyboard layout. My problem was, sometimes it reports the other keyboard layout as I was switching between two layouts.
Following your example, it's kinda working. But because I migrated to AHK V2, I want to understand why you call WinActivate.
Would you mind explaining the code in the if block?
Thank you so much.
Hello @tuangd
My main laptop which runs Windows died a week ago, I'm currently on a Linux machine and can't check this for you but you can check it yourself, if you check Spy++ which comes with Visual Studio, if I remember correctly the ahk_class WorkerW is the parent window of all windows, so it's the first one that's notified when the keyboard layout changes, we activate it so we correctly get the current keyboard layout.

You can check WorkerW being the parent window of all windows with Spy++.

BTW if my laptop comes back to life, I have a new method for this issue, I created my own dll which correctly identifies the keyboard layout and all you have to do is call the dll from AHK. I will upload the source code of my dll so that you can compile it yourself, just do remind me in a week. I'm taking my laptop to a repair shop.
tuangd
Posts: 5
Joined: 14 Aug 2020, 05:06

Re: Get current keyboard layout

13 Oct 2022, 00:23

XShayanX wrote:
10 Oct 2022, 11:41
Hello @tuangd
My main laptop which runs Windows died a week ago, I'm currently on a Linux machine and can't check this for you but you can check it yourself, if you check Spy++ which comes with Visual Studio, if I remember correctly the ahk_class WorkerW is the parent window of all windows, so it's the first one that's notified when the keyboard layout changes, we activate it so we correctly get the current keyboard layout.

You can check WorkerW being the parent window of all windows with Spy++.

BTW if my laptop comes back to life, I have a new method for this issue, I created my own dll which correctly identifies the keyboard layout and all you have to do is call the dll from AHK. I will upload the source code of my dll so that you can compile it yourself, just do remind me in a week. I'm taking my laptop to a repair shop.
I'll do that. Thank you so much for the answer and information. Looking forward to the DLL *bow*
XShayanX
Posts: 83
Joined: 16 Sep 2018, 04:48

Re: Get current keyboard layout

14 Oct 2022, 09:32

tuangd wrote:
13 Oct 2022, 00:23
XShayanX wrote:
10 Oct 2022, 11:41
Hello @tuangd
My main laptop which runs Windows died a week ago, I'm currently on a Linux machine and can't check this for you but you can check it yourself, if you check Spy++ which comes with Visual Studio, if I remember correctly the ahk_class WorkerW is the parent window of all windows, so it's the first one that's notified when the keyboard layout changes, we activate it so we correctly get the current keyboard layout.

You can check WorkerW being the parent window of all windows with Spy++.

BTW if my laptop comes back to life, I have a new method for this issue, I created my own dll which correctly identifies the keyboard layout and all you have to do is call the dll from AHK. I will upload the source code of my dll so that you can compile it yourself, just do remind me in a week. I'm taking my laptop to a repair shop.
I'll do that. Thank you so much for the answer and information. Looking forward to the DLL *bow*
Hello! @tuangd

Here you go:
https://github.com/Saya47/GetKeyboardLayout
tuangd
Posts: 5
Joined: 14 Aug 2020, 05:06

Re: Get current keyboard layout

14 Oct 2022, 11:27

XShayanX wrote:
14 Oct 2022, 09:32
Hello! @tuangd

Here you go:
https://github.com/Saya47/GetKeyboardLayout
Thank you so much!!!
User avatar
submeg
Posts: 326
Joined: 14 Apr 2017, 20:39
Contact:

Re: Get current keyboard layout

14 Dec 2022, 15:20

XShayanX wrote:
14 Oct 2022, 09:32
tuangd wrote:
13 Oct 2022, 00:23
XShayanX wrote:
10 Oct 2022, 11:41
Hello @tuangd
My main laptop which runs Windows died a week ago, I'm currently on a Linux machine and can't check this for you but you can check it yourself, if you check Spy++ which comes with Visual Studio, if I remember correctly the ahk_class WorkerW is the parent window of all windows, so it's the first one that's notified when the keyboard layout changes, we activate it so we correctly get the current keyboard layout.

You can check WorkerW being the parent window of all windows with Spy++.

BTW if my laptop comes back to life, I have a new method for this issue, I created my own dll which correctly identifies the keyboard layout and all you have to do is call the dll from AHK. I will upload the source code of my dll so that you can compile it yourself, just do remind me in a week. I'm taking my laptop to a repair shop.
I'll do that. Thank you so much for the answer and information. Looking forward to the DLL *bow*
Hello! @tuangd

Here you go:
https://github.com/Saya47/GetKeyboardLayout
Hey @XShayanX, how do you go about actually using this dll? Do you not have to install it somewhere?
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
XShayanX
Posts: 83
Joined: 16 Sep 2018, 04:48

Re: Get current keyboard layout

15 Dec 2022, 08:59

submeg wrote:
14 Dec 2022, 15:20
XShayanX wrote:
14 Oct 2022, 09:32
tuangd wrote:
13 Oct 2022, 00:23
XShayanX wrote:
10 Oct 2022, 11:41
Hello @tuangd
My main laptop which runs Windows died a week ago, I'm currently on a Linux machine and can't check this for you but you can check it yourself, if you check Spy++ which comes with Visual Studio, if I remember correctly the ahk_class WorkerW is the parent window of all windows, so it's the first one that's notified when the keyboard layout changes, we activate it so we correctly get the current keyboard layout.

You can check WorkerW being the parent window of all windows with Spy++.

BTW if my laptop comes back to life, I have a new method for this issue, I created my own dll which correctly identifies the keyboard layout and all you have to do is call the dll from AHK. I will upload the source code of my dll so that you can compile it yourself, just do remind me in a week. I'm taking my laptop to a repair shop.
I'll do that. Thank you so much for the answer and information. Looking forward to the DLL *bow*
Hello! @tuangd

Here you go:
https://github.com/Saya47/GetKeyboardLayout
Hey @XShayanX, how do you go about actually using this dll? Do you not have to install it somewhere?
No DLL's don't need to be "installed", simple copy and paste to your desired location would suffice.
You might want to read on calling DLL's from AHK here:
https://www.autohotkey.com/docs/commands/DllCall.htm
And I suggest studying about DLL's:
https://learn.microsoft.com/en-us/troubleshoot/windows-client/deployment/dynamic-link-library

But do not limit yourself to the links I provided, feel free to Google about these.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: gwarble, JoeWinograd and 140 guests