For instant key press feedback, the window.on_key_press event is the
easiest method.
If you want to check for held-in keys, maybe a KeyStateHandler would work.
http://pyglet.readthedocs.io/en/latest/programming_guide/keyboard.html#remembering-key-state
You could then make a function to query the key state:
from pyglet.window import key
window = pyglet.window.Window()
keys = key.KeyStateHandler()
window.push_handlers(keys)
def check_key_status(dt):
global keys
if keys[key.J]:
do_something()
pyglet.clock.schedule_interval(check_key_status, 1/60.0)
Would that work?
On Tuesday, July 4, 2017 at 2:27:15 PM UTC+9, 杨金骉 wrote:
>
> Thanks for your reply!
>
> - Is it necessary to create your own event loop?
> I built a toolbox for the psychological/neuroscience experiments. The
> pyglet.app.run() might confuse the user so I sidestepped it and made a flat
> way (e.g. http://expy.readthedocs.io/en/latest/quickstart/) for the
> coders of experiments.
>
> - My project
> I'm writing a function that enables the experiment participants to adjust
> the volume or pitch of the sound output by pressing keyboard
> continuously.
> In that way, I need the sound playing track and the keyboard sensor
> working in different threads.
> I'll be appreciated if you have the better solution!
>
>
> Best regards,
> Jinbiao Yang(杨金骉)
>
>
> -----------------------------------------------------------------------------
> Cell Phone: +86 13162513165
> Email: [email protected] <javascript:>
>
> Research Associate,
> NYU-ECNU Institute of Brain and Cognitive Science,
> NYU Shanghai,
> 3663 North Zhongshan Road, Shanghai, China 200062
>
> 2017-07-04 12:15 GMT+08:00 Benjamin Moran <[email protected]
> <javascript:>>:
>
>> I don't think win.dispatch_events() will work unless it's on the main
>> thread, but I haven't look at this code recently.
>> Is it necessary to create your own event loop? The Player class already
>> uses threads internally for playback.
>>
>> There is an example in the pyglet repositories that might be useful:
>>
>> https://bitbucket.org/pyglet/pyglet/src/aca0272d8f04e75f345272f8e1b5027ec8091586/examples/synthesizer.py?at=default&fileviewer=file-view-default
>>
>> If that doesn't help, could you descrbe your project more?
>>
>> On Monday, July 3, 2017 at 2:04:04 PM UTC+9, 杨金骉 wrote:
>>>
>>> No. I just used loop + dispatch_events() manually.
>>>
>>>
>>> On Jul 3, 2017 1:00 PM, "Benjamin Moran" <[email protected]> wrote:
>>>
>>>> Are you using pyglet.app.run() in your code?
>>>>
>>>>
>>>> On Sunday, July 2, 2017 at 11:29:12 PM UTC+9, 杨金骉 wrote:
>>>>>
>>>>> I'm building a real-time sound controller.
>>>>> I tried to put the sound player and detector of keypress in different
>>>>> threads, and used a variable as the messenger. Below is the demo code:
>>>>>
>>>>> ## detector
>>>>> def wait():
>>>>> while True:
>>>>> win.dispatch_events()
>>>>>
>>>>> shared.pressing = e['key'] # the got key
>>>>>
>>>>> if shared.figure_released:
>>>>> shared.pressing = None
>>>>>
>>>>> time.sleep(0.01)
>>>>>
>>>>>
>>>>> td = threading.Thread(target=wait)
>>>>> td.start()
>>>>>
>>>>> ## player
>>>>> while 1:
>>>>> if shared.pressing=='k':
>>>>> # pitch up
>>>>> elif shared.pressing=='j':
>>>>> # pitch down
>>>>>
>>>>> And then I got the error:
>>>>>
>>>>>> *RuntimeError: EventLoop.run() must be called from the same thread
>>>>>> that imports pyglet.app*
>>>>>
>>>>>
>>>>>
>>>>> Why can't pyglet get the events from another thread?
>>>>>
>>>> --
>>>> You received this message because you are subscribed to a topic in the
>>>> Google Groups "pyglet-users" group.
>>>> To unsubscribe from this topic, visit
>>>> https://groups.google.com/d/topic/pyglet-users/PrD2Z8O1qnI/unsubscribe.
>>>> To unsubscribe from this group and all its topics, send an email to
>>>> [email protected].
>>>> To post to this group, send email to [email protected].
>>>> Visit this group at https://groups.google.com/group/pyglet-users.
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "pyglet-users" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/pyglet-users/PrD2Z8O1qnI/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> [email protected] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> Visit this group at https://groups.google.com/group/pyglet-users.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/pyglet-users.
For more options, visit https://groups.google.com/d/optout.