[solved] Counting the number of times a key is pressed
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
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 wantEduard
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
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
Cheers!
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!!
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)
Oh sorry.
You have to replace
kb.get_key(10)withkb.get_key(timeout=10).This is a great example of sloppy programming.
Cheers!
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!!
I can't open your image. Can you try posting it again? Without I have trouble seeing what the problem could be.
Eduard
is it working with this link?
file:///N:/Desktop/ExperimentBuilder/OpenSesame/ClickTask.jpg
Thanks!
No, it doesn't. Try using Filedropper, googleDrive or any other image hoster. Follow their instructions and post the final link here.
https://drive.google.com/a/port.ac.uk/file/d/0B2zMbS_dRlIuYTRRQUtZWU9YLUU/view?usp=sharing
Hopefully this one will work...
Another stupid mistake of mine. I forgot to actually show the image (and correspondingly, clear it)
Awesome, it works! Cheers!
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!
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
keyand the time, at which the key was pressed is saved intime.you can add
t0 = clock.time()before thewhileloop, removeif key == '0':and changecount +=1intot1 = clock.time()(including removing the indent). Finally, set the break condition tot1-t0 > 45000and 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
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