0

I'm looking for a way to create keyboard shortcuts in Linux using backtick (`) / tilde (~) key and some other key. In an ideal case:

  1. pressing tilde down does nothing
  2. pressing another key while the tilde is pressed down triggers a (customizable) shortcut
  3. when releasing the tilde before/without pressing another key, just send the tilde keystroke.

I have something similar in AutoHotKey for Windows, and i've been searching for a way to recreate this in a (any) Linux environment. I would consider using any GUI if i could get this working, but of course a more "general" solution would be even better.

burt
  • 41
  • 4

3 Answers3

1

I think i finally got it!!

I use xmodmap to turn the grave key into the modifier Hyper_L, and XCape to send the grave if the key is released without another key being pressed.

Xcape was intended to open the app menu ("Start menu") when the meta-key ("windows key") is pressed and released without another key, so as an added bonus, it does that too. Meaning you can both use Meta as a modifier, like Meta-F to open the file manager AND use the meta-key seperately to open the whiskers menu.

If all is right, you can open the keyboard settings manager using ~-k, and you can make new shortcuts using the ~-key. Because that's still tiresome and not easily portable between different systems, i've included some shortcuts using xfconf-query, which will probably only work in Xfce.

Here's the basics of my script:

#!/bin/sh

# reset pretty much ALL keyboard settings 
setxkbmap

# Free up the mod3 and mod4 flags from all keys it may be associated with:
xmodmap -e "clear mod3"
xmodmap -e "clear mod4"

# Add Hyper_L to the grave key (49)
xmodmap -e "keycode 49 = Hyper_L asciitilde grave asciitilde"

# You need a grave key somewhere else (!) so, bind it to an unused key:
xmodmap -e "keycode 250 = grave"

# Restore Mod4 but without Hyper_L (which was at location 4)
xmodmap -e "add mod4 = Super_L Super_R Super_L"

# Assign the mod3 to Hyper_L:
xmodmap -e "add mod3 = Hyper_L"

dist=100
/usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>Right -s "xdotool mousemove_relative -- $dist 0" --create -t string
/usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>Down  -s "xdotool mousemove_relative -- 0 $dist" --create -t string
/usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>Left  -s "xdotool mousemove_relative -- -$dist 0" --create -t string
/usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>Up    -s "xdotool mousemove_relative -- 0 -$dist" --create -t string
/usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>space -s "xdotool click 1" --create -t string

/usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>k -s "xfce4-keyboard-settings" --create -t string

# (re)starting xcape to produce a ` after key-up if no other key was pressed
killall xcape
xcape -t5000 -e "#49=grave;Super_L=Control_L|Escape" &

A more extended version of the script, with some more shortcuts can be found here.

burt
  • 41
  • 4
  • This works for me but when I pres shift+grave I get both a tilde and the backtick character, like this: ~` . Any ideas? – stonea Apr 17 '21 at 15:03
0

I am not sure if it will work out for you but you should check:

Both these tools let you create some custom actions and shortcuts.

Here is an example of xdotool: https://askubuntu.com/questions/212154/create-a-custom-shortcut-that-types-clipboard-contents

Hope it helps and good luck :)

Bruno

Community
  • 1
  • 1
0

After switching to Ubuntu on another machine, I also wanted to use tilde as a modifier key the way I use it in my AHK scripts.

I did quite some research on different tools for ex. xdotool, xev, autotools, xbindkeys and so on and finally found a solution to that. Here are the steps.

  1. Install Autokey, python, python evded module, xte(sudo apt-get install xautomation).
  2. Read a little about Autokey and how it launches python scripts or create hotstrings. In Autokey, we can set a hotkey to call a python script. So you can assign the python script down below to your tilde key or for that matter whatever custom hotkey you plan to create.
  3. Here is the required custom functionality(thought not yet completely ported to linux, I have it scripted in autohotkey and just love it. It keeps the hand glued to keyboard ;))
    • Tilde + Up arrow: Move mouse pointer upwards by 100 positions
    • Tilde + Down arrow: Move mouse pointer downwards by 100 positions
    • Tilde + Right arrow: Move mouse pointer to right by 100 positions
    • Tilde + Left arrow: Move mouse pointer to left by 100 positions
    • Tilde + Enter: Left Mouse click (not present in the python script)
    • Tilde + Alt + Enter: Right Mouse Click
  4. I use the tilde (KEY_GRAVE) as my modifier key. When this key is pressed, Autokey launches the python script. The scripts runs a loop until the tilde key is released. In the loop, the script keeps on detecting the keyboard inputs. On a UP arrow key(KEY_UP) press, the script sends a command to move a mouse by relative position (0, -100) utilizing 'xte' and so on. from evdev import InputDevice, categorize, ecodes from select import select dev = InputDevice('/dev/input/event4') releasekey = False while releasekey==False: r,w,x = select([dev], [], []) for event in dev.read(): if event.type == ecodes.EV_KEY: #system.exec_command("xte 'mousermove 0 3'", False) #break if event.code == ecodes.KEY_UP: if event.value == 1: system.exec_command("xte 'mousermove 0 -100'", False) if event.code == ecodes.KEY_DOWN: if event.value == 1: system.exec_command("xte 'mousermove 0 100'", False) if event.code == ecodes.KEY_RIGHT: if event.value == 1: system.exec_command("xte 'mousermove 100 0'", False) if event.code == ecodes.KEY_LEFT: if event.value == 1: system.exec_command("xte 'mousermove -100 0'", False)
    if event.code == ecodes.KEY_GRAVE: if event.value == 0: releasekey = True break
  5. You have to adjust the dev = InputDevice('/dev/input/event4') line to assign the correct name of your keyboard. In my case, event4 is my keyboard. Yours might be different. You can check out the handy tutorial "Reading Events" on python-evdev. That code actually outputs the name of your keyboard listed under /dev/input. Actually, my script is an extention to that tutorial script.
  6. The only problem is that the python script must be started as a root user otherwise the keyboard input device can not be accessed. You can overcome this by creating a udev rule file which changes the permission of the device to make it available for reading writing eg. create a rule file and add this line KERNEL=='event4', MODE="0660" and load the rule . At the end you must add yourself in the GROUP which have read/write permission for the device. The information regarding file permission can be found using ls -la in the /dev/input folder.

I hope it works for you. In it doesn´t work at first go, then get a cup of coffee and fight on till it works ;)

bzed
  • 119
  • 4
  • Thanks so much, i ended up using a different approach, but you sure helped my quest a lot with this! – burt Jan 18 '17 at 01:25