Skip to content

Instantly share code, notes, and snippets.

@batica81
Created March 14, 2022 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save batica81/cf0b8e4f2d3804ea0b95c7d2d2434fd7 to your computer and use it in GitHub Desktop.
Save batica81/cf0b8e4f2d3804ea0b95c7d2d2434fd7 to your computer and use it in GitHub Desktop.
Plays a tone when some of specified keys are pressed.
# App plays a tone when some of specified keys are pressed. It works in the background as well.
# Best used with two sound cards (for Morse practice Zoom sessions for example).
# TODO: Add cosine shaping and remove clicking noise.
# Requirements:
# pip3 install pynput
# pip3 install pysinewave
from time import sleep
from pynput import keyboard
from pysinewave import SineWave
sinewave = SineWave(pitch = 17, decibels = -30) # Set to 692 Hz. Play with these two parameters for different tone.
def on_press(key):
if (str(key) in ["Key.ctrl_r", "Key.space", "Key.right"]):
sinewave.play()
def on_release(key):
sinewave.stop()
if key == keyboard.Key.esc:
# Stop listener
return False
# Collect events until released
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
sleep(0.01)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment