Skip to content

Commit

Permalink
Add more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
boppreh committed Jun 7, 2017
1 parent a692a7f commit 0d01d54
Show file tree
Hide file tree
Showing 4 changed files with 684 additions and 0 deletions.
16 changes: 16 additions & 0 deletions examples/pressed_keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
Prints the scan code of all currently pressed keys.
Updates on every keyboard event.
"""
import sys
sys.path.append('..')
import keyboard

def print_pressed_keys(e):
line = ', '.join(str(code) for code in keyboard._pressed_events)
# '\r' and end='' overwrites the previous line.
# ' '*40 prints 40 spaces at the end to ensure the previous line is cleared.
print('\r' + line + ' '*40, end='')

keyboard.hook(print_pressed_keys)
keyboard.wait()
27 changes: 27 additions & 0 deletions examples/push_to_talk_ubuntu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#quick and dirty push-to-talk example for Ubuntu 16.04, by Abd Azrad

import keyboard
import subprocess

is_muted = False

def unmute():
global is_muted
if not is_muted: # if mic is already enabled
return # do nothing
is_muted = False
subprocess.call('amixer set Capture cap', shell=True) # unmute mic

def mute():
global is_muted
is_muted = True
subprocess.call('amixer set Capture nocap', shell=True) # mute mic

if __name__ == "__main__":
is_muted = True
mute() # mute on startup

keyboard.add_hotkey('win', unmute) # unmute on keydown
keyboard.add_hotkey('win', mute, trigger_on_release=True) # mute on keyup

keyboard.wait() # wait forever
18 changes: 18 additions & 0 deletions examples/record_and_replay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
Prints lines with JSON object for each keyboard event, and reads similar events
from stdin to simulate events. Example:
{"event_type": "down", "name": "a", "scan_code": 30, "time": 1491442622.6348252}
{"event_type": "down", "name": "s", "scan_code": 31, "time": 1491442622.664881}
{"event_type": "down", "name": "d", "scan_code": 32, "time": 1491442622.7148278}
{"event_type": "down", "name": "f", "scan_code": 33, "time": 1491442622.7544951}
{"event_type": "up", "name": "a", "scan_code": 30, "time": 1491442622.7748237}
{"event_type": "up", "name": "s", "scan_code": 31, "time": 1491442622.825077}
{"event_type": "up", "name": "d", "scan_code": 32, "time": 1491442622.8644736}
{"event_type": "up", "name": "f", "scan_code": 33, "time": 1491442622.9056144}
"""
import sys
sys.path.append('..')

# Also available as just `python -m keyboard`.
from keyboard import __main__
Loading

0 comments on commit 0d01d54

Please sign in to comment.