Howdy, Stranger!

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

Supported by

[open] Keystroke speed

edited October 2014 in OpenSesame

Hi everyone, I am just getting started with OpenSesame for my undergrad project. But I feel a bit daunted by it.

I want to create a block where the user types text, and the program times the keypress between keys and saves the mean speed into the csv file.

I have knowledge with VB.NET/C# and Python seems simple enough. But I don't know where to start, can someone push me in the right direction?

import time
start_time = time.time()`
user_input = raw_input("")
stop_time = time.time()
chars_per_s = len(user_input) / (stop_time - start_time)
print "you typed at %.4f characters per second" % chars_per_s

Comments

  • edited October 2014

    Hi,

    Welcome to the forum! As a general tip: you can find information on Python inline scripting on the Documentation Page.

    The following script should allow users to type, and calculate the average typing speed:

    import copy
    from openexp.canvas import canvas
    from openexp.keyboard import keyboard
    
    allowed = 'abcdefghijklmnopqrstuvwxyz.,'
    
    my_kb = keyboard(exp)
    my_canvas = canvas(exp)
    my_canvas.clear()
    my_canvas.show()
    
    t0 = None
    stop = False
    my_string = ""
    while not stop:
        # get keypress
        key, t1 = my_kb.get_key(timeout=None)
        # save timestamp of first input
        if t0 == None:
            t0 = copy.copy(t1)
        # handle input
        if key.lower() in allowed:
            my_string += key
        elif key == 'space':
            my_string += " "
        elif key in ['enter','return']:
            stop = True
        # update display
        my_canvas.clear()
        my_canvas.text(my_string)
        my_canvas.show()
    
    # calculate time
    if t1 == t0:
        speed = "NOTHING (only one keystroke, no timing info)"
    else:
        speed = len(my_string) / ((t1-t0)/1000.0)
    
    # display speed
    my_canvas.clear()
    my_canvas.text("Your speed was %.2f keystrokes per second" % speed)
    my_canvas.show()
    
    # wait for a spacepress
    my_kb.get_key(keylist=['space'], timeout=None)
    
    # save the speed and the string
    exp.set("response", my_string)
    exp.set("typespeed", speed)
    

    Now, in your logger save the variables response and typespeed (you might have to manually add those) In other experiment items, simply refer to [typespeed] if you want to use its value.

    Good luck!

Sign In or Register to comment.