Xfce Forum

Sub domains
 

You are not logged in.

#1 2017-01-14 18:51:32

booth
Member
Registered: 2016-05-11
Posts: 5

Simulating media keys with key combinations

Hi everyone,

I purchased a keyboard without media keys, and I've been trying to figure out how to map a key combination to simulate the pressing of the media keys, e.g. pressing Super+7 to simulate pressing XF86AudioNext. I've been trying to do this because I use the media keys with multiple media players (Clementine and Google Music Desktop), and I'd like the keys to work with both. This means that using the Settings > Keyboard > Application Shortcuts to map a combination to the command for one of those media players won't work for my situation. I've also tried to map the the volume controls directly to amixer through the keyboard settings using the associated CLI arguments, but I can never turn the volume completely to 0 using these key combinations, so I'd prefer to simulate those media keys (eg XF86AudioLowerVolume) as well. To achieve this, I've tried to use xdotool mapped to a key combination through settings > keyboard, for example:

command:

xdotool key --clearmodifiers XF86AudioLowerVolume

shortcut: Super+-

but this rarely actually works. I think it's because xdotool isn't always able to clear the modifying Super despite the --clearmodifiers flag, so the system is actually seeing Super+XF86AudioLowerVolume, which doesn't do anything.

I've also tried the method as described here: https://wiki.archlinux.org/index.php/Xb … media_keys but this only occasionally works even straight in the terminal:

echo 'KeyStrRelease Super_L KeyStrRelease minus' | xmacroplay :0; xmodmap -e 'remove Mod4 = Super_L'; echo 'KeyStrPress XF86AudioLowerVolume KeyStrRelease XF86AudioLowerVolume' | xmacroplay :0; xmodmap -e 'add Mod4 = Super_L'; echo 'KeyStrPress Super_L' | xmacroplay :0

I've also tried using autokey-gtk, but that crashes on start and is listed as unstable in the repository anyway.

I've been working on this and researching deep into forums for a couple days, so I'm wondering if any of you have some better ideas or if you've seen something in a forum somewhere  that I've missed.

FYI, I'm running Xubuntu 16.04

Thanks for the help.

Offline

#2 2017-01-15 05:23:42

ozjd
Member
From: Hawkesbury NSW Australia
Registered: 2012-02-05
Posts: 560
Website

Re: Simulating media keys with key combinations

For Clementine you need configure the keys in its Preferences-Configure Shortcuts.

Offline

#3 2017-01-15 09:15:58

rzi
Member
Registered: 2016-12-08
Posts: 9

Re: Simulating media keys with key combinations

Have a look at this: http://unix.stackexchange.com/questions … m-terminal

If you are using alsamixer

# increase by 3%
amixer -q sset Master 3%+

# decrease by 3%
amixer -q sset Master 3%-

If you are using pulseaudio

#increase volume
pactl set-sink-volume 0 +15%

Should be easy to map it to kbd shortcuts.

Offline

#4 2017-02-25 21:56:53

booth
Member
Registered: 2016-05-11
Posts: 5

Re: Simulating media keys with key combinations

Hi all,

I want to thank all of you for your help on this. Here's what ended up working for me:

Volume:

# increase volume by 5% 
amixer -D pulse sset Master 5%+

# decrease volume by 5%
amixer -D pulse sset Master 5%-

# mute/unmute audio
amixer -D pulse sset Master 1+ toggle

Media key simulations, which now works across all of my media players without any additional settings, are contained within separate shell scripts as follows:

audioPrev.sh

#! /bin/sh

#simulate XF86AudioPrev keypress with Super + 7 
echo 'KeyStrRelease Super_L KeyStrRelease 7' | xmacroplay :0; xmodmap -e 'remove Mod4 = Super_L'; echo 'KeyStrPress XF86AudioPrev KeyStrRelease XF86AudioPrev' | xmacroplay :0; xmodmap -e 'add Mod4 = Super_L'

audioNext.sh

#! /bin/sh

# Simulate XF86AudioNext keypress with Super + 9
echo 'KeyStrRelease Super_L KeyStrRelease 9' | xmacroplay :0; xmodmap -e 'remove Mod4 = Super_L'; echo 'KeyStrPress XF86AudioNext KeyStrRelease XF86AudioNext' | xmacroplay :0; xmodmap -e 'add Mod4 = Super_L'

audioPlay.sh

#! /bin/sh

# simulate XF86AudioPlay keypress for Play/Pause toggle with Super + 8
echo 'KeyStrRelease Super_L KeyStrRelease 8' | xmacroplay :0; xmodmap -e 'remove Mod4 = Super_L'; echo 'KeyStrPress XF86AudioPlay KeyStrRelease XF86AudioPlay' | xmacroplay :0; xmodmap -e 'add Mod4 = Super_L'

All of the above commands and scripts are mapped to keyboard shortcuts in [edit - thanks ozjd] Settings - Keyboard - Applications shortcuts. Obviously xmacroplay and xmodmap are needed if you don't have them. For reference I am running Xubuntu 16.04.2 with XFCE 4.12. I hope this helps someone in the future!

Last edited by booth (2017-02-25 22:24:38)

Offline

#5 2017-02-25 22:12:25

ozjd
Member
From: Hawkesbury NSW Australia
Registered: 2012-02-05
Posts: 560
Website

Re: Simulating media keys with key combinations

It would be better to put them in Settings - Keyboard - Applications shortcuts. That is a more universal location and would work for others who may not use Whisker Menu. Of course on your system you can do what ever you want but this may help that "someone in the future" smile

Offline

#6 2017-02-25 22:49:46

booth
Member
Registered: 2016-05-11
Posts: 5

Re: Simulating media keys with key combinations

ozjd wrote:

It would be better to put them in Settings - Keyboard - Applications shortcuts. That is a more universal location and would work for others who may not use Whisker Menu. Of course on your system you can do what ever you want but this may help that "someone in the future" smile

Good call, thanks. I changed it in the post above.

Offline

#7 2017-02-26 04:48:15

doodloo
Member
From: London
Registered: 2015-09-11
Posts: 59

Re: Simulating media keys with key combinations

@booth to make things simpler, you could rewrite your scripts to use arguments. For instance if a script named `simulate-key` exists in $PATH containing:

#!/usr/bin/env bash

echo "KeyStrRelease Super_L KeyStrRelease $1" | xmacroplay :0
xmodmap -e "remove Mod4 = Super_L"
echo "KeyStrPress $2 KeyStrRelease $2" | xmacroplay :0
xmodmap -e 'add Mod4 = Super_L'

You'll notice the usage of double quotes instead of single, to allow variable extrapolation in strings.
Then you can call it with the key you want to simulate. It would effectevelly decrease the number of scripts you're using to just one:

simulate-key 7 XF86AudioPrev

Hope it helps make the system more flexible wink

Last edited by doodloo (2017-02-26 04:49:11)

Offline

Registered users online in this topic: 0, guests: 1
[Bot] ClaudeBot

Board footer

Powered by FluxBB
Modified by Visman

[ Generated in 0.010 seconds, 7 queries executed - Memory usage: 547.08 KiB (Peak: 547.92 KiB) ]