项目作者: mooz

项目描述 :
Yet another keyboard remapping tool for X environment
高级语言: Python
项目地址: git://github.com/mooz/xkeysnail.git
创建时间: 2018-01-02T11:22:47Z
项目社区:https://github.com/mooz/xkeysnail

开源协议:

下载


xkeysnail

xkeysnail is yet another keyboard remapping tool for X environment written in Python. It’s like
xmodmap but allows more flexible remappings.

screenshot

  • Pros
    • Has high-level and flexible remapping mechanisms, such as
      • per-application keybindings can be defined
      • multiple stroke keybindings can be defined such as Ctrl+x Ctrl+c to Ctrl+q
      • not only key remapping but arbitrary commands defined by Python can be bound to a key
    • Runs in low-level layer (evdev and uinput), making remapping work in almost all the places
  • Cons
    • Runs in root-mode (requires sudo)

The key remapping mechanism of xkeysnail is based on pykeymacs
(https://github.com/DreaminginCodeZH/pykeymacs).

Installation

Requires root privilege and Python 3.

Ubuntu

  1. sudo apt install python3-pip
  2. sudo pip3 install xkeysnail
  3. # If you plan to compile from source
  4. sudo apt install python3-dev

Fedora

  1. sudo dnf install python3-pip
  2. sudo pip3 install xkeysnail
  3. # Add your user to input group if you don't want to run xkeysnail
  4. # with sudo (log out and log in again to apply group change)
  5. sudo usermod -a -G input $USER
  6. # If you plan to compile from source
  7. sudo dnf install python3-devel

Manjaro/Arch

  1. # Some distros will need to compile evdev components
  2. # and may fail to do so if gcc is not installed.
  3. sudo pacman -Syy
  4. sudo pacman -S gcc

Solus

  1. # Some distros will need to compile evdev components
  2. # and may fail to do so if gcc is not installed.
  3. sudo eopkg install gcc
  4. sudo eopkg install -c system.devel

From source

  1. git clone --depth 1 https://github.com/mooz/xkeysnail.git
  2. cd xkeysnail
  3. sudo pip3 install --upgrade .

Usage

  1. sudo xkeysnail config.py

When you encounter the errors like Xlib.error.DisplayConnectionError: Can't connect to display ":0.0": b'No protocol specified\n', try

  1. xhost +SI:localuser:root
  2. sudo xkeysnail config.py

If you want to specify keyboard devices, use --devices option:

  1. sudo xkeysnail config.py --devices /dev/input/event3 'Topre Corporation HHKB Professional'

If you have hot-plugging keyboards, use --watch option.

If you want to suppress output of key events, use -q / --quiet option especially when running as a daemon.

How to prepare config.py?

(If you just need Emacs-like keybindings, consider to
use
example/config.py,
which contains Emacs-like keybindings)
.

Configuration file is a Python script that consists of several keymaps defined
by define_keymap(condition, mappings, name)

define_keymap(condition, mappings, name)

Defines a keymap consists of mappings, which is activated when the condition
is satisfied.

Argument condition specifies the condition of activating the mappings on an
application and takes one of the following forms:

  • Regular expression (e.g., re.compile("YYY"))
    • Activates the mappings if the pattern YYY matches the WM_CLASS of the application.
    • Case Insensitivity matching against WM_CLASS via re.IGNORECASE (e.g. re.compile('Gnome-terminal', re.IGNORECASE))
  • lambda wm_class: some_condition(wm_class)
    • Activates the mappings if the WM_CLASS of the application satisfies the condition specified by the lambda function.
    • Case Insensitivity matching via casefold() or lambda wm_class: wm_class.casefold() (see example below to see how to compare to a list of names)
  • None: Refers to no condition. None-specified keymap will be a global keymap and is always enabled.

Argument mappings is a dictionary in the form of {key: command, key2: command2, ...} where key and command take following forms:

  • key: Key to override specified by K("YYY")
  • command: one of the followings
    • K("YYY"): Dispatch custom key to the application.
    • [command1, command2, ...]: Execute commands sequentially.
    • { ... }: Sub-keymap. Used to define multiple stroke keybindings. See multiple stroke keys for details.
    • pass_through_key: Pass through key to the application. Useful to override the global mappings behavior on certain applications.
    • escape_next_key: Escape next key.
    • Arbitrary function: The function is executed and the returned value is used as a command.
      • Can be used to invoke UNIX commands.

Argument name specifies the keymap name. This is an optional argument.

Key Specification

Key specification in a keymap is in a form of K("(<Modifier>-)*<Key>") where

<Modifier> is one of the followings

  • C or Ctrl -> Control key
  • M or Alt -> Alt key
  • Shift -> Shift key
  • Super or Win -> Super/Windows key

You can specify left/right modifiers by adding any one of prefixes L/R.

And <Key> is a key whose name is defined
in key.py.

Here is a list of key specification examples:

  • K("C-M-j"): Ctrl + Alt + j
  • K("Ctrl-m"): Ctrl + m
  • K("Win-o"): Super/Windows + o
  • K("M-Shift-comma"): Alt + Shift + comma (= Alt + >)

Multiple stroke keys

When you needs multiple stroke keys, define nested keymap. For example, the
following example remaps C-x C-c to C-q.

  1. define_keymap(None, {
  2. K("C-x"): {
  3. K("C-c"): K("C-q"),
  4. K("C-f"): K("C-q"),
  5. }
  6. })

Checking an application’s WM_CLASS with xprop

To check WM_CLASS of the application you want to have custom keymap, use
xprop command:

  1. xprop WM_CLASS

and then click the application. xprop tells WM_CLASS of the application as follows.

  1. WM_CLASS(STRING) = "Navigator", "Firefox"

Use the second value (in this case Firefox) as the WM_CLASS value in your
config.py.

Example config.py

See example/config.py.

Here is an excerpt of example/config.py.

  1. from xkeysnail.transform import *
  2. define_keymap(re.compile("Firefox|Google-chrome"), {
  3. # Ctrl+Alt+j/k to switch next/previous tab
  4. K("C-M-j"): K("C-TAB"),
  5. K("C-M-k"): K("C-Shift-TAB"),
  6. }, "Firefox and Chrome")
  7. define_keymap(re.compile("Zeal"), {
  8. # Ctrl+s to focus search area
  9. K("C-s"): K("C-k"),
  10. }, "Zeal")
  11. define_keymap(lambda wm_class: wm_class not in ("Emacs", "URxvt"), {
  12. # Cancel
  13. K("C-g"): [K("esc"), set_mark(False)],
  14. # Escape
  15. K("C-q"): escape_next_key,
  16. # C-x YYY
  17. K("C-x"): {
  18. # C-x h (select all)
  19. K("h"): [K("C-home"), K("C-a"), set_mark(True)],
  20. # C-x C-f (open)
  21. K("C-f"): K("C-o"),
  22. # C-x C-s (save)
  23. K("C-s"): K("C-s"),
  24. # C-x k (kill tab)
  25. K("k"): K("C-f4"),
  26. # C-x C-c (exit)
  27. K("C-c"): K("M-f4"),
  28. # cancel
  29. K("C-g"): pass_through_key,
  30. # C-x u (undo)
  31. K("u"): [K("C-z"), set_mark(False)],
  32. }
  33. }, "Emacs-like keys")

Example of Case Insensitivity Matching

  1. terminals = ["gnome-terminal","konsole","io.elementary.terminal","sakura"]
  2. terminals = [term.casefold() for term in terminals]
  3. termStr = "|".join(str(x) for x in terminals)
  4. # [Conditional modmap] Change modifier keys in certain applications
  5. define_conditional_modmap(lambda wm_class: wm_class.casefold() not in terminals,{
  6. # Default Mac/Win
  7. Key.LEFT_ALT: Key.RIGHT_CTRL, # WinMac
  8. Key.LEFT_META: Key.LEFT_ALT, # WinMac
  9. Key.LEFT_CTRL: Key.LEFT_META, # WinMac
  10. Key.RIGHT_ALT: Key.RIGHT_CTRL, # WinMac
  11. Key.RIGHT_META: Key.RIGHT_ALT, # WinMac
  12. Key.RIGHT_CTRL: Key.RIGHT_META, # WinMac
  13. })
  14. # [Conditional modmap] Change modifier keys in certain applications
  15. define_conditional_modmap(re.compile(termStr, re.IGNORECASE), {
  16. # Default Mac/Win
  17. Key.LEFT_ALT: Key.RIGHT_CTRL, # WinMac
  18. Key.LEFT_META: Key.LEFT_ALT, # WinMac
  19. Key.LEFT_CTRL: Key.LEFT_CTRL, # WinMac
  20. Key.RIGHT_ALT: Key.RIGHT_CTRL, # WinMac
  21. Key.RIGHT_META: Key.RIGHT_ALT, # WinMac
  22. Key.RIGHT_CTRL: Key.LEFT_CTRL, # WinMac
  23. })

FAQ

How do I fix Firefox capturing Alt before xkeysnail?

In the Firefox location bar, go to about:config, search for ui.key.menuAccessKeyFocuses, and set the Value to false.

License

xkeysnail is distributed under GPL.

  1. xkeysnail
  2. Copyright (C) 2018 Masafumi Oyamada
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses></http:>.

xkeysnail is based on pykeymacs
(https://github.com/DreaminginCodeZH/pykeymacs), which is distributed under
GPL.

  1. pykeymacs
  2. Copyright (C) 2015 Zhang Hai
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses></http:>.