AutoHotkey Community

It is currently May 27th, 2012, 12:43 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 18 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: September 24th, 2005, 10:21 pm 
for a hotkey script i need to if my keyboard is currently set to english or hebrew.
in order to do it, i thought about sending a keystroke to the clipboard only, read it back and compare it to its ascii code.
for example, send an "r" and check if its ascii result is 114 <=> english or 248 <=> hebrew.
how can i send it to the clipboard ?
is there a simplier way of deciding the keyboard status ?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 25th, 2005, 1:22 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Perhaps the trouble is caused by switching your keyboard layout in the active window without changing it in the script. UPDATE: Hopefully this limitation/restriction is no longer present in newer versions. If anyone has any trouble with it, please let me know.

Also related is the built-in variable A_Language, but that only contains the system's default language, not the user's currently selected language.


Last edited by Chris on September 2nd, 2006, 5:00 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject: that's correct, but...
PostPosted: September 25th, 2005, 3:23 pm 
that's my problem.
i'm trying to send certain keystrokes (an email address), which are of course language dependant, using the hotkey. It's imperative to know if I'm not in English in order to change English (the change itself is simple according to your FAQ link) before sending the keystrokes and change it back to Hebrew afterwards.
I tried to over come it by sending a keystroke to clipboard, then read the outcome & compare it to its correspondent ascii: if they are equal => english, otherwise = > hebrew (& have to be changed).


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 25th, 2005, 4:33 pm 
Code:
ש::
{
If A_Hotkey = ש
   ToolTip, Hebrew Keyboard Layout!
Else
   ToolTip, Whatever Language Keyboard Layout!
}

Something like this ? Untested.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 25th, 2005, 5:37 pm 
Offline

Joined: September 25th, 2005, 4:31 pm
Posts: 610
The following code demonstrates how to determine the current keyboard layout. The keyboard layout is configured per thread.

Code:
SetFormat, integer, hex
CoordMode, ToolTip, relative

Run, notepad.exe

WinWait, Untitled - Notepad ahk_class Notepad
WinGet, hw_notepad, ID

tid_notepad := DllCall( "GetWindowThreadProcessId", "uint", hw_notepad, "uint", 0 )

lang_id := DllCall( "GetKeyboardLayout", "uint", tid_notepad ) & 0xFFFF

if ( lang_id = 0x0409 )
   lang_id_text = English (United States)
else if ( lang_id = 0x0419 )
   lang_id_text = Russian
else if ( lang_id = 0x040d )
   lang_id_text = Hebrew
else
   lang_id_text = undefined language: %lang_id%

text = keyboard layout for notepad is %lang_id_text% (%lang_id%)
text = %text%`n`nnow activate the alternate language, then press any key to continue ...
ToolTip, %text%, 100, 100

Input, key, L1

lang_id := DllCall( "GetKeyboardLayout", "uint", tid_notepad ) & 0xFFFF

if ( lang_id = 0x0409 )
   lang_id_text = English (United States)
else if ( lang_id = 0x0419 )
   lang_id_text = Russian
else if ( lang_id = 0x040d )
   lang_id_text = Hebrew
else
   lang_id_text = undefined language: %lang_id%

text = %text%`n`nkeyboard layout for notepad is %lang_id_text% (%lang_id%)
text = %text%`n`npress any key to quit ...
ToolTip, %text%, 100, 100

Input, key, L1

WinClose, ahk_id %hw_notepad%
ExitApp


Report this post
Top
 Profile  
Reply with quote  
 Post subject: thanks
PostPosted: September 25th, 2005, 10:13 pm 
shimanov thanks,
that helped me solve the problem. :D


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 26th, 2005, 12:30 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Until now, I didn't know how to find out the active window's layout. So thanks for the example.

This might help provide a built-in feature or at least some more info in the help file of how to make a script adapt to the language of the currently active window. I think most users would want scripts to automatically switch languages to match that of the active window, at least before sending keystrokes. However, I think that would break some existing scripts, so the challenge now is to figure out how and whether to offer such a feature. Perhaps it can be a new directive.

Edit: As of v1.0.44.03, the Send command, Hotstrings, and similar features adapt to the keyboard layout of the active window. Hopefully, this will solve a lot of inconveniences like the ones in this topic.


Last edited by Chris on May 31st, 2006, 3:55 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
PostPosted: September 26th, 2005, 6:55 pm 
any idea how can I use the LoadKeyboardLayout function in order to set the correct language (instead of the shift + lalt combination which doesn't work properly) ?
thanks


Report this post
Top
  
Reply with quote  
PostPosted: September 26th, 2005, 8:45 pm 
Offline

Joined: September 25th, 2005, 4:31 pm
Posts: 610
eepp wrote:
any idea how to set the correct language


Some idea.

The following method works:

Code:
WinGet, hw_notepad, ID, Untitled - Notepad ahk_class Notepad

locale_en = 0x04090409 ; English (United States)
locale_ru = 0x04190419 ; Russian
locale_he = 0x040D040D ; Hebrew

WM_INPUTLANGCHANGEREQUEST = 0x0050

PostMessage, WM_INPUTLANGCHANGEREQUEST, 0, %locale_en%, Edit1, ahk_id %hw_notepad%

pause

PostMessage, WM_INPUTLANGCHANGEREQUEST, 0, %locale_he%, Edit1, ahk_id %hw_notepad%
return


eepp wrote:
use the LoadKeyboardLayout function


Keyboard Layout is controlled in the same manner. The high word in locale specifies the keyboard layout. For example:

English with standard layout is 0x04090409
English with Dvorak layout is 0xF0020409

Try the following code:

Code:
SetFormat, integer, hex

language_id = 0409   ; English (United States)
device_id = 0000   ; US layout
; device_id = 0001   ; United States-Dvorak layout

locale := DllCall( "LoadKeyboardLayout", "str", "0000" "0409", "uint", 0, "uint" )
MsgBox, % "input language and keyboard layout identifier = ". locale
return


I was able to determine the device id and language id by checking the key in the Registry at HKLM\SYSTEM\ControlSet001\Control\Keyboard Layouts\.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: code doesn't work
PostPosted: September 27th, 2005, 4:25 pm 
I tried it but it didn't change the language.
Instead, I use the {lAlt Down}{Shift}{lAlt Up} combination to change language when the wrong keyboard setting is detected.
thanks for your help.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 27th, 2005, 8:24 pm 
Offline

Joined: September 25th, 2005, 4:31 pm
Posts: 610
The Hebrew language is not available on my system; however, Russian was available with a default Windows XP installation and I have only tested switching from English to Russian (and vice versa).

Try the following experiment:

First, it is necessary to verify that the language is available. Run the following code:

Code:
SetFormat, Integer, hex

language_id = 0409   ; English (United States)
device_id = 0000   ; US keyboard
;device_id = 0001   ; United States-Dvorak keyboard

/*
language_id = 0419   ; Russian
device_id = 0000   ; Russian keyboard
device_id = 0001   ; Typewriter keyboard
*/

locale_id := DllCall( "LoadKeyboardLayout", "str", device_id language_id, "uint", 0, "uint" )
MsgBox, % "locale identifier = ". locale_id
return


If the last 4 hex digits are "0409" then the input locale has been loaded successfully; otherwise, you should observe your default input locale (e.g., "040D" for Hebrew). You can verify the input locale has been loaded by observing the "Text Services and Input Languages" window (refer to the "Installed Services" group). The window is accessible via the "Regional and Language Options" Control Panel applet or "Settings" menu in the language bar.

Second, run and activate Notepad. Switch the input language to Hebrew and verify the change by typing something.

Third, to actually choose an input locale for a thread, run the following code (tested with Notepad):

Code:
WinGet, hw_notepad, ID, Untitled - Notepad ahk_class Notepad

locale_en = 0x04090409   ; English (United States) with US keyboard layout
locale_en1 = 0xf0020409   ; English (United States) with Dvorak keyboard layout
locale_ru = 0x04190419   ; Russian with Russian keyboard layout
locale_ru1 = 0xf0080419   ; Russian with Typewriter keyboard layout

WM_INPUTLANGCHANGEREQUEST = 0x0050

PostMessage, WM_INPUTLANGCHANGEREQUEST, 0, locale_en, Edit1, ahk_id %hw_notepad%
return


Finally, activate Notepad and and verify that the input language has changed to English by typing something.

Table of Language Identifiers at MSDN.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: almost there
PostPosted: September 27th, 2005, 9:26 pm 
the "LoadKeyboardLayout" part doesn't seem to work:
I changed the language_id = 040D but I always get "locale identifier = 0x4090409". I don't have a clue what's wrong.

the secod part works perfect changing from English to Hebrew and backwards !


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 27th, 2005, 10:30 pm 
Offline

Joined: September 25th, 2005, 4:31 pm
Posts: 610
Unfortunately, I do not have Hebrew language support on my system. So I cannot verify its accessibility.

Try the following code:

Code:
InputBox, term, Locale Availability Verification, Enter the name of a language to verify availability.,, 300, 100,,,,, us

if ( ErrorLevel = 1 or term = "" )
   ExitApp

result=
loop, HKEY_LOCAL_MACHINE, SYSTEM\CurrentControlSet\Control\Keyboard Layouts, 2, 1
{
   RegRead, layout_file, HKEY_LOCAL_MACHINE, %a_LoopRegSubKey%\%a_LoopRegName%, Layout File
   RegRead, layout_text, HKEY_LOCAL_MACHINE, %a_LoopRegSubKey%\%a_LoopRegName%, Layout Text
   
   if ( InStr( layout_text, term, false ) )
   {
      result := result "`n" layout_text " (" a_LoopRegName ")"
   
      if ( FileExist( SystemRoot "\system32\" layout_file ) )
         result := result " is available."
      else
         result := result ", " layout_file " was not found."
   }
}

MsgBox, The following results match your search term (%term%):`n%result%
return


Run the code as is and search for "us". You should observe several items in the results. The text in parenthesis is the language_id and device_id combination used for input to LoadKeyboardLayout.


You can also try loading Spanish language support:

Code:
SetFormat, Integer, hex

/*
language_id = 0409   ; English (United States)
device_id = 0000   ; US keyboard
device_id = 0001   ; United States-Dvorak keyboard
*/
/*
language_id = 0419   ; Russian
device_id = 0000   ; Russian keyboard
device_id = 0001   ; Typewriter keyboard
*/
language_id = 040a   ; Spanish (Spain, Traditional Sort)
device_id = 0000   ; Spanish keyboard

KLF_ACTIVATE = 0x00000001
locale_id := DllCall( "LoadKeyboardLayout", "str", device_id language_id, "uint", 0, "uint" )
MsgBox, % "locale identifier = ". locale_id
return


Then test it with Notepad (or verify in Text Services and Input Languages"):

Code:
WinGet, hw_notepad, ID, Untitled - Notepad ahk_class Notepad

locale_en = 0x04090409      ; English (United States) with US keyboard layout
locale_en1 = 0xf0020409      ; English (United States) with Dvorak keyboard layout
locale_ru = 0x04190419      ; Russian with Russian keyboard layout
locale_ru1 = 0xf0080419      ; Russian with Typewriter keyboard layout
locale_es = 0x040a040a      ; Spanish (Spain, Traditional Sort) with Spanish keyboard layout   

WM_INPUTLANGCHANGEREQUEST = 0x0050

PostMessage, WM_INPUTLANGCHANGEREQUEST, 0, locale_es, Edit1, ahk_id %hw_notepad%
return


At the moment, I am juggling typing with English, Русски, and Español. Hopefully, this post will be in English.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: found...
PostPosted: September 28th, 2005, 8:11 am 
I found my mistake.
Now it works perfectly.
shimanov - Thank you very much for help.
:D


Report this post
Top
  
Reply with quote  
PostPosted: September 29th, 2005, 1:18 am 
Offline

Joined: July 15th, 2005, 3:19 pm
Posts: 140
Location: Denmark
Chris. This is a script to adjust the script layout and language to those of the active window.

Code:
   SetFormat, integer, hex
   WinGet, hw_notepad, ID, Repeater Test.ahk ahk_class AutoHotkey
   tid_notepad := DllCall( "GetWindowThreadProcessId", "uint", hw_notepad, "uint", 0 )
   lang_id := DllCall( "GetKeyboardLayout", "uint", tid_notepad )
   
   WinGet, hw_notepad, ID, A
   tid_notepad := DllCall( "GetWindowThreadProcessId", "uint", hw_notepad, "uint", 0 )
   lang_id := DllCall( "GetKeyboardLayout", "uint", tid_notepad )

   DetectHiddenWindows On
   SetTitleMatchMode 2
   WinGet, this_id, ID, Repeater Test.ahk ahk_class AutoHotkey
   PostMessage, 0x0050, 0, %lang_id%, , ahk_id %this_id%
   
   WinGet, hw_notepad, ID, Repeater Test.ahk ahk_class AutoHotkey
   tid_notepad := DllCall( "GetWindowThreadProcessId", "uint", hw_notepad, "uint", 0 )
   lang_id := DllCall( "GetKeyboardLayout", "uint", tid_notepad )


Hope you find it useful.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 18 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: chaosad, jrav and 23 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group