# Not properly updating key presses

**URL:** https://psychtoolbox.discourse.group/t/not-properly-updating-key-presses/3313
**Category:** Programming Help
**Created:** [July 29, 2020, 11:32pm UTC](https://psychtoolbox.discourse.group/t/not-properly-updating-key-presses/3313 "2020-07-29T23:32:38Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![stephen](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@stephen](https://psychtoolbox.discourse.group/u/stephen)
#### Post date: [July 29, 2020, 11:32pm UTC](https://psychtoolbox.discourse.group/t/not-properly-updating-key-presses/3313/1 "2020-07-29T23:32:38Z")

</div>

I have a piece of code that is supposed to collect key presses during a 2.5 second period. Every .5 seconds I am checking how many presses were made during that period. If the amount of presses exceeds the threshold, then I want to decrease the transparency of an image. Otherwise, it should stay the same. I am logging times of key presses as well as the final transparency level at the end of each trial. In my analysis however, I am noticing that the final transparency level sometimes is not what i am expecting based on times of presses. Sometimes it works, sometimes it does not. Any thoughts would be greatly appreciated. Thank you!

 ![Screen Shot 2020-07-29 at 7.25.09 PM](https://global.discourse-cdn.com/free1/uploads/psychtoolbox/original/1X/b91502ebab0fce38277b739146b1afa805f9b5d9.png)

---

<div class="post-metadata">

### Author: ![xiangrui](https://avatars.discourse-cdn.com/v4/letter/x/ec9cab/32.png) [@xiangrui](https://psychtoolbox.discourse.group/u/xiangrui)
#### Post date: [July 30, 2020, 1:19am UTC](https://psychtoolbox.discourse.group/t/not-properly-updating-key-presses/3313/2 "2020-07-30T01:19:40Z")

</div>

I see a potential issue. If you use KbCheck to count key presses, you will need to detect the key release whenever there is a press. Otherwise one press will be counted as multiple press.

---

<div class="post-metadata">

### Author: ![stephen](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@stephen](https://psychtoolbox.discourse.group/u/stephen)
#### Post date: [July 30, 2020, 5:32am UTC](https://psychtoolbox.discourse.group/t/not-properly-updating-key-presses/3313/3 "2020-07-30T05:32:27Z")

</div>

Interesting. So for example, if someone made a press within a given .5 second window, and then made another press within that window, the second press may not be recorded by the program?

---

<div class="post-metadata">

### Author: ![dcnieho](https://avatars.discourse-cdn.com/v4/letter/d/779978/32.png) [@dcnieho](https://psychtoolbox.discourse.group/u/dcnieho)
#### Post date: [July 30, 2020, 8:46am UTC](https://psychtoolbox.discourse.group/t/not-properly-updating-key-presses/3313/4 "2020-07-30T08:46:07Z")

</div>

No, your loop checks the keypress many times in rapid succession. Each time that it sees the key is down, it adds one. So it doesn’t press individual key presses, but every time the check is executed and the key is down. You need to not count until the key has been released again.

Cheers,  
Dee

---

<div class="post-metadata">

### Author: ![stephen](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@stephen](https://psychtoolbox.discourse.group/u/stephen)
#### Post date: [July 30, 2020, 2:47pm UTC](https://psychtoolbox.discourse.group/t/not-properly-updating-key-presses/3313/5 "2020-07-30T14:47:09Z")

</div>

Hi Dee, thanks for you response. What are the implications for actual logging of the key press times? Would this mean that we may be logging presses that are not actually occurring? Or not that we may not be logging all presses? Or both maybe?

---

<div class="post-metadata">

### Author: ![xiangrui](https://avatars.discourse-cdn.com/v4/letter/x/ec9cab/32.png) [@xiangrui](https://psychtoolbox.discourse.group/u/xiangrui)
#### Post date: [July 30, 2020, 5:00pm UTC](https://psychtoolbox.discourse.group/t/not-properly-updating-key-presses/3313/6 "2020-07-30T17:00:27Z")

</div>

What KbCheck reports is if a key is CURRENTLY DOWN, not the key press event. For you purpose, maybe it is better to use KbQueue or KbEvent series functions, although the syntax is a little complicated.

---

<div class="post-metadata">

### Author: ![stephen](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@stephen](https://psychtoolbox.discourse.group/u/stephen)
#### Post date: [July 30, 2020, 7:15pm UTC](https://psychtoolbox.discourse.group/t/not-properly-updating-key-presses/3313/7 "2020-07-30T19:15:57Z")

</div>

Sorry I am new to matlab and ptb. What do you mean by currently down?

---

<div class="post-metadata">

### Author: ![xiangrui](https://avatars.discourse-cdn.com/v4/letter/x/ec9cab/32.png) [@xiangrui](https://psychtoolbox.discourse.group/u/xiangrui)
#### Post date: [July 30, 2020, 7:42pm UTC](https://psychtoolbox.discourse.group/t/not-properly-updating-key-presses/3313/8 "2020-07-30T19:42:58Z")

</div>

A key is pressed down, and not yet released.

---

<div class="post-metadata">

### Author: ![dcnieho](https://avatars.discourse-cdn.com/v4/letter/d/779978/32.png) [@dcnieho](https://psychtoolbox.discourse.group/u/dcnieho)
#### Post date: [July 30, 2020, 8:58pm UTC](https://psychtoolbox.discourse.group/t/not-properly-updating-key-presses/3313/9 "2020-07-30T20:58:29Z")

</div>

As in, you are currently holding it down, physically, on the keyboard. Your loop is checking that many times every second. So even a very short keypress will be found to be down for multiple iterations in your loop. Play with just that bit of code and see what it does with single presses, that may help you understand what is going on (e.g. print some text to the console every time a key is found to be depressed).
