Jump to content
Sign in to follow this  
adamgal

How to detect current language

Recommended Posts

Does anyone know a way to detect the language the user is typing in?

I tried dllcall with getcurrentkeyboardlayout but that only returns the default keyboard layout.

anyone have any other ideas?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

$userkeyboard=RegRead("HKCU\Keyboard Layout\Preload","1")

it should return something equivalent to @OSLANG :)

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites
Guest cvi

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Guest cvi

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Guest cvi

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

Share this post


Link to post
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.

This is just what I need, thanks a lot

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...