Skip to content

Instantly share code, notes, and snippets.

@udnaan
Last active September 2, 2018 03:29
Show Gist options
  • Save udnaan/52a0d78a2eef311589ceae09640bc659 to your computer and use it in GitHub Desktop.
Save udnaan/52a0d78a2eef311589ceae09640bc659 to your computer and use it in GitHub Desktop.
Changes workspace on a gnome desktop (i.e Ubuntu). Written to be able to switch workspace while running vnc/remote desktop in fullscreen
#!/usr/bin/env python3
# Documentation for Wnck is at http://lazka.github.io/pgi-docs/#Wnck-3.0/classes/Workspace.html#Wnck.Workspace.get_number
# Documentation for keyboard is at https://github.com/boppreh/keyboard
import keyboard as kb
import gi
gi.require_version('Wnck', '3.0')
from gi.repository import Wnck, Gtk
Gtk.init([])
s = Wnck.Screen.get_default()
s.force_update() # required to fill the data into s. Without it, s.get_workspaces or s.get_windows returns and empty array
def ws_left(*args):
current = s.get_number() + 1
workspaces = s.get_workspaces()
if current == len(workspaces):
current = 0
workspaces[current].activate(11) # activate requires an arbitrary timestamp type int. No clue why
def ws_right(*args):
current = s.get_number() - 1
workspaces = s.get_workspaces()
if current < 0:
current = len(workspaces) - 1
workspaces[current - 1].activate(11) # activate requires an arbitrary timestamp type int. No clue why
kb.add_hotkey('ctrl+shift+left', ws_left)
kb.add_hotkey('ctrl+shift+right', ws_right)
print("Press Ctrl+ESC to stop.")
# print(kb.read_hotkey())
kb.wait('ctrl+esc')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment