# Using a four-buttons button-box to code a number between 0 and 99

**URL:** <https://discourse.psychopy.org/t/using-a-four-buttons-button-box-to-code-a-number-between-0-and-99/9257>\
**Category:** Coding\
**Created:** [October 1, 2019, 12:40am UTC](https://discourse.psychopy.org/t/using-a-four-buttons-button-box-to-code-a-number-between-0-and-99/9257 "2019-10-01T00:40:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lironroz](https://avatars.discourse-cdn.com/v4/letter/l/ecccb3/32.png) [@lironroz](https://discourse.psychopy.org/u/lironroz)\
**Post date:** [October 1, 2019, 12:40am UTC](https://discourse.psychopy.org/t/using-a-four-buttons-button-box-to-code-a-number-between-0-and-99/9257/1 "2019-10-01T00:40:51Z")

</div>

Hello.  
In my task (in the fMRI), participants are asked to enter a number between 0 and 99 in multiple trials.  
I’d like to use a four-button button-box (1-4) to code for that number, such that two buttons control the 10s digit - one increases it and one decreases, and two buttons control the ones digit in a similar manner.  
At the beginning of each trial, participants will see the number 55 and work their way from there, by increasing and decreasing each digit. This means that their actions would have to be displayed on the screen while they press the buttons. Ultimately, they may use a fifth button to confirm their selection.

Has anyone come across anything similar to that or has a clue on how to start?  
I looked throughout the forum and couldn’t find a similar question.  
Any advise would be highly appreciated!  
Thanks!

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [October 1, 2019, 2:05am UTC](https://discourse.psychopy.org/t/using-a-four-buttons-button-box-to-code-a-number-between-0-and-99/9257/2 "2019-10-01T02:05:22Z")

</div>

Are you using Builder, or programming the experiment from scratch?

---

<div class="post-metadata">

**Author:** ![lironroz](https://avatars.discourse-cdn.com/v4/letter/l/ecccb3/32.png) [@lironroz](https://discourse.psychopy.org/u/lironroz)\
**Post date:** [October 1, 2019, 2:17am UTC](https://discourse.psychopy.org/t/using-a-four-buttons-button-box-to-code-a-number-between-0-and-99/9257/3 "2019-10-01T02:17:54Z")

</div>

I’m programing it.

---

<div class="post-metadata">

**Author:** ![lironroz](https://avatars.discourse-cdn.com/v4/letter/l/ecccb3/32.png) [@lironroz](https://discourse.psychopy.org/u/lironroz)\
**Post date:** [October 1, 2019, 2:19am UTC](https://discourse.psychopy.org/t/using-a-four-buttons-button-box-to-code-a-number-between-0-and-99/9257/4 "2019-10-01T02:19:25Z")

</div>

It’s currently ready, only the responses were thought to arrive form the corresponding numeric keys (meaning 0-9). I now realized this would not be possible in the scanner (MRI) and thus need to change only this part of the script.

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [October 1, 2019, 4:37am UTC](https://discourse.psychopy.org/t/using-a-four-buttons-button-box-to-code-a-number-between-0-and-99/9257/5 "2019-10-01T04:37:29Z")

</div>

So it’s not clear now: is the issue with controlling the numeric display (which will be straightforward), or is it some problem with actually receiving the keypresses from the response pad in the MRI?

But assuming you can detect keys in some fashion, some pseudo code would be something like this:

```auto
# at the beginning of the trial:
tens = 5
ones = 5

```

```auto
# on every screen refresh:

response = event.getKeys() # or however you are getting responses

if response: 
    # allowing for multiple responses per check:
    if '1' in response: # decrement the tens counter:
        tens = max(0, tens - 1)
    if '2' in response: # increment the tens counter:
        tens = min(9, tens + 1)
    if '3' in response:
        ones = max(0, ones - 1)
    if '4' in response:
        ones = min(9, ones + 1)
    some_text_stimulus.text = str(tens) + str(ones)

some_text_stimulus.draw

```

---

<div class="post-metadata">

**Author:** ![lironroz](https://avatars.discourse-cdn.com/v4/letter/l/ecccb3/32.png) [@lironroz](https://discourse.psychopy.org/u/lironroz)\
**Post date:** [October 1, 2019, 7:05pm UTC](https://discourse.psychopy.org/t/using-a-four-buttons-button-box-to-code-a-number-between-0-and-99/9257/6 "2019-10-01T19:05:06Z")

</div>

Thank you! I will try it soon and see how it works.  
(there is no problem with receiving the input, only with numeric display and coding of digits going up or down)

---

<div class="post-metadata">

**Author:** ![lironroz](https://avatars.discourse-cdn.com/v4/letter/l/ecccb3/32.png) [@lironroz](https://discourse.psychopy.org/u/lironroz)\
**Post date:** [October 5, 2019, 2:22am UTC](https://discourse.psychopy.org/t/using-a-four-buttons-button-box-to-code-a-number-between-0-and-99/9257/7 "2019-10-05T02:22:55Z")

</div>

it worked great! Thank you!
