Sunday, March 11, 2012

Use Caps Lock to switch input languages in Windows

AutoHotKey is a great piece of software to use, if you're on Windows and getting jealous of Xorg's grp:caps_toggle option under Linux.

AutoHotKey scripts can do magic, but for now, let's use a script to switch between English and Greek using Caps Lock.

An initial approach would be to reprogram Caps Lock to send Left Alt + Shift, the default language switch combination in Windows. However, I've also got Spanish enabled on my keyboard, and there doesn't seem to be a way to exclude a language when switching.

So here's an alternative approach, which:
  1. allows selecting each language individually, based on an Left Alt + Shift + number combination,
  2. enables switching between only a subset of the supported input languages.
Start by setting a different Left Alt + Left Shift + number combination for each language under Control Panel / Region and Language / Keyboards and Languages / Change Keyboards / Advanced Key Settings, and disable global Alt-Shift switching. When you're done, your settings should be similar to the following:

Between input languages: (None)
To English (united States) - US: Left Alt + Shift + 0
To Greek (Greece) - Greek: Left Alt + Shift +9
To Spanish (Spain, International Sort) - Spanish: Left Alt + Shift + 8

Since there's no keys to switch languages, an AHK script will have to remember the current input language, and switch to the other one directly. Here's a sample script, using a simple boolean to remember the state:

ingreek = False
CapsLock::
ingreek := not ingreek
OutputDebug, ingreek has become %ingreek%
if ingreek {
    OutputDebug, Sending Alt-Shift-9
    SendInput, {Lalt Down}{LShift Down}9{LShift Up}{LAlt Up}
} else {
    OutputDebug, Sending Alt-Shift-0
    SendInput, {Lalt Down}{LShift Down}0{LShift Up}{LAlt Up}
}

It's my first ever script, so please forgive any style errors :)

So there you have it: you can use Caps Lock to switch between English and Greek, or Left Alt + Shift + 8 to get into Spanish mode directly.

A debugging tip: The OutputDebug call sends output to the system debugger. Download a copy of  SysInternals' DebugView, run it with elevated privileges and toggle Capture / Capture Global Win32, to view the debugging output of the script.

1 comment: