Jump to content

How to detect current language


Recommended Posts

my english is very poor,sorry

it is visual basic code. you will convert autoit code using dllcall()

i am newbie,so i don't convert autoit code. sorry

Declare Function GetKeyboardLayout Lib "user32" _

(ByVal dwLayout As Long) As Long

Declare Function ImmGetDescription Lib "imm32" Alias "ImmGetDescriptionA" _

(ByVal hKL As Long, _

ByVal lpszDescription As String, _

ByVal uBufLen As Long) As Long

Dim lngKeyboardLayoutHandle As Long

Dim strIMEDescription As String * 128

Dim lngWin32apiResultCode As Long

lngKeyboardLayoutHandle = GetKeyboardLayout(0)

lngWin32apiResultCode = ImmGetDescription(lngKeyboardLayoutHandle, _

strIMEDescription, _

Len(strIMEDescription))

Label2.Caption = Left(strIMEDescription, _

InStr(strIMEDescription, vbNullChar) - 1)

Edited by dok_do
Link to comment
Share on other sites

I wonder if RegRead("HKCU\Keyboard Layout\Preload","1") will work in this case...

dok_do is probably on the right track. If someone alternates between English and Japanese, for example, you probably need a DllCall that reads IME information.... (If I remember from a computer I helped set up, Japanese could be especially tricky since the language bar lets you choose "Direct Input" or "Hiragana" or "Katakana" ....)

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

  • 1 month later...

I wonder if RegRead("HKCU\Keyboard Layout\Preload","1") will work in this case...

.......

I tried it and it didn't work. I needed to know if the keyboard is set to Hebrew or English, and this function returned 409 (English), that is, the default setting and not the active one.

Cvi

Link to comment
Share on other sites

I don't found any references for "getcurrentkeyboardlayout". Maybe you mean "GetKeyboardLayoutName"? Anyway, try this code (works with beta only!). Because under NT's keyboard layout remember for each program, try to change layout in the inputbox.

InputBox("InputBox", "Try change layout here and press key")
$p = DllStructCreate("char[9]"); $KL_NAMELENGTH = 9
$ret = DllCall("user32.dll", "int", "GetKeyboardLayoutName", "ptr", DllStructGetPtr($p))
MsgBox (0, "title", DllStructGetData($p, 1))
DllStructDelete($p)
Edited by Lazycat
Link to comment
Share on other sites

I don't found any references for "getcurrentkeyboardlayout". Maybe you mean "GetKeyboardLayoutName"? Anyway, try this code (works with beta only!). Because under NT's keyboard layout remember for each program, try to change layout in the inputbox.

Thanks for the fast answer.

I have not tried it yet - because if the keyboard layout is remembered for each program, then it won't help me anyway.

My original problem was that something like:

send("H:\TAG_2005\AZMANCVI.DOC")

that worked with English keyboard, won't work with Hebrew keyboard layout because the "." is mapped in it to the key that has "?" in English.

So, I wanted to check the keyboard language, and change it if necessary.

If this change would affect only Autoit, and not the program receiving it - that won't help.

Cvi

Link to comment
Share on other sites

After some digging I come to following code:

Opt("WinTitleMatchMode", 4)
Run("notepad.exe")
Sleep(500)
$hWnd = WinGetHandle("classname=Notepad")
MsgBox (0, "Old Layout", _GetKeyboardLayout($hWnd))
_SetKeyboardLayout("00000409", $hWnd)
MsgBox (0, "New Layout", _GetKeyboardLayout($hWnd))

Func _GetKeyboardLayout($hWnd)
    Local $ret = DllCall("user32.dll", "long", "GetWindowThreadProcessId", "hwnd", $hWnd, "ptr", 0)
          $ret = DllCall("user32.dll", "long", "GetKeyboardLayout", "long", $ret[0])
          Return "0000" & Hex($ret[0], 4)
EndFunc

Func _SetKeyboardLayout($sLayoutID, $hWnd)
    Local $WM_INPUTLANGCHANGEREQUEST = 0x50
    Local $ret = DllCall("user32.dll", "long", "LoadKeyboardLayout", "str", $sLayoutID, "int", 0)
    DllCall("user32.dll", "ptr", "SendMessage", "hwnd", $hWnd, _
                                                "int", $WM_INPUTLANGCHANGEREQUEST, _
                                                "int", 1, _
                                                "int", $ret[0])
EndFunc

This more generic instead previous sample. You can get or set layout for given window.

Hope this helps.

Edited by Lazycat
Link to comment
Share on other sites

After some digging I come to following code:

This more generic instead previous sample. You can get or set layout for given window.

Hope this helps.

<{POST_SNAPBACK}>

Thanks!

It is just what I needed - it does work (at least in NOTEPAD as in your example. I hope it will also in real use)

I wrote alternating Hebrew and latin letters, looks good.

Cvi

Link to comment
Share on other sites

  • 1 year later...
  • 7 months later...

Use this

MsgBox(0, "Your Keyboard Language:", _Language())

Func _Language()
    
    $language = RegRead("HKCU\Keyboard Layout\Preload","1")
    
    Select
        Case StringInStr("0413,0813", $language)
            Return "Dutch"

        Case StringInStr("0409,0809,0c09,1009,1409,1809,1c09,2009,2409,2809,2c09,3009,3409", $language)
            Return "English"
            
        Case StringInStr("040c,080c,0c0c,100c,140c,180c", $language)
            Return "French"

        Case StringInStr("0407,0807,0c07,1007,1407", $language)
            Return "German"

        Case StringInStr("0410,0810", $language)
            Return "Italian"

        Case StringInStr("0414,0814", $language)
            Return "Norwegian"

        Case StringInStr("0415", $language)
            Return "Polish"

        Case StringInStr("0416,0816", $language)
            Return "Portuguese"

        Case StringInStr("040a,080a,0c0a,100a,140a,180a,1c0a,200a,240a,280a,2c0a,300a,340a,380a,3c0a,400a,440a,480a,4c0a,500a", $language)
            Return "Spanish"

        Case StringInStr("041d,081d", $language)
            Return "Swedish"

        Case Else
            Return "Other (can't determine with $language directly)"
        
    EndSelect

EndFunc
My UDFs:- _RegEnumKey
Link to comment
Share on other sites

  • Moderators

Use this

MsgBox(0, "Your Keyboard Language:", _Language())

Func _Language()
    
    $language = RegRead("HKCU\Keyboard Layout\Preload","1")
    
    Select
        Case StringInStr("0413,0813", $language)
            Return "Dutch"

        Case StringInStr("0409,0809,0c09,1009,1409,1809,1c09,2009,2409,2809,2c09,3009,3409", $language)
            Return "English"
            
        Case StringInStr("040c,080c,0c0c,100c,140c,180c", $language)
            Return "French"

        Case StringInStr("0407,0807,0c07,1007,1407", $language)
            Return "German"

        Case StringInStr("0410,0810", $language)
            Return "Italian"

        Case StringInStr("0414,0814", $language)
            Return "Norwegian"

        Case StringInStr("0415", $language)
            Return "Polish"

        Case StringInStr("0416,0816", $language)
            Return "Portuguese"

        Case StringInStr("040a,080a,0c0a,100a,140a,180a,1c0a,200a,240a,280a,2c0a,300a,340a,380a,3c0a,400a,440a,480a,4c0a,500a", $language)
            Return "Spanish"

        Case StringInStr("041d,081d", $language)
            Return "Swedish"

        Case Else
            Return "Other (can't determine with $language directly)"
        
    EndSelect

EndFunc
This doesn't work for me (My layout is 00000409)... so the StringInStr will fail

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

This might get you the results your looking for more efficiently Tiger:

Func _CurrentLang()
    Local $sLanNum = "0413,0813|0409,0809,0c09,1009,1409,1809,1c09,2009,2409,2809," & _
        "2c09,3009,3409|040c,080c,0c0c,100c,140c,180c|0407,0807,0c07,1007," & _
        "1407|0410,0810|0414,0814|0415|0416,0816|040a,080a,0c0a,100a,140a," & _
        "180a,1c0a,200a,240a,280a,2c0a,300a,340a,380a,3c0a,400a,440a,480a," & _
        "4c0a,500a|041d,081d"
    Local $aLanName = StringSplit("Dutch|English|French|German|Italian|" & _
        "Norwegian|Polish|Portuguese|Swedish", "|")
    Local $aLanNum = StringSplit($sLanNum, "|")
    Local $sLang = StringRight(RegRead("HKCU\Keyboard Layout\Preload","1"), 4)
    If $sLang = "" Then Return SetError(1, 0, "Not Defined")
    For $iCC = 1 To UBound($aLanNum) - 1
        If StringInStr($aLanNum[$iCC], $sLang) Then Return $aLanName[$iCC]
    Next
    Return SetError(2, 0, "Other")
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • 14 years later...
On 6/7/2005 at 3:45 AM, Lazycat said:

After some digging I come to following code:

 

 

Opt("WinTitleMatchMode", 4)
Run("notepad.exe")
Sleep(500)
$hWnd = WinGetHandle("classname=Notepad")
MsgBox (0, "Old Layout", _GetKeyboardLayout($hWnd))
_SetKeyboardLayout("00000409", $hWnd)
MsgBox (0, "New Layout", _GetKeyboardLayout($hWnd))

Func _GetKeyboardLayout($hWnd)
    Local $ret = DllCall("user32.dll", "long", "GetWindowThreadProcessId", "hwnd", $hWnd, "ptr", 0)
          $ret = DllCall("user32.dll", "long", "GetKeyboardLayout", "long", $ret[0])
          Return "0000" & Hex($ret[0], 4)
EndFunc

Func _SetKeyboardLayout($sLayoutID, $hWnd)
    Local $WM_INPUTLANGCHANGEREQUEST = 0x50
    Local $ret = DllCall("user32.dll", "long", "LoadKeyboardLayout", "str", $sLayoutID, "int", 0)
    DllCall("user32.dll", "ptr", "SendMessage", "hwnd", $hWnd, _
                                                "int", $WM_INPUTLANGCHANGEREQUEST, _
                                                "int", 1, _
                                                "int", $ret[0])
EndFunc

 

This more generic instead previous sample. You can get or set layout for given window.

 

Hope this helps.

I tried this code, the read function works for me, but the set function didn't

my language was chinese 0084, but after running the set function, it stays any 0084.

Can you please help? thanks

Link to comment
Share on other sites

1 minute ago, illidan333 said:

I tried this code, the read function works for me, but the set function didn't

my language was chinese 0084, but after running the set function, it stays any 0084.

Can you please help? thanks

nvm i figured it out, the issue i had is because the window handler i had was invalid.

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...