Sure, this is what you should do:
Install wmctrl
sudo apt-get install wmctrl
Startup your virtual machine, write down exactly the window name as it appears in the title bar. Mine is for example:
Windows 7 [Draaiend] - Oracle VM VirtualBox
The command to bring the VM's window to the front is then:
wmctrl <window_name>
NB: if name of the window contains spaces, use quotes around the name
so in my case it would be:
wmctrl -a "Windows 7 [Draaiend] - Oracle VM VirtualBox"
Test the command running it in a terminal window
Choose: System Settings > "Keyboard" > "Shortcust" > "Custom Shortcuts".
Click the "+" and add the command:
wmctrl -a "Windows 7 [Draaiend] - Oracle VM VirtualBox"
to a key combinbation of your choice.
From man wmctrl
:
-a <WIN>
Switch to the desktop containing the window <WIN>, raise the
window, and give it focus.
In case you run the machine from different snapshots
or in any other case when the window name might be extended by other strings, as mentioned by OP in a comment: copy the script below into an empty file, save it as run_vm.py
. Run it by the command:
python3 /path/to/run_vm.py <machine_name>
where, like in the first example, you have to put the machine name in quotes if it contains spaces.
The script:
#!/usr/bin/env python3
import subprocess
import sys
machine = sys.argv[1]
command = "wmctrl -l"
wlist = [(" ").join(line.split(" ")[4:]) for line in subprocess.check_output(
["/bin/bash", "-c", command]
).decode("utf-8").split("\n") if machine in line][0]
command2 = "wmctrl -a "+"'"+wlist+"'"
subprocess.Popen(["/bin/bash", "-c", command2])