Howdy, Stranger!

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

Supported by

[solved] Collecting number of keypresses in a fixed amount of time

edited March 2014 in OpenSesame

Hi, i'm making an experiment and i want to collect the number of times that the subject press the key "1" in 8000ms.
That's the important part.

If possible (it's secondary), i want to collect also the times between responses, or the amount of time if "1" is being holded (it would be interesting for a graphic, later).

It seems simple, but i'm new with this software and i would be very grateful if somebody can help me, because we're a little rushed.

Thanks, thank you for make OpenSesame, great software.

Comments

  • edited March 2014

    Hi!

    In theory, you could do this using the GUI, but you'd end up with massive data files! The easiest solution would probably be to use the following code in an inline_script item:

    # import the keyboard class, to be able to
    # poll the keyboard
    from openexp.keyboard import keyboard
    # import numpy, which we will use to do
    # some calculations with
    import numpy
    
    # initialize a keyboard instance
    kb = keyboard(exp, keylist=['1'], timeout=10)
    
    # create an empty list, to store the keypress
    # times in (so we can use those later)
    presstimes = []
    
    # let the user smash your keyboard for 8 seconds!
    t0 = self.time()
    while self.time() - t0 < 8000:
        # wait for a keypress
        keypress, presstime = kb.get_key()
        # add the press time to our list
        if keypress:
            presstimes.append(presstime)
    
    # convert the list of keypresses to a numpy
    # array of keypresses, to make calculations
    # easier to do
    presstimes = numpy.array(presstimes)
    
    # after the frenzy, convert the keypress times to
    # reactiontimes, by calculating the difference
    # between each presstime (using numpy.diff)
    intresptimes = numpy.diff(presstimes)
    
    # calculate the mean and standard deviation
    # of the inter-press times
    M = numpy.mean(intresptimes)
    SD = numpy.std(intresptimes)
    
    # some more descriptives: the amount of
    # presses and the standard error of the mean
    N = len(intresptimes)
    SEM = SD / (N**0.5)
    
    # save the variables so that they can be used
    # in OpenSesame's GUI
    exp.set("Nipt", N)
    exp.set("Mipt", M)
    exp.set("SDipt", SD)
    exp.set("SEMipt", SEM)
    

    Keeping track of how long the button was down, is not possible in this way. You'll have to directly use either PyGame or PsychoPy to do so.

    Good luck!

  • edited 4:29PM

    Thank you very much Edwin!

    I'm experiencing this problem when trying to run the experiment with the given code:

    Error while executing inline script
    
    phase: run
    item: inline_script
    line: 9
    exception message: __init__() takes at least 2 arguments (3 given)
    exception type: TypeError
    
  • edited March 2014

    My apologies, that's due to a silly mistake (forgot to pass exp to the keyboard!). I've edited my previous post, so it should work now.

  • edited March 2014

    Thank you very much. It works like a charm.

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games