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:24PM

    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:24PM

    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:24PM

    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:24PM

    Cheers!

  • edited 11:24PM

    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:24PM

    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:24PM

    Cheers!

  • edited 11:24PM

    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:24PM

    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:24PM

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

    Thanks!

  • edited 11:24PM

    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:24PM

    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:24PM

    Awesome, it works! Cheers!

  • edited 11:24PM

    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:24PM

    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.