[Tutor] call key on_press event multiple times when key is held down

eryk sun eryksun at gmail.com
Tue Jul 4 15:55:51 EDT 2017

On Tue, Jul 4, 2017 at 8:50 AM, Carlton Banks <noflaco at gmail.com> wrote:
> I am using pynput <https://pypi.python.org/pypi/pynput> for keyboard events

You could use an event that enables a recording loop. The on_press and
on_release callbacks of the Listener [1] would set() and clear() this
event, respectively. For example:

    import threading
    from pynput import keyboard

    def main():
        do_record = threading.Event()

        def on_press(key):
            if key == keyboard.Key.cmd_l:

        def on_release(key):
            if key == keyboard.Key.cmd_l:

        with keyboard.Listener(on_press=on_press,
                               on_release=on_release) as listener:


            frames = []

            while do_record.is_set():
                print('Started recording')
                # record and append audio frame

            print('Stopped recording')

[1]: http://pythonhosted.org/pynput/keyboard.html#pynput.keyboard.Listener

More information about the Tutor mailing list