PsychoPy Key Down Code using ioHub

怎甘沉沦 提交于 2019-12-11 04:51:24

问题


I am trying to write a code in which the user is able to adjust the length of a line with the up and down arrow keys.

I am able to have the user adjust the line by pressing them, but this takes a very long time since I am having it so that the length increases (if the up key is pressed) or decreases (if the down key is pressed) by 1 pixel every time a key is pressed.

I was wondering if anyone knew how to adjust the length of a line while a key is held down and stop the adjustment of that line once the key is released?

I have been playing with different ioHub features, but have not been able to figure it out.

I tried to base it on the duration of the key press, but the duration is not reported until after the key is released, so the user is not able to view the see how they are adjusting the line while they are holding down the key.

Does anyone have any suggestions?


回答1:


This answer assumes you are using PsychoPy's Builder interface. But you can just put the same code snippets at appropriate places within the Coder interface as well.

I assume you have a Line component, and that its size is specified in normalised units. Now insert a Code component (after it is created, right-click on it and move it above the Line component so that changes to the Line object get applied immediately rather than on the next screen refresh).

In the Code component's "Begin Experiment" tab, put this code to initialise ioHub and to create an initial value for the scaling factor that will be applied to the line (defaults to zero):

from psychopy.iohub import launchHubServer, EventConstants

io=launchHubServer(experiment_code='key_evts', psychopy_monitor_name='default')
keyboard = io.devices.keyboard

increment = [0, 0] # initial value of scaling factor

Then in the "Each frame" tab, we will check for key presses. So if your screen is running at 60 Hz, that is the rate at which the line size will be updated.

# check the keyboard
for event in keyboard.getEvents():
    if event.type == EventConstants.KEYBOARD_PRESS:
    # a key has been pressed. This is reported only once, so set the value 
    # of the scaling factor to be used until the key is released:
        if event.key == u'UP':
            increment = [0.01, 0] # make it 1% of screen half-width longer
        elif event.key == u'DOWN':
            increment = [-0.01, 0] # make 1% shorter
    if event.type == EventConstants.KEYBOARD_RELEASE:
    # the key is no longer being pressed, so stop changing the size:
        increment = [0, 0]

# regardless of what key is/isn't pressed, apply the current
# scaling factor on every screen refresh
line.size += increment

Hope that works for you. (I'm a novice at using ioHub: this works for me but may not be the "correct" way to do it).



来源:https://stackoverflow.com/questions/25495252/psychopy-key-down-code-using-iohub

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!