Welcome!

Sign in with your CogSci, Facebook, Google, or Twitter account.

Or register to create a new account.

We'll use your information only for signing in to this forum.

Supported by

Measuring how much time a button was pressed

amihai_gottliebamihai_gottlieb Posts: 23
edited June 2016 in OpenSesame

Hello,

Is there a way to measure how much time a button was pressed?
I cannot seem to locate it in the log file.

Amihai

Comments

  • PascalPascal Posts: 52
    edited 10:58AM

    Hi Amihai,

    I am not completely sure what your exact problem is. Are you using a button in a form base plugin and do you want to know the total response time for the form?

    If so, I don't think that it calculates response times.

    However, you could calculate them using Python code in inline scripts. You first get the time (t0) in the run phase of an inline script directly before the form is presented. Then you present the form. Then you get the time (t1) in the run phase after the inline script. The difference (t1-t0) is your response time. This is similar to the general example in the clock functions (http://osdoc.cogsci.nl/python/clock/).

    Alternatively, you could install and use the mousetrap form plugin (general information about mousetrap: http://osdoc.cogsci.nl/devices/mouse-tracking/ , specific information about mousetrap_form https://github.com/PascalKieslich/mousetrap-os/blob/master/plugins/mousetrap_form/mousetrap_form.md). It is a modified version of the form base plugin and it calculates response times automatically. If you are not interested in the mouse-tracking data (which is the actual purpose of the plugin), you can simply uncheck the option "Save mouse-tracking data".

    Hope this answers your question - feel free to ask if not!

    Best
    Pascal

    Thanked by 1amihai_gottlieb
  • Dear Pascal,

    I just saw your answer to my questions (after quite a delay, sorry..).

    In my exp I am asking participants to reproduce duration times of stimuli by pressing the space button.
    Is there a way to measure the time this button was pressed? I use the logger to record response time of the keyboard response but it does not seem to measure the pressing time.

    Thanks
    Amihai

  • Or maybe anyone else has an idea how to measure how much time a button was pressed using a keyboard response?

    Amihai

  • PascalPascal Posts: 52

    Hi Amihai,

    I assume you are using a keyboard_response item to record the key press. This will give you the duration after which the key was pressed (in the variable response_time). However, as far as I know it will not tell you for how long the key was pressed.

    Unfortunately, I don't know much about the technical details of the keyboard Python functions - so I cannot tell you whether it is possible to determine that at all - If this is what you want to know. I think that it is not possible using OpenSesame's keyboard class. One idea I have is that you maybe could directly use a Python function from one of the backends.

    Best,

    Pascal

  • Hi Pascal,

    Thanks for the help.
    I found this thread:

    http://forum.cogsci.nl/index.php?p=/discussion/2271/measure-duration-of-key-press

    But unfortunately, I do not know python very well and cannot understand which part of the code I can use just to measure the keypress and create a variable out of it. Can you help?

    Amihai

  • JarikJarik Posts: 190

    Hi Amihai,

    Maybe searching the forum for "keyup" helps.

    Best,
    Jarik

  • Jarik,

    Thanks for the tip! I found this thread:

    http://forum.cogsci.nl/index.php?p=/discussion/37/solved-how-to-log-the-duration-of-a-key-press

    which looks very relevant, but I can not figure out where to put this script and how to define the key which I need to measure. Do you know?

    Amihai

  • JarikJarik Posts: 190

    You can copy the code in an inline item that follows a sketchpad item for example. This sketchpad item can have a duration of 0 milliseconds so it advances to your inline code immediately. You can use the sketchpad item to present instructions like "press space for 2 seconds" to the subject.

    The code logs all keys now.

    Best,
    Jarik

  • Dear Jarik,

    I did what you wrote and it workes!

    Thank you very much.

    Amihai

  • Jarik, or anyone else how can help,

    Is there a way to record which button was pressed using the inline script item?

    Amihai

  • eduardeduard Posts: 977

    Hi,

    Maybe you can summarize what you already accomplished and what is the code that you use? Then, it will be easier to find out what else is missing.

    Eduard

  • Eduard,

    I just noticed that I never answered your message. Sorry about that.
    I ended up using the following code in the run part of the inline script:

    import pygame
    from pygame.locals import *

    key_down_time = None
    while key_down_time == None:
    for event in pygame.event.get():
    if event.type == KEYDOWN:
    key_down_time = self.time()
    key_pressed = event.key
    break

    key_up_time = None
    while key_up_time == None:
    for event in pygame.event.get():
    if event.type == KEYUP:
    key_up_time = self.time()
    break

    key_duration = key_up_time - key_down_time
    self.experiment.set("key_duration", key_duration)
    self.experiment.set("key_pressed" , key_pressed)

    With this, I manage to log the pressing time of the key that was pressed but the actual key name is logged as a number which I need to "translate" using an ASCII table. I was wondering whether it is possible to just log the name of the key.

    Thanks
    Amihai

  • eduardeduard Posts: 977

    Hi Amihai,

    Good to know it works.

    I was wondering whether it is possible to just log the name of the key.

    Maybe this discussion helps: https://stackoverflow.com/questions/227459/ascii-value-of-a-character-in-python

  • Eduard,

    Thanks for the link. I understand that I can use the chr() function to return the key but I do not know how to implement it in the code.
    Do you think this will this work (I do not know how to code in python):

    self.experiment.set("key_pressed" , chr(key_pressed))

    Amihai

  • eduardeduard Posts: 977

    That is exactly how it works.

    Eduard

Sign In or Register to comment.