Howdy, Stranger!

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

Supported by

Detect any key press

Hi,

I am working on OpenSesame with Expyriment back end. I need users to give a description as response. So I need to use entire keyboard. I am collecting key-presses one by one. The code I am using is shown below. It seems like the execution is paused at line 'key, time = my_keyboard.get_key()' until a key is pressed. I need the execution to continue even if there is no key press. That is, in case there is a key press, it should execute the 'if' statement, and if there is no keypress it should continue execution bypassing the 'if'. I need the execution to continue with a return value of 'None' even if there is no key press, and not to pause till a key is pressed.
Another issue i am facing is that if keys are pressed at a higher speed, then few key presses are not detected. I want to collect all key presses even if they are made at a higher rate.
Please give me some guidance regarding these issues.

Comments

  • Hi,

    You need to set the timeout parameter in the get_key() command; e.g., my_keyboard.get_key(timeout = 1). This collects a key over the course of 1ms, and thus collects a 'none' for every millisecond that no key was pressed.
    Then there's still the problem that if people type very very fast, you may lose a keypress or two. Is it necessary that people type as fast as possible? Otherwise you could ask your participants to try to type at a relaxed pace. The 1ms timeout should certainly be able to accommodate normal typing speeds.

    Cheers,

    Josh

  • Hi,
    The problem of missing keypress was resolved once i switched the back end from xpyriment to legacy. I am collecting responses using keyboard and all keypresses are collected properly. I wanted to add something sort of a 'Next' button to move forward. I created a rectangle and monitored for mouse click in that rectangular region. Mouse clicks are getting detected properly now, but now the key presses are getting missed. The code I am using is shown below.


    from openexp.keyboard import keyboard
    from openexp.mouse import mouse
    from openexp.canvas import canvas
    
    my_keyboard = keyboard(exp) 
    my_mouse = mouse(exp)
    cv = canvas(exp) # initialize a canvas
    resp = "" # Here we store the response
    my_mouse.show_cursor(show = True)
    
    def timer2clock(timer):
        '''
        converts a plain integer representing the seconds left to a proper
        clock
        '''
        t = timer/1000
        t = int(t)
        min = t//60
        if min<10:
            min = '0'+str(min)
        else:
            min = str(min)
        sec = t%60
        if sec<10:
            sec = '0'+str(sec)
        else:
            sec = str(sec)
    
        clock = min+':'+sec 
        return clock
    
    task_done = False
    start_time = self.time()
    timer = 20000 # 20 seconds
    rest_time = 20000
    clock = timer2clock(timer)
    cv.text(clock, x  = 350, y = -300)
    cv.text('Question 1', x = -250, y = -350)
    cv.text('Write all the details that you can recall', x = -250, y = -300)
    cv.rect(-480, -280, 930, 450, fill=False)
    cv.rect(-120, 240, 160, 50, fill=False)
    cv.text('Next', x = -50, y = 265)
    cv.show()
    
    # update screen
    while (task_done == False and rest_time > 0):
    
        key, time = my_keyboard.get_key(timeout = 1)
        if key is not None: 
            if key == "backspace":
                resp = resp[:-1]    
            elif key == "space":
                resp += " "
            else:
                resp += my_keyboard.to_chr(key)
            if key == "return":
                resp = resp[:-6] 
            elif key == "left shift":
                resp = resp[:-10]
            elif key == "right shift":
                resp = resp[:-11]
            elif key == "caps lock":
                resp = resp[:-9]    
        cur_time = self.time()  # update time
        rest_time = timer - (cur_time - start_time)
        clock = timer2clock(rest_time)
        steps1 = range(0,len(resp),82)
        steps = map(lambda x: x/82, steps1)
        cv.clear()
        cv.text(clock,x = 350, y = -300)
        cv.text('Question 1', x = -250, y = -350)
        cv.text('Write all the details that you can recall', x = -250, y = -300)
        cv.rect(-480, -280, 930, 450, fill=False)
        cv.rect(-120, 240, 160, 50, fill=False)
        cv.text('Next', x = -50, y = 265)
        for i in steps:
            resp1 = resp[(82*i):min(82*(i+1)-1,len(resp))]
            cv.text(resp1, center=False, x = -470, y = -260+30*i)
    
        cv.show()
        button, position, timestamp = my_mouse.get_click(timeout=1) 
        if button is not None:
            (x,y), time = my_mouse.get_pos()
            if (x>-120 and x<40):
                if (y>240 and y<290):
                    task_done == True
                    break
    
    log.write(resp)
    

    When I comment the following portion all keypresses get recorded properly.

       button, position, timestamp = my_mouse.get_click(timeout=1)
    if button is not None:
        (x,y), time = my_mouse.get_pos()
        if (x>-120 and x<40):
            if (y>240 and y<290):
                task_done == True
                break
    

    With above portion included in the code, there is issues with collecting keypresses.

    Please help me resolving this issue.

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