Howdy, Stranger!

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

Supported by

Measure duration of key press

Hi! I'm making an experiment about time estimation, where the participant will hold down a key for three seconds (according to their own estimation). I'm having problem measuring the duration of the key press, though.

Here is one version I've tried, but when the experiment gets to the point of the inline script, the experiment stops and it says that it ended successfully. I'm new to OpenSesame and Python, so unfortunately I don't understand what goes wrong.

The prepare phase:

The run phase:

Comments

  • Hi Charlotte,

    The reason nothing happened, is that you only broke out of the for loop, but not out of the while loop. YOu would need another break statement outside the for loop that is only executed once you broke out of the for loop. Alternatively, you can also get rid of the for loop entirely (assuming that your participants are not pressing multiple keys simultaneously). Below is an example of the code for when you remove the for loop. I also added the presentation of a the current key press duration for illustration purposes.

    var.start_response = None
    var.time_down = 0
    var.time_up = 0
    var.time_elapsed = 0
    ok = False
    t0 = clock.time()
    
    cv = canvas()
    while True:
            # used for displaying the current key presses duration
            if ok:
                cv.text(str(pygame.time.get_ticks()-var.time_down),0,0)
                cv.show()
                cv.clear()
    
            # assume only one event, if event is empty proceed with next one
            # otherwise do the checks below
            eve = pygame.event.get()
            if len(eve) == 0:
                continue
            else:
                ev  = eve[0]
            if ev.type == pygame.KEYDOWN:
                if ev.key == pygame.K_SPACE:
                    var.time_down = pygame.time.get_ticks()
                    # used to display the current key press duration
                    ok = True
            elif ev.type == pygame.KEYUP and var.time_down != 0:
                if ev.key == pygame.K_SPACE:
                    var.time_up = pygame.time.get_ticks()
                    var.time_elapsed = (var.time_up-var.time_down)
                    break
            # break the loop if nothing happend in 10 seconds
            t1 = clock.time()
            if t1-t0> 10000:
                break
    
    #var.time_elapsed = (var.time_up-var.time_down)
    RTs_three_hold.append(var.time_elapsed)
    print RTs_three_hold
    

    Does this make sense?

    Eduard

    Buy Me A Coffee

  • @eduard is correct, of course. (You also mix up the name of the event, calling it event first and ev later.)

    But, just as a general remark, you can also use the for ... else pattern to deal with these kinds of nested loops, like so:

    while True:
        # Loop through all events and break the for loop when there is a KEYDOWN
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                break
        # The else block is executed only when the for loop did *not* break. So
        # we continue the while loop if the for loop did *not* break.
        else:
            continue
        # If the for loop *did* break, we also break the while loop.
        break
    

    See:

  • Thank you so much, Eduard, it all works as it should now!

    And thank you Sebastian, I didn't even think about doing it that way, it's good to know that's a possibility!

    /Charlotte

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