How to assign keyboard shortcuts to a VM?

66 views
Skip to first unread message

Michael Singer

unread,
Jun 21, 2021, 12:03:40 PM6/21/21
to qubes...@googlegroups.com
Dear Qubes community,

I am looking for a way to execute a command with a keyboard shortcut. For example, the command "screenshot" should start the screenshot program within the respective VM or the command "nautilus" should pop up a file browser window of the VM that is currently being worked with.

If I set up a keyboard combination for this in dom0 in the xfce keyboard settings, then I would have to set a different keyboard combination for each Qube:

Ctrl+Alt+1 for Qube1: qvm-run Qube1 nautilus
Ctrl+Alt+2 for Qube2 qvm-run Qube2 nautilus

So, how can I do the same thing at the qube level instead of in dom0?

You can install the autokey program, but it requires me to write a python script, and I don't know how to express a simple shell command in python.

All the best
Michael Singer

Sven Semmler

unread,
Jun 22, 2021, 8:15:37 PM6/22/21
to qubes...@googlegroups.com
Hi Michael,

I don't have the time currently to figure this out and send you a
working solution. So instead I'll share things I know:

1) if you inquire the window property "_QUBES_VMNAME" of the active
window, you'll get the name of the qube

2) you should be able to get to this with wmctrl or maybe xdotool

Then write a little bash script in dom0 and hook it up to a shortcut, is
what I think.

Good luck!

/Sven


--
public key: https://www.svensemmler.org/2A632C537D744BC7.asc
fingerprint: DA59 75C9 ABC4 0C83 3B2F 620B 2A63 2C53 7D74 4BC7

OpenPGP_signature

unman

unread,
Jun 23, 2021, 6:18:59 AM6/23/21
to qubes...@googlegroups.com
On Tue, Jun 22, 2021 at 07:15:22PM -0500, Sven Semmler wrote:
> Hi Michael,
>
> I don't have the time currently to figure this out and send you a working
> solution. So instead I'll share things I know:
>
> 1) if you inquire the window property "_QUBES_VMNAME" of the active window,
> you'll get the name of the qube
>
> 2) you should be able to get to this with wmctrl or maybe xdotool
>
> Then write a little bash script in dom0 and hook it up to a shortcut, is
> what I think.
>
> Good luck!
>
> /Sven

Here's a script that does exactly this.
Edit it to do something with $QUBE.
Associate it with a shortcut.

```
#!/bin/bash
ID=`xdotool getwindowfocus`
QUBE=`xprop _QUBES_VMNAME -id $ID|cut -f2 -d\" `
if [[ "$QUBE" == "_QUBES_VMNAME: not found." ]]; then
exit
else
# Do something with $QUBE
fi
```

The if clause is to exclude dom0 windows, but you could adapt that if
you *do* want action in dom0.

unman

Michael Singer

unread,
Jun 27, 2021, 11:41:50 AM6/27/21
to qubes...@googlegroups.com
Dear unman,

thank you very much for your help (and thank you Sven for being so cooperative). It works fantastic. So, for example, if we wanted to give a user instructions on how to start the screenshot tool via keyboard shortcut in any active Qube (assuming the tool is installed there), it would be like this:

1) Open xterm in dom0
2) Type "cd /home/USERNAME/Documents" and hit enter if you want your script to be saved there.
3) Type "nano Screenshot.sh" and press enter.
4) Type the following script:

#!/bin/bash
ID=`xdotool getwindowfocus`
QUBE=`xprop _QUBES_VMNAME -id $ID|cut -f2 -d\" `
if [[ "$QUBE" == "_QUBES_VMNAME: not found." ]]; then
exit
else
qvm-run $QUBE "gnome-screenshot -a"
fi

5) Press Ctrl+x, then y, then Enter.
6) Make the script executable by typing "chmod +x /home/USERNAME/Documents/Screenshot.sh" and pressing Enter.
7) Open System Tools > Keyboard > Application Shortcuts (this works in this way as long as you don't have the KDE desktop installed in dom0). Click "add". Type the path to your script: /home/USERNAME/Documents/Screenshot.sh
Click "OK" and press a keyboard shortcut.
8) Now you are able to invoke the screenshot tool with your keyboard shortcut in the Qube you are working with and drag a frame with the mouse over what you want to take a picture of.* The pictures will be automatically saved in the folder "Pictures" in the home directory of the Qube.

*If you want the whole screen to be photographed automatically instead, change the line "qvm-run $QUBE "gnome-screenshot -a" to "qvm-run $QUBE "gnome-screenshot" You can find out more possibilities in the terminal of the Qube with the command "gnome-screenshot --help".

If it does not work, the program "gnome-screenshot" is not installed. Open a terminal of the template and install the program with "sudo dnf install gnome-screenshot" if it is a normal fedora template. If it is a Debian template, type "sudo apt-get install gnome-screenshot". After installation, shut down the template and restart the Qubes based on the template.

Sven Semmler

unread,
Aug 11, 2021, 8:09:13 PM8/11/21
to qubes...@googlegroups.com
Just to riff on this ...

I made a bash script called qvm-run-in-focused-vm that accepts one command as parameter and then uses the structure @unman showed to run that command either in dom0 or the VM that has the focus.

Then I hooked up the following XFCE shortcuts:

Ctrl+Enter --> qvm-run-in-focused-vm xterm
Ctrl+Shift+Enter --> qvm-run-in-focused-vm nautilus
Ctrl+Shift+W --> qvm-run-in-focused-vm "sudo poweroff"

So much goodness! ;-)
OpenPGP_signature
Reply all
Reply to author
Forward
0 new messages