Howdy, Stranger!

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

Supported by

[solved] Counting the number of times a key is pressed

edited November 2015 in OpenSesame

Hello!

I realize my question may be very basic but I don't really know my way with Python..
I would like to have a program that is able to count the number of times the participant press the '0' key, store the information in an incremented variable (I guess..) that I would be able to call/present on a sketchpad.
The participant would have to press the key and will see at any time his/her score on the screen.

I think I need a conditional loop using "if" but I admit that is the best I'm able to do on my own..

Any help would be greatly appreciated!

Many thanks in advance! :)

Comments

  • edited 11:09AM

    Hey,

    If this is really everything your experiment is supposed to do, the solution is indeed quite easy. However, I don't think that's really all there is to it. So how about you share a few more details with us. Maybe including what you are interested in, or the general structure of it (blocks, trials, etc.)
    Just for sakes of completeness. This code, placed in an inline_script, will do what you want


    kb = keyboard() cv = canvas() count = 0 while True: cv.text(str(count)) key,time = kb.get_key(10) if key == '0': count +=1 # termination condition to avoid infinite loop if count > 100: break

    Eduard

    Buy Me A Coffee

  • edited 11:09AM

    Hey,

    Many thanks for your answer!
    I really want for it to be a game where participants need to press the key as many times as possible. The key is meant to break at some point in order to induce guilt in the participant. This program is one of 5 tasks presented, including answering questions about personality with on a rating-scale and presenting images.
    Will this script still do?

    Thank you for your help, really appreciated!
    Eglantine

  • edited 11:09AM

    Yep, the script would work for this experiment. However, it will stop as soon as the button is pressed 100 times. You can simply set it to the value that you need, or to some time duration. Whatever you prefer.

    Eduard

    Buy Me A Coffee

  • edited 11:09AM

    Cheers!

  • edited 11:09AM

    Alright I think I'm not quite there yet...
    From what I understood, as I want to get a response from the keyboard, I added a keyboard_response and a logger in order to save the response.
    When I try to run the script, the experiment stop with the following error message (see image). How can I change the script to make it work?

    Thank you!!

  • edited 11:09AM

    Error message

    The experiment did not finish normally for the following reason:

    Error while executing inline script

    Details

    item-stack: experiment[run].ClickTask_Loop[run].ClickTask_sequence[run].new_inline_script_2[run]
    exception type: TypeError
    exception message: get_key() takes exactly 1 argument (2 given)
    item: new_inline_script_2
    time: Fri Nov 27 10:23:29 2015
    phase: run

    Traceback (also in debug window)

    File "dist\libopensesame\inline_script.py", line 102, in run
    File "dist\libopensesame\python_workspace.py", line 160, in _exec
    File "", line 8, in
    File "dist\openexp\backend.py", line 181, in inner
    TypeError: get_key() takes exactly 1 argument (2 given)

  • edited November 2015

    Oh sorry.
    You have to replace kb.get_key(10) with kb.get_key(timeout=10).
    This is a great example of sloppy programming.

    Buy Me A Coffee

  • edited 11:09AM

    Cheers!

  • edited 11:09AM

    Hi again... so sorry to keep bothering you but when I try to run the program it just takes forever and never start...

    My experiment looks like that.
    file:///N:/Desktop/ExperimentBuilder/OpenSesame/ClickTask.png

    The text from contains "You pressed the key [count] times"
    the keyboard_response defined the corrected response/allowed responses as 0 et timeout=infinite.

    When I try to do a quick test run, the expyriment backend window opens up but stays frozen at "Preparing experiment..."

    Is there anything I can do to make it works?
    Should I just wait until it's ready to run or present an error message?

    Thank you!!

  • edited 11:09AM

    I can't open your image. Can you try posting it again? Without I have trouble seeing what the problem could be.

    Eduard

    Buy Me A Coffee

  • edited 11:09AM

    is it working with this link?
    file:///N:/Desktop/ExperimentBuilder/OpenSesame/ClickTask.jpg

    Thanks!

  • edited 11:09AM

    No, it doesn't. Try using Filedropper, googleDrive or any other image hoster. Follow their instructions and post the final link here.

    Buy Me A Coffee

  • edited 11:09AM

    Another stupid mistake of mine. I forgot to actually show the image (and correspondingly, clear it)

    kb = keyboard()
    cv = canvas()
    
    count = 0
    while True:
        cv.clear() # this is new
        cv.text(str(count))
        cv.show() # and that is new
        key,time = kb.get_key(timeout=10)
        if key == '0':
            count +=1
        # termination condition to avoid infinite loop
        if count > 100:
            break
    

    Buy Me A Coffee

  • edited 11:09AM

    Awesome, it works! Cheers!

  • edited 11:09AM

    Alright, I have one last question... How can I modify the script so the counter is on for 45 sec only?

    I understand it needs to be in the if ... break bit of the code but do I need to define the time at some point?
    Also, what does this:
    key,time = kb.get_key(timeout=10)
    refers to? Just so I fully understand the code :)

    Many thanks for your help!

  • edited 11:09AM

    key,time = kb.get_key(timeout=10)

    This line tells the experiment for 10 ms and wait for a key to be pressed. If something was pressed the identity of the key is saved in key and the time, at which the key was pressed is saved in time.

    you can add t0 = clock.time() before the while loop, remove if key == '0': and change count +=1 into t1 = clock.time() (including removing the indent). Finally, set the break condition to t1-t0 > 45000 and you're done.

    In general, I recommend you do some of the tutorials, listed on the documentation page of OpenSesame. This will improve your understanding quite a lot.

    Good luck,

    Eduard

    Buy Me A Coffee

  • I really want for it to be a game where participants need to press the key as many times as possible. The key is meant to break at some point in order to induce guilt in the participant. go This program is one of 5 tasks presented, including answering questions about personality with on a rating-scale and presenting images.

  • Hi,

    Sounds like a plan. What exactly is the problem then? When you have questions, please provide not just the general idea of your research, but also what you have already tried, how far you got and where you you ran into problems. Otherwise it is not really possible to properly help you.

    Eduard

    Buy Me A Coffee

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