Change Keyboard Layout with both left and right Alt + Shift combos

Ok, yet another try, this one will do the trick I hope... ;)

Credits are due to reverendj1, who described a way of assigning a Custom key to toggle keyboard layout, using setxkbmap, which featured in the workaround alluded to in Edit 2 of my previous answer...

Here is the slightly more involved way, but will give you more flexibility for the key combination. First, copy and paste this script I wrote and name it something like kb_toggle.sh. Make sure to edit LANG1 and LANG2 to be the keyboard layout codes you need.

#!/bin/bash

LANG1="us"
LANG2="de"
CURRENT_LANG=$(setxkbmap -query | tail -n 1 | cut -f6 -d ' ')
if [ "$CURRENT_LANG" = $LANG1 ]; then
    setxkbmap $LANG2
else
    setxkbmap $LANG1
fi

make the file by right-clicking on it -> "Properties" -> "Permissions" tab, then select "Allow executing file as program.

Now, open "System Settings" -> "Keyboard" -> "Shortcuts" tab and select "Custom Shortcuts". Click the + button on the bottom and name the shortcut "Keyboard Toggle" or whatever you want, really. Then give the full path to the script you made earlier in the command box. Hit Apply. Click where it says "Disabled" then you can set the shortcut to whatever you want by clicking your key combination!

I think you'd need to change "de" to "ir".

Again, good luck!


Edit

You may even get away with a one-liner, if you prefer:

setxkbmap us,ir -option "grp:alt_shift_toggle"

Now you can switch by pressing alt+shift

See also http://wiki.lxde.org/en/Change_keyboard_layouts and http://www.x.org/releases/X11R7.7/doc/man/man1/setxkbmap.1.xhtml


Edit 2

Depending on how you decide to achieve your goal, you may want to include lv3:ralt_alt in your command, resulting in

setxkbmap us,ir -option "lv3:ralt_alt,grp:alt_shift_toggle"

See man xkeyboard-config | grep lv3 for details:

lv3:ralt_alt

Right Alt key never chooses 3rd level

   xkeyboard-config  provides  the  description files for the X Keyboard Extension (XKB). The configuration options below are
   usually applied with setxkbmap(1).

The "right Alt key" is often (always ?) not the same as the "left Alt key", but corresponds to the AltGr, see for instance

  • http://en.wikipedia.org/wiki/AltGr_key
  • http://en.wikipedia.org/wiki/Persian_keyboard

and as such has a different-and-distinct function.

You may want to try to try using xdotool to emulate certain key combination behaviour. Thus, you could set the

  1. Left Alt + Left Shift in the way described by Abdulkader in his answer, and
  2. the second combination as Right AltGr + Right Shift) with xdotool.

Edit

For a detailed example of how this can be done in general, with screenshots, plz refer to izx's excellent answer here. Regarding your questions specifically, you could try

xdotool key --clearmodifiers alt+shift

and then assigning this command to the Shift+Level3 Shift (don't type this, just press right_Shift + right_AltGr) shortcut in Custom Shortcuts. Again, plz refer to the relevant part of izx's answer.

Voilá, you're sorted.


Note that if you tried to do it "the other way around" like so

xdotool key --clearmodifiers ISO_Level3_Shift+Shift

(or alternatively, but less likely to work, Alt_R+Shift_R), noting that AltGr is "equivalent to" ISO_Level3_Shift,

you'd probaly get stuck, because "Alt_R+Shift_R" does not seem to be offered in the standard layout switching key dialog.


Further reading: from the xdotool man page:

Generally, any valid X Keysym string will work. Multiple keys are separated by '+'. Aliases exist for "alt", "ctrl", "shift", "super", and "meta" which all map to Foo_L, such as Alt_L and Control_L, etc.

also Remapping the AltGr key to Control with setxkbmap may be of interest to you...


Edit 2

See https://bugs.launchpad.net/ubuntu/+source/xdotool/+bug/696247 and http://code.google.com/p/semicomplete/issues/detail?id=13 ... and the workaround suggested therein (and confirmed to be working as of Nov 2012) of "The workaround, for now, is to use setxkbmap to force your keymap in xmodmap (setxkbmap dvorak, etc)". Thusly, plz refer to my 2nd, separate answer.


You can use Alt-Shift for both languages

System Settings... → Keyboard Layout → (click on "+" to Add new Language layouts) →

Options... → (click on) "Key(s) to change layout" → (tick) "Left Alt+Left Shift" → Close

enter image description here

enter image description here


Simply add this command as a startup application:

setxkbmap -option lv3:ralt_alt,grp:alt_shift_toggle us,ir

This should enable both Alt-Shift toggles. However, you will lose the compositing functionality of the right Alt key (e.g.: you won't be able to draw a Euro sign (€) by hitting RightAlt+E).

setxkbmap's options, including language codes, are documented under /usr/share/X11/xkb/rules/base.lst.

You could test the command out in a terminal first before adding it to your autostart applications.

Source: http://crunchbang.org/forums/viewtopic.php?id=11038 (post #7)