06 Sep 2022
Library resources | |
---|---|
PyPI | https://pypi.org/project/pynput/ |
Github | https://github.com/moses-palmer/pynput |
Documentation | https://pynput.readthedocs.io/en/latest/ |
pip3 install pynput
Snippets
keyboard automation
from pynput.keyboard import Key, Controller
keyb = Controller()
with keyb.pressed(Key.cmd):
keyb.press('f')
keyb.release('f')
see which key is being pressed
from pynput.keyboard import Key, Listener
def show(key):
print('\nYou Entered {0}'.format( key))
if key == Key.delete:
# Stop listener
return False
# Collect all event until released
with Listener(on_press = show) as listener:
listener.join()
10 Oct 2023
switch between windows
from pynput.keyboard import Key, Controller
keyb = Controller()
with keyb.pressed(Key.cmd):
keyb.press(Key.tab)
keyb.release(Key.tab)
Make sure to print after switching, eg.:
webbrowser.get('chrome').open_new_tab(x.url)
with keyb.pressed(Key.cmd):
keyb.press(Key.tab)
keyb.release(Key.tab)
keep = input('\nKeep (y/n)?\n> ')