Open probonopd opened 4 years ago
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()
Can anyone beat me to this using ObjC?
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
Also check
For Gnome:
More information:
https://askubuntu.com/questions/885045/how-to-swap-ctrl-and-alt-keys-in-ubuntu-16-04