Jump to content

[Question]Changing keyboard layout


Recommended Posts

Hi!

I'm trying to create a script that when a hotkey is pressed it will change your keyboard layout.

Originally i thought i could do it through regedit, but this attempt has failed.

If RegRead("HKCU\Keyboard Layout\Preload", "1") <> "00000409" Then
     RegWrite("HKCU\Keyboard Layout\Preload", "1", "REG_SZ", "00000409")
EndIf

Anyone can help me on this matter?

Link to comment
Share on other sites

have found below code:

Func _WinAPI_GetKeyboardLayout($hWnd)

    Local $Ret = DllCall('user32.dll', 'long', 'GetWindowThreadProcessId', 'hwnd', $hWnd, 'ptr', 0)

    If (@error) Or ($Ret[0] = 0) Then
        Return SetError(1, 0, 0)
    EndIf
    
    $Ret = DllCall('user32.dll', 'long', 'GetKeyboardLayout', 'long', $Ret[0])
    
    If (@error) Or ($Ret[0] = 0) Then
        Return SetError(1, 0, 0)
    EndIf
    
    Return '0000' & Hex($Ret[0], 4)
    
EndFunc   ;==>_WinAPI_GetKeyboardLayout

Func _WinAPI_SetKeyboardLayout($sLayout, $hWnd)

    If Not WinExists($hWnd) Then
        Return SetError(1, 0, 0)
    EndIf
    
    Local $Ret = DllCall('user32.dll', 'long', 'LoadKeyboardLayout', 'str', StringFormat('%08s', StringStripWS($sLayout, 8)), 'int', 0)

    If (@error) Or ($Ret[0] = 0) Then[url="http://msdn.microsoft.com/en-us/library/dd318693(VS.85).aspx"]http://msdn.microsoft.com/en-us/library/dd318693(VS.85).aspx[/url]
        Return SetError(1, 0, 0)
    EndIf
    
    DllCall('user32.dll', 'ptr', 'SendMessage', 'hwnd', $hWnd, 'int', 0x0050, 'int', 1, 'int', $Ret[0])
    
    Return SetError(0, 0, 1)
    
EndFunc   ;==>_WinAPI_SetKeyboardLayout

to read and change keyboard layout...

but as im still a bit new to autoit, i don't fully understand the above code

Why is the variable '$hWnd' for?

Link to comment
Share on other sites

have found below code:

Func _WinAPI_GetKeyboardLayout($hWnd)

    Local $Ret = DllCall('user32.dll', 'long', 'GetWindowThreadProcessId', 'hwnd', $hWnd, 'ptr', 0)

    If (@error) Or ($Ret[0] = 0) Then
        Return SetError(1, 0, 0)
    EndIf
    
    $Ret = DllCall('user32.dll', 'long', 'GetKeyboardLayout', 'long', $Ret[0])
    
    If (@error) Or ($Ret[0] = 0) Then
        Return SetError(1, 0, 0)
    EndIf
    
    Return '0000' & Hex($Ret[0], 4)
    
EndFunc   ;==>_WinAPI_GetKeyboardLayout

Func _WinAPI_SetKeyboardLayout($sLayout, $hWnd)

    If Not WinExists($hWnd) Then
        Return SetError(1, 0, 0)
    EndIf
    
    Local $Ret = DllCall('user32.dll', 'long', 'LoadKeyboardLayout', 'str', StringFormat('%08s', StringStripWS($sLayout, 8)), 'int', 0)

    If (@error) Or ($Ret[0] = 0) Then[url="http://msdn.microsoft.com/en-us/library/dd318693(VS.85).aspx"]http://msdn.microsoft.com/en-us/library/dd318693(VS.85).aspx[/url]
        Return SetError(1, 0, 0)
    EndIf
    
    DllCall('user32.dll', 'ptr', 'SendMessage', 'hwnd', $hWnd, 'int', 0x0050, 'int', 1, 'int', $Ret[0])
    
    Return SetError(0, 0, 1)
    
EndFunc   ;==>_WinAPI_SetKeyboardLayout

to read and change keyboard layout...

but as im still a bit new to autoit, i don't fully understand the above code

Why is the variable '$hWnd' for?

Hi,

if you have only 2 layouts, e.g DE and EN then this should work as well:

Send ("{ALTDOWN}{LSHIFT}")
Send ("{ALTUP}")
Link to comment
Share on other sites

  • 3 weeks later...

Hi,

if you have only 2 layouts, e.g DE and EN then this should work as well:

Send ("{ALTDOWN}{LSHIFT}")
Send ("{ALTUP}")

Hiya

I have to switch between 3layouts, dutch period / french / en-us

So i was actually looking to assign a hotkey to each layout.

But i still find it hard to decypher the code i found myselfm as i have no reference to the variables used in the code and since im pretty new to using autoit

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...