Help with converting a keyboard function Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Help with converting a keyboard function

Post by GameNtt » 03 Jun 2023, 07:23

Code: Select all

; v1
SetDefaultKeyboard(LocaleID){
	Global
	SPI_SETDEFAULTINPUTLANG := 0x005A
	SPIF_SENDWININICHANGE := 2
	Lan := DllCall("LoadKeyboardLayout", "Str", Format("{:08x}", LocaleID), "Int", 0)
	VarSetCapacity(Lan%LocaleID%, 8, 0)
	NumPut(LocaleID, Lan%LocaleID%)
	DllCall("SystemParametersInfo", "UInt", SPI_SETDEFAULTINPUTLANG, "UInt", 0, "UPtr", &Lan%LocaleID%, "UInt", SPIF_SENDWININICHANGE)
	WinGet, windows, List
	Loop %windows% {
		PostMessage 0x50, 0, %Lan%, , % "ahk_id " windows%A_Index%
	}
}
return

Code: Select all

; v2
SetDefaultKeyboardLang(LocaleID)
{
	Static SPI_SETDEFAULTINPUTLANG := 0x005A, SPIF_SENDWININICHANGE := 2	
	Lan := DllCall("LoadKeyboardLayout", "Str", Format("{:08x}", LocaleID), "Int", 0)
    binaryLocaleID := Buffer(8,0)
	NumPut("UPtr",LocaleID, binaryLocaleID)
	DllCall("SystemParametersInfo", "UInt", SPI_SETDEFAULTINPUTLANG, "UInt", 0, "UPtr", binaryLocaleID, "UInt", SPIF_SENDWININICHANGE)	
	owindows := WinGetList(,,,)
    Loop owindows.Length    
    	PostMessage 0x50, 0, Lan, , "ahk_id " windows[A_Index]
}
Why does code 1 work in v1, but code 2 not work in v2?
Is there some incorrect conversion? (There is a warning saying this, but seeing the docs, I don't understand why there is a warning [In the image it shows just "", but it is "UPtr"])
image.png
image.png (16.07 KiB) Viewed 700 times
image.png
image.png (15.5 KiB) Viewed 700 times
I also have another function for changing the language. What is the difference between SetDefaultKeyboard and SetInputLang? SetDefaultKeyboard(0x10409) works in v1 as intended so why does SetInputLang(0x10409) not work in v2 or v1? That is the LocaleID of Dvorak (Standard).

Code: Select all

SetInputLang(Lang)
{
PostMessage 0x50, 0, Lang, , "A" 
}
When I run this modified function of SetDefaultKeyboardLang(), the language changes as intended and no warning is displayed. What is the purpose of those lines which I ommitted then? Could someone explain all of this to me? Edit: When I use a hotkey that changes the language, it is now saying this and it holds the enter key down for some reason.
image.png
image.png (12.95 KiB) Viewed 574 times

Code: Select all

SetDefaultKeyboardLang(LocaleID)
{
	Lan := DllCall("LoadKeyboardLayout", "Str", Format("{:08x}", LocaleID), "Int", 0)
	owindows := WinGetList(,,,)
    Loop owindows.Length    
    	PostMessage 0x50, 0, Lan, , "ahk_id " owindows[A_Index]
}
Also, if binaryLocaleID is initialised as binaryLocaleID = 0, the DllCall warning does not show up, but NumPut warning shows up.
Even with the DllCall warning (binLocId = Buffer(8, 0)), the language gets changed for some reason.
I changed the byte size of the Buffer which removed the NumPut warning.
Last edited by GameNtt on 06 Jun 2023, 09:55, edited 9 times in total.

User avatar
mikeyww
Posts: 26595
Joined: 09 Sep 2014, 18:38

Re: Help with converting a keyboard function

Post by mikeyww » 03 Jun 2023, 10:08

I do not have all answers, but does the windows pseudoarray actually exist in your second script? If so, which line creates it, and what is the value of the first element?

Code: Select all

#Requires AutoHotkey v2.0
For each, hWnd in WinGetList()
 MsgBox hWnd
Explained: WinGetList

GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Re: Help with converting a keyboard function

Post by GameNtt » 03 Jun 2023, 10:34

It seems like line 9 (owindows := WinGetList(,,,)) creates the psuedoarray. When doing your method, the hWnd shows up.


Ok, after testing, the correct code for the loop would be PostMessage 0x50, 0, Lan, , "ahk_id" owindows[A_Index]. Instead of owindows%A_Index%, it is owindows[A_Index]. I was referencing v1 docs for that part. The first two errors are still there. Why does it say that the parameters are invalid?

User avatar
mikeyww
Posts: 26595
Joined: 09 Sep 2014, 18:38

Re: Help with converting a keyboard function

Post by mikeyww » 03 Jun 2023, 11:56

I don't know much about NumPut, but I think you'll need to follow the v2 syntax.

v1: NumPut(Number, VarOrAddress [, Offset := 0][, Type := "UPtr"])

v2: NumPut Type, Number, [Type2, Number2, ...] Target [, Offset]

Would have a careful look at the v2 documentation as well as examples.

Others can likely guide you further if needed.

GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Re: Help with converting a keyboard function

Post by GameNtt » 05 Jun 2023, 06:09

Could someone else help me with the other questions asked?

GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Re: Help with converting a keyboard function

Post by GameNtt » 06 Jun 2023, 09:37

I have made some changes to the original question.

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Help with converting a keyboard function  Topic is solved

Post by teadrinker » 06 Jun 2023, 10:35

The default input language is the input language that will be set when you open a new window. To change the input language of a particular window, you don't need to change the default input language every time, so SetDefaultKeyboardLang() is probably not needed at all. Use this instead:

Code: Select all

SetLayout(localeID := 0) {
    static WM_INPUTLANGCHANGEREQUEST := 0x50
         , INPUTLANGCHANGE_SYSCHARSET := 0x1
         , INPUTLANGCHANGE_FORWARD    := 0x2

    wParam := localeID ? INPUTLANGCHANGE_SYSCHARSET : INPUTLANGCHANGE_FORWARD
    PostMessage WM_INPUTLANGCHANGEREQUEST, wParam, localeID, ControlGetFocus('A') || unset, 'A'
}

GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Re: Help with converting a keyboard function

Post by GameNtt » 06 Jun 2023, 11:05

Thanks tea, but the Dvorak issue still is there. Why does that happen?
The Dvorak issue is applicable for all LocaleIDs that are greater than 0xFFFF. No clue as to why this method does not support non-standard keyboard layouts.

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Help with converting a keyboard function

Post by teadrinker » 06 Jun 2023, 11:30

I don't use Dvorak, I can't say for sure what the problem might be. Try calling my function without parameters, in which case it should switch the set layouts in order.

GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Re: Help with converting a keyboard function

Post by GameNtt » 07 Jun 2023, 07:34

Hmm. On my device, I have removed United States English Qwerty Keyboard and have only Dvorak on it. When I use 0x0409, it switches to Dvorak. I need to check further with multiple keyboard layouts of a particular language to see why this happens.

Post Reply

Return to “Ask for Help (v2)”