Skip to content

Instantly share code, notes, and snippets.

@vkz
Created October 13, 2020 12:21
Show Gist options
  • Save vkz/2bf2c9ff60ea6676a0a686c954b0107f to your computer and use it in GitHub Desktop.
Save vkz/2bf2c9ff60ea6676a0a686c954b0107f to your computer and use it in GitHub Desktop.
Dual key behavior with xcape: modifier or keypress
# Space is Shift modifier when pressed with any other key, usual SPACE otherwise
# https://github.com/alols/xcape
# only works in X
# Map an unused modifier's keysym to the spacebar's keycode and make it a
# control modifier. It needs to be an existing key so that emacs won't
# spazz out when you press it. Hyper_L is a good candidate.
spare="Hyper_L"
xmodmap -e "keycode 65 = $spare"
xmodmap -e "remove mod4 = $spare" # hyper_l is mod4 by default
xmodmap -e "add Shift = $spare"
# Map space to an unused keycode (to keep it around for xcape to
# use).
xmodmap -e "keycode any = space"
# Finally use xcape to cause the space bar to generate a space when tapped.
xcape -e "$spare=space"
@vkz
Copy link
Author

vkz commented Oct 13, 2020

Proper way to do it that works both in X and console would probably require https://gitlab.com/interception/linux/plugins/dual-function-keys

From what I gather it should be possible to achieve the same with just XKB which maybe arcane but appears very flexible. It is however underdocumented (I'm being generous here). Crude notes from when I attempted that and failed:

#+begin_src shell
  # dump current keyboard configuration
  xkbcomp -xkb $DISPLAY mymap
#+end_src
now search for ~interpret~. If I had to guess these specify how to combine
certain keys and interpret them and these look like
#+begin_src C
  intepret Alt_R+AnyOf(all) {
    virtualModifier= Alt;
    action= ...
  }
#+end_src

then in later sections we have keys e.g.
#+begin_src C
  key <SPCE> {
    ...
  }
#+end_src

space is <SPCE> btw, so I'm thinking I should be able to create my own "layout"
from this base and then have xkbcomp or whateven load it for the default
$DISPLAY.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment