You are not logged in.

#1 2018-10-23 10:23:39

myak
Member
Registered: 2018-10-23
Posts: 6

[Solved] Using Menu key as modifier for shortcuts

I would like to use my Menu key (keycode 135, the context menu key next to Super_R) to emulate XF86AudioMute and other multimedia keys when it's pressed with other keys, like Menu + Pause = XF86AudioMute, Menu + PgUp = XF86AudioRaiseVolume, etc. But, when the key is pressed on its own, I would still like to invoke its original function, the context menu. Sort of like when you press Super on its own, and it brings the launcher right up, but you can set it up as a modifier key to create shortcut combinations.

I'm using KDE and I try to set something like that up in global shortcuts, I can't because whenever I press the Menu key, it is assigned as the only key, and does not act as a modifier. My best guess is I need to remap it somehow with xmodmap. I've tried the following:

clear mod3
keycode 135 = Hyper_R
add mod3 = Hyper_R

But even though the key no longer acts as Menu, it still won't let me use it as a modifier key in KDE, and it shows some garbage character. If I remap to mod4 then it just acts as regular Super. Is there any way to achieve what I want?

Last edited by myak (2018-10-23 23:04:02)

Offline

#2 2018-10-23 23:03:49

myak
Member
Registered: 2018-10-23
Posts: 6

Re: [Solved] Using Menu key as modifier for shortcuts

So, I managed to somehow work around this. KDE alone, being based on Qt, does not support Hyper keys as modifiers, because Qt doesn't. The solution with just remapping the key to Hyper_R might work for other DEs that would allow it, then you just map the Hyper+Pause to mute using whatever capabilities the DE has. One thing left to do would be to have the key still act as menu when pressed alone. This can be achieved using xcape (also in repos):

xmodmap -e "keycode 135 = Hyper_R"
xmodmap -e "add mod3 = Hyper_R"
xmodmap -e "keycode any = Menu" # this is needed because xcape won't allow mapping a keysym that isn't linked to a keycode
xcape -e "Hyper_R=Menu"

Now, back to KDE. autokey (autokey-py3) allows using Hyper as a modifier key. It also allows sending keycodes, but

keyboard.fake_keypress("<code121>")

(121 being the regular keycode for XF86AudioMute )
was to slow, because of how autokey releases modifiers. So I came up with the following script;

key = 'XF86AudioMute'
mod = 'Pause'
cmd = 'xdotool keyup Hyper_R keyup %s key --clearmodifiers %s keydown Hyper_R key Control' % (mod, key)
system.exec_command(cmd)

Somehow --clearmodifiers didn't work, as in KDE wasn't detecting the keypress alone. So given this script is invoked with Hyper+Pause, both these keys have to be keyup'ed first, and only then is the desired XF86Audio* key sent. Then keydown Hyper_R is sent again to allow repeating the shortcut key (e.g. PgUp for volume up, so you can press PgUp multiple times while holding Menu). Then Control is sent to prevent xcape responding to fake keydown at the end and real keyup. Et voila.

Offline

#3 2018-10-24 10:01:28

myak
Member
Registered: 2018-10-23
Posts: 6

Re: [Solved] Using Menu key as modifier for shortcuts

Actually, autokey is not necessary, and  wastes a lot of CPU (if you're not using other autokey features that is). It can be achieved using xbindkeys alone, very similarly to autokey. In .xbindkeysrc:

"xdotool keyup Hyper_R keyup Pause key --clearmodifiers XF86AudioMute keydown Hyper_R key Control"
    Mod3 + Pause

Offline

Board footer

Powered by FluxBB