Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Detecting keyup-events

Hi everyone,
Recently, I wrote a script where you can move a slider in order to rate a picture (see below). Atm, you have to press the arrow key every time you move the slider for one step. I want to change that in a way that you can hold down the arrow key and the slider will continuously move along the bar.
The problem is that any time I use a wait/ or check method, only the keydown event is considered. I think a viable strategy might be to use the check method + detecting when a keyup-event is happening. Does someone have an idea how to implement that?

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed May  9 17:20:15 2018

@author: peterkraemer
"""

from expyriment import control, stimuli

#control.set_develop_mode(True)

exp = control.initialize()
######################  STIMULI  ######################
#   creating elements on screen

# picture
path_picture =   'some/picture/path'
picture      =   stimuli.Picture(path_picture, position=(0,200))

# bar
barLength = 300
barThick = 10
posBar=(0,-100)
bar = stimuli.Rectangle((barLength,barThick),corner_rounding=None,position=posBar)
bar.preload()
barEndLeft = stimuli.Rectangle((5,barThick*3),position=((-(barLength+2)/2), posBar[1]))
barEndLeft.preload()
barEndRight = stimuli.Rectangle((5,barThick*3),position=(((barLength+2)/2), posBar[1]))
barEndRight.preload()

# slider
posSlider = (0,posBar[1]) # initial position of the slider

# sliding function
def sliding(pos,commit):
    while commit==0:
        # create a composition to show multiple elements at the same time
        composition = stimuli.BlankScreen()
        picture.plot(composition)
        bar.plot(composition)
        barEndLeft.plot(composition)
        barEndRight.plot(composition)
        slider = stimuli.Rectangle((3,30),position=pos, colour = (194, 24, 7))
        slider.plot(composition)
        composition.present()
        # participants can use left and right arrow to adjust slider position & commit with enter
        key, rt = exp.keyboard.wait(process_control_events=True)
        if key == 13:
            commit = 1
            return pos[0]
        elif key == 275:
            pos = (pos[0]+10, pos[1])
            if pos[0] > barLength/2: # fixes upper end of the scale
                pos = (barLength/2,pos[1])
        elif key == 276:
            pos = (pos[0]-10, pos[1])
            if pos[0] < -barLength/2: # fixes lower end of the scale
                pos = (-barLength/2,pos[1])

# position - money translation
scaleMax = 5 # upper end of the scale
moneyPerPixel=scaleMax/barLength # = CHF/Pixel

## start ##
control.start(exp,skip_ready_screen=True)

val = (sliding(posSlider,0)-(-barLength/2))*moneyPerPixel
print(val)

## end ##

control.end()

Sign In or Register to comment.