Is there a simple way to measure the time for which a key is held
down? I know how to use event.getKeys() to record the time of a
keypress, but I'm interested in recording the interval between key
down and key up, and can't work out what to do.
If it's any easier, I'd be just as interested in recording the time
that a mouse button is pressed.
I unfortunately don't have a specialized button box etc, so I
appreciate that any timing of keypresses etc will be error-prone.
Thanks for your help,
Will
http://www.pyglet.org/doc/api/pyglet.window.key.KeyStateHandler-class.html
the key state handler lets you know when a key is released.
pyglet docs show this example:
====================
Simple handler that tracks the state of keys on the keyboard. If a key
is pressed then this handler holds a True value for it.
For example:
>>> win = window.Window
>>> keyboard = key.KeyStateHandler()
>>> win.push_handlers(keyboard)
# Hold down the "up" arrow...
>>> keyboard[key.UP]
True
>>> keyboard[key.DOWN]
False
===================
The pyglet window.Window object will be the same as the psychopy Window,
I believe. You would want to block all other processing activity
probably, while you poll the state of the keyboard, to be sure you have
the most accurate time:
# wait for the key (up-arrow in this example) to be pressed:
# (pyglet has constants defined for all the keys)
while not keyboard(key.UP):
... pass
# immediately after key is pressed:
t1=time() # or use psychopy Clock
while keyboard(key.UP):
... pass # stop everything until up-arrow key is released
t2=time()
elapsedtime=t2-t1
If you need to do other stuff while the key is being held, you will have
to write a more complex handler that attaches a callback to the keyup
event to capture the time when the pyglet event loop notices the key is
released. (No longer a simple way.)
-Dave
--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/psychopy-users/-/Qzc4_RqAxZoJ.
For more options, visit https://groups.google.com/groups/opt_out.
This is a class (i.e. the definition of what a Window is and what it does):
visual.Window
This is a function that returns an actual instance of a class that you can actually do things with:
visual.Window()
You want the second one.
Mike
On 17 Apr, 2013, at 14:13, ShoinExp <mark.a....@gmail.com> wrote:
> Hi Andre,
> Thanks for that. I changed to visual.Window and that solved that problem, but now I have a new error:
>
> win.push_handlers(keyboard)
> AttributeError: class Window has no attribute 'push_handlers'
>
> Any thoughts on that one?
> Thanks,
> Mark
>
> On Tuesday, April 16, 2013 11:28:42 PM UTC+9, Andre Gouws wrote:
> should probably be "visual.Window" not "window.Window"
>
>
> On Tue, Apr 16, 2013 at 3:01 PM, ShoinExp <mark.a....@gmail.com> wrote:
> Hi Dave,
> This is an old post, but I'm trying to do the same thing. I'm not a coder at all, so I'm cutting, pasting and praying. When I added your code to my experiment (which at the moment is just a single trial with a single keyboard-event so that I can play around with getting your code working), I got the following error:
>
> win = window.Window
> NameError: name 'window' is not defined
--
Michael R. MacAskill, PhD michael.maca...@nzbri.org
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/15535a2b-c3eb-443e-a1c5-ab636cde485b%40googlegroups.com.
from psychopy.iohub import launchHubServer,EventConstants,
io=launchHubServer(psychopy_monitor_name='default')
keyboard = io.devices.keyboard
# Create you PsychoPy Window and and do other stuff needed to setup your exp....
#....
# When you want to start getting key events, discarding any previous events call...
io.clearEvents('all')
# Each time you want to check for key press - release combinations
# and get the duration etc. do as follows...
for event in keyboard.getEvents():
if event.type == EventConstants.KEYBOARD_CHAR:
# KEYBOARD_CHAR events are created only after a key is press and then released.
# access the key press - release duration...
key_press_duration=event.duration
# access also can access other event details:
key_press_start_time=event.press_event.time
key_release_time=event.time
# For visible keys,key is the unicode char value (i.e. 'a', '+', '1', etc.), and for non visible keys it is a str constant (i.e. 'DOWN', 'UP', 'HOME', 'PAGE_DOWN', etc.). key=event.key
# If you also need to calculate the RT from a stim. onset to when the key started to be pressed it would go something like:
# draw your stim..
# .....
flip_time=win.flip()
# Use code above to get KEYBOARD_CHAR events.
# ......
# The RT for each KEYBOARD_CHAR event from the stim onset =:rt_in_secs=event.press_event.time - flip_time
from psychopy.iohub import launchHubServer
io=launchHubServer(psychopy_monitor_name='default')
keyboard = io.devices.keyboard
print "ioHub Started.... Press Any Key to Exit"
io.clearEvents()
while not keyboard.getEvents():
io.wait(0.25)
io.quit()If this does not work, please send the full stack trace for the error that should be printed to the console window.
Thanks again,
Sol