Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set keyboard layout #14

Open
probonopd opened this issue Sep 26, 2020 · 2 comments
Open

Set keyboard layout #14

probonopd opened this issue Sep 26, 2020 · 2 comments
Labels
enhancement New feature or request

Comments

@probonopd
Copy link
Owner

probonopd commented Sep 26, 2020

user@FreeBSD$ setxkbmap -layout de -variant nodeadkeys -option ctrl:swap_lalt_lctl,caps:none 

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

  • German
  • Alt and Ctrl swapped
  • Caps Lock disabled

BUT:

  • Ctl-Backspace becomes Alt-Backspace
  • Ctr-C, Ctrl-D becomes Alt-C, Alt-D
  • Does it mess up GNUstep apps which already have the "correct" shortcuts?

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

  • Shortcut labels in the menu are wrong, as they are still referring to Ctrl instead of Alt.

--> 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
Copy link
Owner Author

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
Copy link
Owner Author

Can anyone beat me to this using ObjC?

@probonopd probonopd added the enhancement New feature or request label Sep 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant