[Tutor] call key on_press event multiple times when key is held down
Joel Goldstick
joel.goldstick at gmail.com
Thu Jul 13 08:20:36 EDT 2017
On Thu, Jul 13, 2017 at 3:36 AM, Carlton Banks <noflaco at gmail.com> wrote:
> So i finally made it work..
> My error was caused in the callback function, which
> aborted the stream, hence didn’t record.
>
> This was the solution I ended with:
>
> https://pastebin.com/isW2brW2 <https://pastebin.com/isW2brW2>
>
>
Carlton, please don't post above the discusion -- post below or interleave
where appropriate. Also, paste your code in the message next time (which I
have done below)
>
> > Den 10. jul. 2017 kl. 11.10 skrev Alan Gauld via Tutor <tutor at python.org
> >:
> >
> > On 04/07/17 13:45, Carlton Banks wrote:
> >
> >> Any suggestion on any GUI solutions?
> >
> > Here is a Tkinter solution that increments a counter
> > while the mouse button is pressed. It should give you the idea...
> > Obviously you need to replace the counter increment
> > with your desired processing. And the print statements
> > are just to show the events being processed.
> >
> > ##################################
> > import tkinter as tk
> >
> > display = "count: "
> > flag = False
> > counter = 0
> >
> > def do_down(e):
> > global flag
> > print('Key down')
> > flag = True
> > after_loop()
> >
> > def do_up(e):
> > global flag
> > print('key up')
> > flag = False
> >
> > def after_loop():
> > print('looping')
> > global counter
> > counter += 1
> > l['text'] = display +str(counter)
> > if flag:
> > top.after(10,after_loop)
> >
> >
> > top = tk.Tk()
> > l = tk.Label(top,text=display+'0')
> > l.pack()
> > l.bind("<Button-1>",do_down)
> > l.bind("<ButtonRelease-1>",do_up)
> > top.mainloop()
> >
> > ###################################
> >
> > HTH
> > --
> > Alan G
> > Author of the Learn to Program web site
> > http://www.alan-g.me.uk/
> > http://www.amazon.com/author/alan_gauld
> > Follow my photo-blog on Flickr at:
> > http://www.flickr.com/photos/alangauldphotos
> >
> >
> > _______________________________________________
> > Tutor maillist - Tutor at python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
from pynput import keyboard
import time
import pyaudio
import wave
import math
CHUNK = 8192
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
WAVE_OUTPUT_FILENAME = "output.wav"
class MyListener(keyboard.Listener):
def __init__(self):
super(MyListener, self).__init__(on_press=self.on_press,
on_release=self.on_release)
self.key_pressed = None
def on_press(self, key):
if key == keyboard.Key.cmd_l:
self.p = pyaudio.PyAudio()
self.frames = []
self.stream = self.p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK,
stream_callback = self.callback)
print ("Stream active? " + str(self.stream.is_active()))
self.key_pressed = True
def on_release(self, key):
if key == keyboard.Key.cmd_l:
self.key_pressed = False
def callback(self,in_data, frame_count, time_info, status):
if self.key_pressed == True:
#stream_queue.put(in_data)
print("record")
self.frames.append(in_data)
return (in_data, pyaudio.paContinue)
elif self.key_pressed == False:
#stream_queue.put(in_data)
self.frames.append(in_data)
return (in_data, pyaudio.paComplete)
else:
return (in_data,pyaudio.paContinue)
class yesno_generator:
def __init__(self,pattern_length):
self.pattern_length = pattern_length
self.limit = math.pow(2,pattern_length)-1
self.step = 0
def begin(self):
if self.step =< self.limit:
def generate_patter(self):
pattern = bin(self.step)[2:].zfill(sef.length)
listener = MyListener()
listener.start()
started = False
while True:
time.sleep(0.1)
if listener.key_pressed == True and started == False:
started = True
listener.stream.start_stream()
print ("Start stream - Key is down")
# elif listener.key_pressed == True and started == True:
#print("stream has started and key is still down")
#print("Stream is active? " + str(listener.stream.is_active()))
#print("Stream is stopped? " + str(listener.stream.is_stopped()))
#print("Stream is time? " + str(listener.stream.get_time()))
elif listener.key_pressed == False and started == True:
print("Key has been released")
listener.stream.stop_stream()
listener.stream.close()
print("stream has been closed")
listener.p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(listener.p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(listener.frames))
wf.close()
started = False
--
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
More information about the Tutor
mailing list