Howdy, Stranger!

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

Supported by

Detect any key press

Hi,

I am working on OpenSesame with Expyriment back end. I need users to give a description as response. So I need to use entire keyboard. I am collecting key-presses one by one. The code I am using is shown below. It seems like the execution is paused at line 'key, time = my_keyboard.get_key()' until a key is pressed. I need the execution to continue even if there is no key press. That is, in case there is a key press, it should execute the 'if' statement, and if there is no keypress it should continue execution bypassing the 'if'. I need the execution to continue with a return value of 'None' even if there is no key press, and not to pause till a key is pressed.
Another issue i am facing is that if keys are pressed at a higher speed, then few key presses are not detected. I want to collect all key presses even if they are made at a higher rate.
Please give me some guidance regarding these issues.

Comments

  • Hi,

    You need to set the timeout parameter in the get_key() command; e.g., my_keyboard.get_key(timeout = 1). This collects a key over the course of 1ms, and thus collects a 'none' for every millisecond that no key was pressed.
    Then there's still the problem that if people type very very fast, you may lose a keypress or two. Is it necessary that people type as fast as possible? Otherwise you could ask your participants to try to type at a relaxed pace. The 1ms timeout should certainly be able to accommodate normal typing speeds.

    Cheers,

    Josh

  • Hi,
    The problem of missing keypress was resolved once i switched the back end from xpyriment to legacy. I am collecting responses using keyboard and all keypresses are collected properly. I wanted to add something sort of a 'Next' button to move forward. I created a rectangle and monitored for mouse click in that rectangular region. Mouse clicks are getting detected properly now, but now the key presses are getting missed. The code I am using is shown below.


    from openexp.keyboard import keyboard
    from openexp.mouse import mouse
    from openexp.canvas import canvas
    
    my_keyboard = keyboard(exp) 
    my_mouse = mouse(exp)
    cv = canvas(exp) # initialize a canvas
    resp = "" # Here we store the response
    my_mouse.show_cursor(show = True)
    
    def timer2clock(timer):
        '''
        converts a plain integer representing the seconds left to a proper
        clock
        '''
        t = timer/1000
        t = int(t)
        min = t//60
        if min<10:
            min = '0'+str(min)
        else:
            min = str(min)
        sec = t%60
        if sec<10:
            sec = '0'+str(sec)
        else:
            sec = str(sec)
    
        clock = min+':'+sec 
        return clock
    
    task_done = False
    start_time = self.time()
    timer = 20000 # 20 seconds
    rest_time = 20000
    clock = timer2clock(timer)
    cv.text(clock, x  = 350, y = -300)
    cv.text('Question 1', x = -250, y = -350)
    cv.text('Write all the details that you can recall', x = -250, y = -300)
    cv.rect(-480, -280, 930, 450, fill=False)
    cv.rect(-120, 240, 160, 50, fill=False)
    cv.text('Next', x = -50, y = 265)
    cv.show()
    
    # update screen
    while (task_done == False and rest_time > 0):
    
        key, time = my_keyboard.get_key(timeout = 1)
        if key is not None: 
            if key == "backspace":
                resp = resp[:-1]    
            elif key == "space":
                resp += " "
            else:
                resp += my_keyboard.to_chr(key)
            if key == "return":
                resp = resp[:-6] 
            elif key == "left shift":
                resp = resp[:-10]
            elif key == "right shift":
                resp = resp[:-11]
            elif key == "caps lock":
                resp = resp[:-9]    
        cur_time = self.time()  # update time
        rest_time = timer - (cur_time - start_time)
        clock = timer2clock(rest_time)
        steps1 = range(0,len(resp),82)
        steps = map(lambda x: x/82, steps1)
        cv.clear()
        cv.text(clock,x = 350, y = -300)
        cv.text('Question 1', x = -250, y = -350)
        cv.text('Write all the details that you can recall', x = -250, y = -300)
        cv.rect(-480, -280, 930, 450, fill=False)
        cv.rect(-120, 240, 160, 50, fill=False)
        cv.text('Next', x = -50, y = 265)
        for i in steps:
            resp1 = resp[(82*i):min(82*(i+1)-1,len(resp))]
            cv.text(resp1, center=False, x = -470, y = -260+30*i)
    
        cv.show()
        button, position, timestamp = my_mouse.get_click(timeout=1) 
        if button is not None:
            (x,y), time = my_mouse.get_pos()
            if (x>-120 and x<40):
                if (y>240 and y<290):
                    task_done == True
                    break
    
    log.write(resp)
    

    When I comment the following portion all keypresses get recorded properly.

       button, position, timestamp = my_mouse.get_click(timeout=1)
    if button is not None:
        (x,y), time = my_mouse.get_pos()
        if (x>-120 and x<40):
            if (y>240 and y<290):
                task_done == True
                break
    

    With above portion included in the code, there is issues with collecting keypresses.

    Please help me resolving this issue.

Sign In or Register to comment.