Howdy, Stranger!

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

Supported by

[solved] count keystrokes in a specified time

edited October 2014 in OpenSesame

HI ,
I am looking at counting the keystrokes in a specified set duration .
want to know how can this be achieved .
I do see a variable "count_keyboard_response".
which i am currently unable to use .
It would be great if there is some pointers to this.

Regards,
Sundar.

Comments

  • edited 4:30AM

    Hi Sundar,

    The count_keyboard_response variable tells you how often the keyboard_response item has been run. Each keyboard_response item, per definition, collects only one keypress.

    If you would like to count how often participants press a key, you could do with a little Python inline scripting:

    from openexp.canvas import canvas
    from openexp.keyboard import keyboard
    
    # create a new canvas object, showing instructions
    my_canvas = canvas(exp)
    my_canvas.text("Press Space as often as you can!")
    
    # create a new keyboard object, with only Space presses allowed
    my_keyboard = keyboard(exp, keylist=['space'], timeout=1)
    
    # starting value for the keypress counter
    presscount = 0
    
    # run for ten seconds
    t0 = self.time()
    while self.time() - t0 < 10000:
        # check if there is a new keypress
        key, presstime = my_keyboard.get_key()
        # add one to the count if there was a keypress
        if key != None:
            presscount += 1
    
    # store the keypress as in OpenSesame workspace
    exp.set("keypress_count", presscount)
    
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # EVERYTHING BELOW HERE CAN BE REPLACED BY OPENSESAME ITEMS #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    
    # show a blank display for a second, signalling the participant to stop
    my_canvas.clear()
    my_canvas.show()
    self.pause(1000)
    
    # show the result
    my_canvas.text("You pressed Space %d times" % presscount)
    my_canvas.show()
    
    # wait for a keypress before continuing
    my_keyboard.get_key(timeout=None)
    

    The code is pretty self explanatory. Also, please note that everything below the marker can be replaced by a blank sketchpad with a Duration of 1000 ms, and a sketchpad with a Duration of keypress and a text like this: You pressed Space [keypress_count] times.

    Good luck!

  • edited 4:30AM

    Thanks Edwin,
    This trick worked pretty well with python script.
    Also let me know how do i pass this variable "presscount"
    to be logged in in the logger .

    Regards,
    Sundar.

  • edited 4:30AM

    Go to your logger, click Add custom variable, type keypress_count.

    Also, it might be wise to go through some tutorials and the documentation pages.

Sign In or Register to comment.