Howdy, Stranger!

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

Supported by

Keyboard Timeout set to a jitter

Hey Everyone,

Right now I am trying to implement a jitter to my experiment but I need to keyboard to timeout in response to the jitter. In order to do this, I have used inline script to set up my keyboard. Right now I get the following error: "TypeError: keyboard() takes at least 1 argument (0 given)"

from random import randint
from openexp.keyboard import keyboard

#start timer
t0 = clock.time()

#calculate jitter length
offset_time = randint(850, 1150)

#initiate keyboard, set wait time to the jitter
my_keyboard = keyboard(keylist=['space'], timeout=offset_time)
start_time = clock.time()

#get keyboard response
key, end_time = my_keyboard.get_key()
var.response = key
var.response_time = end_time - start_time

#calculate offset wait time (in case of early keypress), using the jitter
t1 = clock.time()
current = (t1-t0)
if current < offset_time:
    time = offset_time-current
    self.sleep(int(time))

Comments

  • Hi,

    The error is because you're mixing the old (≤ 2.9) and new (≥ 3.0) ways of creating a keyboard object.

    The old way is to import the keyboard function (from openexp.keyboard import keyboard), and pass exp as the first argument. You're doing the former, but not the latter.

    # <= 2.9
    from openexp.keyboard import keyboard
    k = keyboard(exp) # Plus optional keywords
    

    The new way is to not important anything, and not pass exp as first argument. So it's much cleaner.

    # >= 3.0
    k = keyboard() # Plus optional keywords
    

    Does that make sense?

    See also:

    Cheers,
    Sebastiaan

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • Sebastiaan,

    This absolutely makes sense. I removed the import and it appears to be working perfectly now. I had another problem come up though, when I try to evaluate whether or not a correct response is given, in the case where the correct response is 'None' (i.e., no keyboard response is the correct response), I can't get the program to evaluate that as a "correct response". I tried adding print lines and "correct_response" is 'None' and the response is 'None' but when I compare the values in an if-statement it evaluates as false! Any ideas?

    from random import randint
    
    #start timer
    t0 = clock.time()
    
    #calculate jitter length
    offset_time = randint(850, 1150)
    
    #initiate keyboard, set wait time to the jitter
    my_keyboard = keyboard(keylist=['space', '1'], timeout=offset_time)
    start_time = clock.time()
    
    #get keyboard response
    key, end_time = my_keyboard.get_key()
    var.response = key
    var.response_time = end_time - start_time
    
    #Set correct response
    if var.response == var.correct_response:
        correct = 1
    else:
        correct = 0
    var.correct = correct
    
    #print for debug
    print("--Trial--")
    print(var.response)
    print(var.response_time)
    print(var.correct_response)
    print(var.response == var.correct_response)
    print(var.correct)
    
    #calculate offset wait time (in case of early keypress), using the jitter
    t1 = clock.time()
    current = (t1-t0)
    if current < offset_time:
        time = offset_time-current
        self.sleep(int(time))
    
  • Hey,

    In case no response is given, your variable key, is of None type. The variables in your loop table are implicitly converted to a number or to a string (where applicable). So typing None, will eventually be converted to "None". Do you see what I mean? (If not you can print the types of your response and your correct response variable. To make it work, you can also convert your key variable to a string (e.g. var.response = unicode(key).
    Does this make sense?

    Eduard

  • Eduard,

    That absolutely makes sense and it absolutely worked. Thank you so much for your response!

Sign In or Register to comment.