How do I send a keyboard layout switch event with autohotkey in Windows 7?

4

1

I want to remap the keyboard layout/language switching to Win+Space, I tried it with:

LWin & Space::Send, ^+

But it doesn't seem to work, I would want it to send a Ctrl+Shift sequence so that I can switch between keyboard layouts with Win+Space.

How can I make this work?

Void

Posted 2013-02-05T11:18:19.327

Reputation: 233

1Windows 10 has this by default now. – Khalid Hussain – 2016-12-08T07:19:49.490

And boy do I wish I could turn it off. – Chris F Carroll – 2017-10-03T16:19:14.333

Answers

8

Win+Space switch works fine on my win7-x64:

; This should be replaced by whatever your native language is. See 
; http://msdn.microsoft.com/en-us/library/dd318693%28v=vs.85%29.aspx
; for the language identifiers list.
ru := DllCall("LoadKeyboardLayout", "Str", "00000419", "Int", 1)
en := DllCall("LoadKeyboardLayout", "Str", "00000409", "Int", 1)

#Space::
w := DllCall("GetForegroundWindow")
pid := DllCall("GetWindowThreadProcessId", "UInt", w, "Ptr", 0)
l := DllCall("GetKeyboardLayout", "UInt", pid)
if (l = en) 
{
    PostMessage 0x50, 0, %ru%,, A
}
else
{
PostMessage 0x50, 0, %en%,, A
}

more details: http://www.autohotkey.com/board/topic/70019-keyboard-layout-switcher-for-many-layouts/

Andrei Pak

Posted 2013-02-05T11:18:19.327

Reputation: 81

Just a tip: Please use "Str", "00000C00" for the default english layout. Works like a charm! If only I could now disable ALT+SHIFT shortcut so only WIN+SPACE changes it... – user7783780 – 2017-03-29T03:45:47.547

1

@Andrei Pak's script has the following limitations:

  1. Doesn't work in FileOpen dialog (and some other windows)
  2. Doesn't work in console windows
  3. You need to know in advance the KLID of layout (0x04090409, etc) and hard code it

This script cycles through your system active layouts using Win+Space:

#Space::PostMessage WM_INPUTLANGCHANGEREQUEST:=0x50, INPUTLANGCHANGE_FORWARD:=0x2,,, % (hWndOwn := DllCall("GetWindow", Ptr, hWnd:=WinExist("A"), UInt, GW_OWNER := 4, Ptr)) ? "ahk_id" hWndOwn : "ahk_id" hWnd

If you want more control (have >2 layouts, need hotkey to set certain layout):

F2::Lyt.Set()               ; switch input language.
F3::Lyt.Set("Forward")      ; move forward (cycle) in current layout list
F4::Lyt.Set("-en")          ; set first non-english
F7::Lyt.Set("en", "global") ; set first english layout in all windows
F8::Lyt.Set(2)              ; set second layout in current layout list

This depends on the Lyt class; you'll need to reference this or copy and paste it into your code.

stealzy

Posted 2013-02-05T11:18:19.327

Reputation: 21

1This doesn't seem to be a complete, self-contained answer to the question. Can you elaborate? – fixer1234 – 2017-02-17T21:09:12.387

OK then, if you say so. – stealzy – 2017-05-15T18:13:17.567

This is a much better solution. – Herb Caudill – 2018-02-06T09:10:03.013

1

Are you sure this is Ctrl+Shift? On my system it is Alt+Shift. anyway, use this command:

LWin & Space::Send, {Alt Down}{Shift}{Alt up}

Replace Alt for Ctrl if your system indeed uses the Ctrl+Shift combination.

Robert Ilbrink

Posted 2013-02-05T11:18:19.327

Reputation: 1 509

0

You can easely (by changing first two lines to:

$~#Space::LangSwitch(1)
$~#Space up::LangSwitch(2)

) modify wOxxOm's Keyboard layout switcher for using Win+Space instead of right control. And it will work no matter which hotkeys are set up for switching layout in Windows (default is Alt+Shift, I'm using Ctrl+Shift).

Because of non-ahk-native switching (script uses WinAPI), whole script is rather bulky, so I didn't paste it inline. In case of autohotkey.com inaccessibility, here it is: http://pastebin.com/ygm3f6sp

(again, all credits go to wOxxOm, source script is there: http://www.autohotkey.com/board/topic/24666-keyboard-layout-switcher/)

P.S. That script is quite old, and has hardcoded byte-offsets, so I'm not sure if it will work with 64-bit Autohotkey.exe or in 64-bit Windows. I only have 32-bit at hand.

LogicDaemon

Posted 2013-02-05T11:18:19.327

Reputation: 1 681

-1

Your script is:

#Space::
Sleep 500
Send, {CTRL}
return

then install puntoswitcher and set change language on control press. thats all

zend

Posted 2013-02-05T11:18:19.327

Reputation: 1