Skip to content

Instantly share code, notes, and snippets.

@leh
Created November 22, 2012 20:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leh/4132740 to your computer and use it in GitHub Desktop.
Save leh/4132740 to your computer and use it in GitHub Desktop.
This python script allows you to change the unity/gnome keyboard settings for swapping the meta (windows) and alt keys. Should work with Python 2.7 and 3.2
#!/usr/bin/python
# This python script allows you to change the unity/gnome keyboard
# settings for swapping the meta (windows) and alt keys. Should work with
# Python 2.7 and 3.2
from gi.repository import Gio
class KeySwapper:
SWAP_ALT_WIN='altwin\taltwin:swap_lalt_lwin'
def __init__(self, quiet):
self.settings = Gio.Settings.new("org.gnome.libgnomekbd.keyboard")
self.quiet = quiet
def inspect(self):
print(self.__get_options())
def enable_swap(self):
options = self.__get_options()
if self.SWAP_ALT_WIN not in options:
options.append(self.SWAP_ALT_WIN)
self.__set_options(options)
if not self.quiet:
print("enabled")
def disable_swap(self):
options = self.__get_options()
if self.SWAP_ALT_WIN in options:
options.remove(self.SWAP_ALT_WIN)
self.__set_options(options)
if not self.quiet:
print("disabled")
def toggle_swap(self):
if self.SWAP_ALT_WIN in self.__get_options():
self.disable_swap()
else:
self.enable_swap()
def __get_options(self):
return self.settings.get_strv("options")
def __set_options(self, options):
self.settings.set_strv("options", options)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='Changes the Unity/Gnome keyboard configuration for key swapping of the left meta and alt key.\n With no argument the configuration gets toggled.')
parser.add_argument('--enable', dest='swap', action='store_const', const="enable", help="Enables the key swapping")
parser.add_argument('--disable', dest='swap', action='store_const', const="disable", help="Disable the key swapping")
parser.add_argument('--quiet', dest='config', action='store_const', const="quiet", help="Mutes any output")
args = parser.parse_args()
quiet = args.config == "quiet"
key_swapper = KeySwapper(quiet)
if args.swap == "enable":
key_swapper.enable_swap()
elif args.swap == "disable":
key_swapper.disable_swap()
else:
key_swapper.toggle_swap()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment