probonopd / LIVEstep

Experimental Live ISO based on FreeBSD with GNUstep and other components.
BSD 3-Clause "New" or "Revised" License
74 stars 12 forks source link

Set keyboard layout #14

Open probonopd opened 4 years ago

probonopd commented 4 years ago
user@FreeBSD$ setxkbmap -layout de -variant nodeadkeys -option ctrl:swap_lalt_lctl,caps:none 

This works for e.g., Gtk apps such as Chrome

BUT:

--> Is there a way to exclude those from the ctrl:swap_lalt_lctl?

--> Is there a way to change those labels?


Read more at: https://www.commandlinux.com/man-page/man7/xkeyboard-config.7.html

List of all supported languages: https://gist.github.com/jatcwang/ae3b7019f219b8cdc6798329108c9aee

Can also be seen from

/usr/local/share/X11/xkb/symbols/

Also check

ctrl:lctrl_meta Left Ctrl as Meta
ctrl:swapcaps   Swap Ctrl and Caps Lock
ctrl:ac_ctrl    At left of 'A'
ctrl:aa_ctrl    At bottom left
ctrl:rctrl_ralt Right Ctrl as Right Alt
ctrl:menu_rctrl Menu as Right Ctrl
ctrl:ctrl_ralt  Right Alt as Right Ctrl
ctrl:swap_lalt_lctl Swap Left Alt key with Left Ctrl key
ctrl:swap_lwin_lctl Swap Left Win key with Left Ctrl key
ctrl:swap_lalt_lctl_lwin    Left Alt as Ctrl, Left Ctrl as Win, Left Win as Alt

For Gnome:

dconf write "/org/gnome/desktop/input-sources/xkb-options" "['ctrl:swap_lalt_lctl','ctrl:swap_ralt_rctl']"

More information:

https://askubuntu.com/questions/885045/how-to-swap-ctrl-and-alt-keys-in-ubuntu-16-04

probonopd commented 4 years ago

image

import sys, os, subprocess
from PyQt5 import QtWidgets

class KeyboardSwitcher(object):

    def __init__(self):
        self.supported_layouts = self.getSupportedLayouts()
        self.basicWindow()

    def getSupportedLayouts(self):
        candidates = os.listdir("/usr/local/share/X11/xkb/symbols/")
        supported_layouts = []
        for candidate in candidates:
            if len(candidate) < 4:
                supported_layouts.append(candidate)
        supported_layouts.sort()
        supported_layouts.insert(0, "en")
        return supported_layouts

    def basicWindow(self):
        app = QtWidgets.QApplication(sys.argv)
        window = QtWidgets.QWidget()

        window.setWindowTitle('Keyboard Layout')

        layout = QtWidgets.QVBoxLayout()

        self.list_widget = QtWidgets.QListWidget()
        print(self.supported_layouts)
        for supported_layout in self.supported_layouts:
            self.list_widget.addItem(supported_layout)
        self.list_widget.itemSelectionChanged.connect(self.onSelectionChanged)
        layout.addWidget(self.list_widget)

        self.checkbox_keyswap = QtWidgets.QCheckBox()
        self.checkbox_keyswap.setText("Swap Ctrl and Alt keys")
        self.checkbox_keyswap.setChecked(True)
        self.checkbox_keyswap.clicked.connect(self.onSelectionChanged)
        layout.addWidget(self.checkbox_keyswap)

        self.checkbox_disablecapslock = QtWidgets.QCheckBox()
        self.checkbox_disablecapslock.setText("Disable Caps Lock key")
        self.checkbox_disablecapslock.setChecked(True)
        layout.addWidget(self.checkbox_disablecapslock)

        window.setLayout(layout)
        window.show()
        sys.exit(app.exec_())

    def onSelectionChanged(self):
        print("Selection changed")
        print(self.list_widget.selectedItems()[0].text())
        args = ["setxkbmap", '-layout', self.list_widget.selectedItems()[0].text(), "-variant", "nodeadkeys"]
        if self.checkbox_keyswap.isChecked() == True and self.checkbox_disablecapslock == False:
            args.append("-option", "ctrl:swap_lalt_lctl")
        elif self.checkbox_keyswap.isChecked() == False and self.checkbox_disablecapslock == True:
            args.append("-option", "caps:none")
        elif self.checkbox_keyswap.isChecked() == True and self.checkbox_disablecapslock == True:
            args.append("-option", "ctrl:swap_lalt_lctl,caps:none")
        out = subprocess.call(args)
        if out == 0:
            print("Changed keyboard layout successfully")
        else:
            print("Error setting keyboard layout")

if __name__ == "__main__":
    ks = KeyboardSwitcher()
probonopd commented 4 years ago

Can anyone beat me to this using ObjC?