Skip to content

Instantly share code, notes, and snippets.

@cb109
Last active March 19, 2020 13:47
Show Gist options
  • Save cb109/7d3d4ad6d3a3267ac0e1ced64cc9982e to your computer and use it in GitHub Desktop.
Save cb109/7d3d4ad6d3a3267ac0e1ced64cc9982e to your computer and use it in GitHub Desktop.
Switch virtual desktops in Windows 10 with hot corners
"""
Toggle between all virtual desktops.
Usage:
$ python cycle_virtualdesktops.py
$ python cycle_virtualdesktops.py left
# Installation
This describes a way to setup hot corners (aka active corners) on Windows 10
that can be used to switch to the next available virtual desktop quickly.
## Steps
1. Download cycle_virtual_desktops.py
2. Compile it to an .exe file using PyInstaller:
- (Inside a virtualenv do) $ pip install pyinstaller
- (Inside a virtualenv do) $ pyinstaller cycle_virtual_desktops.py
3. Make sure the resulting cycle_virtual_desktops.exe is available via PATH
(I can recommend RapidEnvironmentEditor to set that up)
4. Install and compile VirtualDesktop.exe as described at: https://github.com/MScholtes/VirtualDesktop
5. Make sure the VirtualDesktop.exe is available via PATH
6. Install and run WinXCorners: https://github.com/vhanla/winxcorners
7. Go to 'Advanced Options' of WinXCorners and configure a custom command: cycle_virtual_desktops.exe
8. Add that custom command to any hot corners as you wish. Done!
"""
import subprocess
import sys
VIRTUALDESKTOP_BINARY = "VirtualDesktop.exe"
def run_command(*options):
# Configure subprocess to hide the console window.
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
return subprocess.call(options, startupinfo=startupinfo)
def cycle_virtual_desktops(direction):
desktops_count = run_command(VIRTUALDESKTOP_BINARY, "/Count")
current_desktop_id = run_command(VIRTUALDESKTOP_BINARY, "/GetCurrentDesktop")
if direction == "right":
next_desktop_id = (
0 if current_desktop_id == desktops_count - 1 else current_desktop_id + 1
)
elif direction == "left":
next_desktop_id = (
desktops_count - 1 if current_desktop_id == 0 else current_desktop_id - 1
)
run_command(VIRTUALDESKTOP_BINARY, f"/Switch:{next_desktop_id}")
if __name__ == "__main__":
args = sys.argv[1:]
try:
direction = args[0]
assert direction in ("left", "right")
except (IndexError, AssertionError):
direction = "right"
cycle_virtual_desktops(direction)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment