Skip to content

Instantly share code, notes, and snippets.

@TheEpicFace007
Created January 25, 2022 15:16
Show Gist options
  • Save TheEpicFace007/e2ce7f09a0c78717c4f60a768cd4b278 to your computer and use it in GitHub Desktop.
Save TheEpicFace007/e2ce7f09a0c78717c4f60a768cd4b278 to your computer and use it in GitHub Desktop.
Hold the space bar to speak in microsoft teams.
import pynput
from pyautogui import click
from sys import platform
from rich.console import Console
from pynput import keyboard
from time import sleep
def get_active_window():
"""
Get the currently active window.
Returns
-------
string :
Name of the currently active window.
"""
import sys
active_window_name = None
if sys.platform in ['linux', 'linux2']:
# Alternatives: https://unix.stackexchange.com/q/38867/4784
try:
import wnck
except ImportError:
console.log("[red]wnck not installed[/red]")
wnck = None
if wnck is not None:
screen = wnck.screen_get_default()
screen.force_update()
window = screen.get_active_window()
if window is not None:
pid = window.get_pid()
with open("/proc/{pid}/cmdline".format(pid=pid)) as f:
active_window_name = f.read()
else:
try:
from gi.repository import Gtk, Wnck
gi = "Installed"
except ImportError:
console.log("[red]gi.repository not installed[/red]")
gi = None
if gi is not None:
Gtk.init([]) # necessary if not using a Gtk.main() loop
screen = Wnck.Screen.get_default()
screen.force_update() # recommended per Wnck documentation
active_window = screen.get_active_window()
pid = active_window.get_pid()
with open("/proc/{pid}/cmdline".format(pid=pid)) as f:
active_window_name = f.read()
elif sys.platform in ['Windows', 'win32', 'cygwin']:
# https://stackoverflow.com/a/608814/562769
import win32gui
window = win32gui.GetForegroundWindow()
active_window_name = win32gui.GetWindowText(window)
elif sys.platform in ['Mac', 'darwin', 'os2', 'os2emx']:
# https://stackoverflow.com/a/373310/562769
from AppKit import NSWorkspace
active_window_name = (NSWorkspace.sharedWorkspace()
.activeApplication()['NSApplicationName'])
else:
console.log("sys.platform={platform} is unknown. Please report."
.format(platform=sys.platform))
console.log(sys.version)
return active_window_name
def toggle_webcam():
mouse.position = (1171.953125, 24.8515625)
sleep(0.5)
click()
def toggle_mic():
mouse.position = (1218.21875, 23.41796875)
sleep(0.45)
click()
def on_press(key):
if get_active_window() != 'Microsoft Teams':
return
global mouse
try:
key_name = key.name
except AttributeError:
key_name = key.char
match key_name:
case 'space':
toggle_webcam()
sleep(1)
toggle_mic()
console.log('Toggling the webcam and the mic')
case 'f':
pos = mouse.position
console.print(pos)
console = Console()
mouse = pynput.mouse.Controller()
listener = keyboard.Listener(on_press=on_press)
if __name__ == '__main__':
console.print('''[#ff4500 b]
██████ ██▓███ ▄▄▄ ▄████▄ ▓█████ ▄▄▄█████▓ ▒█████ ██████ ██▓███ ▓█████ ▄▄▄ ██ ▄█▀
▒██ ▒ ▓██░ ██▒▒████▄ ▒██▀ ▀█ ▓█ ▀ ▓ ██▒ ▓▒▒██▒ ██▒ ▒██ ▒ ▓██░ ██▒▓█ ▀▒████▄ ██▄█▒
░ ▓██▄ ▓██░ ██▓▒▒██ ▀█▄ ▒▓█ ▄ ▒███ ▒ ▓██░ ▒░▒██░ ██▒ ░ ▓██▄ ▓██░ ██▓▒▒███ ▒██ ▀█▄ ▓███▄░
▒ ██▒▒██▄█▓▒ ▒░██▄▄▄▄██ ▒▓▓▄ ▄██▒▒▓█ ▄ ░ ▓██▓ ░ ▒██ ██░ ▒ ██▒▒██▄█▓▒ ▒▒▓█ ▄░██▄▄▄▄██ ▓██ █▄
▒██████▒▒▒██▒ ░ ░ ▓█ ▓██▒▒ ▓███▀ ░░▒████▒ ▒██▒ ░ ░ ████▓▒░ ▒██████▒▒▒██▒ ░ ░░▒████▒▓█ ▓██▒▒██▒ █▄
▒ ▒▓▒ ▒ ░▒▓▒░ ░ ░ ▒▒ ▓▒█░░ ░▒ ▒ ░░░ ▒░ ░ ▒ ░░ ░ ▒░▒░▒░ ▒ ▒▓▒ ▒ ░▒▓▒░ ░ ░░░ ▒░ ░▒▒ ▓▒█░▒ ▒▒ ▓▒
░ ░▒ ░ ░░▒ ░ ▒ ▒▒ ░ ░ ▒ ░ ░ ░ ░ ░ ▒ ▒░ ░ ░▒ ░ ░░▒ ░ ░ ░ ░ ▒ ▒▒ ░░ ░▒ ▒░
░ ░ ░ ░░ ░ ▒ ░ ░ ░ ░ ░ ░ ▒ ░ ░ ░ ░░ ░ ░ ▒ ░ ░░ ░
░ ░ ░░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░░ ░
[/#ff4500 b]
''')
listener.start()
listener.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment