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

Peter Otten __peter__ at web.de
Mon Jul 10 04:54:13 EDT 2017


Carlton Banks wrote:

> So i tried a different solution, introducing two threads one handling the
> keyboard, and the other one is a while true, that keeps running.
> 
> https://pastebin.com/U0WVQMYP <https://pastebin.com/U0WVQMYP>
> 
> but for some reason, am I constantly running into IOerror, hence
> no frames is being recorded.  Any suggestion on why?

Please remember to always provide the traceback.

> while True:
>     time.sleep(0.1)
>     if listener.key_pressed == True and started == False:
>         started = True
>         listener.stream.start_stream()
>         print "start Stream"
> 
>     elif listener.key_pressed == False and started == True:
>         print "Something coocked"
>         listener.stream.stop_stream()

Did you get a non-empty output.wav?

If yes, my guess is that you can record something the first time you press 
the key, and when you release it and close the audio input with

>         listener.stream.close()

you see the IOError on the second attempt to record something.

>         p.terminate()


As you are only prepared to record once and another record would overwrite 
the file anyway (at least that's what I suppose
 
>         wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')

to do) perhaps you can omit the while-True loop altogether. Or you omit the 
stream.close() call until after that loop (though there seems to be no way 
to exit it cleanly at the moment).

Note that these are just ideas, I'm not familiar with pyaudio, or pynput, or 
wave.

>         wf.setnchannels(CHANNELS)
>         wf.setsampwidth(p.get_sample_size(FORMAT))
>         wf.setframerate(RATE)
>         wf.writeframes(b''.join(frames))
>         wf.close()
> 
>         started = False
> 






More information about the Tutor mailing list