# While key is pressed

**URL:** <https://forums.solar2d.com/t/while-key-is-pressed/150884>\
**Category:** Win32 Desktop\
**Created:** [January 4, 2020, 9:15pm UTC](https://forums.solar2d.com/t/while-key-is-pressed/150884 "2020-01-04T21:15:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![owlondrugs](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/o/f17d59/32.png) [@owlondrugs](https://forums.solar2d.com/u/owlondrugs)\
**Post date:** [January 4, 2020, 9:15pm UTC](https://forums.solar2d.com/t/while-key-is-pressed/150884/1 "2020-01-04T21:15:27Z")

</div>

How do I make the action happen every time the button is pressed?

```
local function onKeyEvent( event ) if ( event.keyName == "up" ) then ballvel = ballvel - 0.1; end if ( event.keyName == "down" ) then ballvel = ballvel + 0.1; end return true end Runtime:addEventListener( "key", onKeyEvent ) function update(event) balloon.y = balloon.y + ballvel; print(ballvel); end Runtime:addEventListener("enterFrame", update)
```

---

<div class="post-metadata">

**Author:** ![XeduR](https://forums.solar2d.com/user_avatar/forums.solar2d.com/xedur/32/256_2.png) [@XeduR](https://forums.solar2d.com/u/XeduR)\
**Post date:** [January 5, 2020, 12:53pm UTC](https://forums.solar2d.com/t/while-key-is-pressed/150884/2 "2020-01-05T12:53:29Z")

</div>

Your title says “while” and body says “every time the button is pressed”, so it’s not 100% clear which you mean.

Simplest approach would be be to just toggle a function’s listener on or off depending on whether a desired key is held down or not and then adjust the direction based on what key was last pressed. Something along these lines:

```
local value, change = 1000 local function updateValue() value = value + change print( value ) end local function onKeyEvent( event ) if event.keyName == "down" then if event.phase == "down" then change = -1 Runtime:addEventListener( "enterFrame", updateValue ) else Runtime:removeEventListener( "enterFrame", updateValue ) end elseif event.keyName == "up" then if event.phase == "down" then change = 1 Runtime:addEventListener( "enterFrame", updateValue ) else Runtime:removeEventListener( "enterFrame", updateValue ) end end return false end Runtime:addEventListener( "key", onKeyEvent )
```

---

<div class="post-metadata">

**Author:** ![owlondrugs](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/o/f17d59/32.png) [@owlondrugs](https://forums.solar2d.com/u/owlondrugs)\
**Post date:** [January 5, 2020, 1:30pm UTC](https://forums.solar2d.com/t/while-key-is-pressed/150884/3 "2020-01-05T13:30:57Z")

</div>

> [@XeduR](#):
>
> Your title says “while” and body says “every time the button is pressed”, so it’s not 100% clear which you mean.
> 
> Simplest approach would be be to just toggle a function’s listener on or off depending on whether a desired key is held down or not and then adjust the direction based on what key was last pressed. Something along these lines:
> 
> ```
> local value, change = 1000 local function updateValue() value = value + change print( value ) end local function onKeyEvent( event ) if event.keyName == "down" then if event.phase == "down" then change = -1 Runtime:addEventListener( "enterFrame", updateValue ) else Runtime:removeEventListener( "enterFrame", updateValue ) end elseif event.keyName == "up" then if event.phase == "down" then change = 1 Runtime:addEventListener( "enterFrame", updateValue ) else Runtime:removeEventListener( "enterFrame", updateValue ) end end return false end Runtime:addEventListener( "key", onKeyEvent )
> 
> ```

&nbsp;I have a balloon.

It always moves up or down depending on its ballvel variable.

when a key is pressed, this variable changes.

How do I make the variable change not in steps, but all the time while the key is pressed?

---

<div class="post-metadata">

**Author:** ![XeduR](https://forums.solar2d.com/user_avatar/forums.solar2d.com/xedur/32/256_2.png) [@XeduR](https://forums.solar2d.com/u/XeduR)\
**Post date:** [January 5, 2020, 1:32pm UTC](https://forums.solar2d.com/t/while-key-is-pressed/150884/4 "2020-01-05T13:32:55Z")

</div>

How? By reading my previous answer and adopting it to yours.

---

<div class="post-metadata">

**Author:** ![pixec](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/p/8e8cbc/32.png) [@pixec](https://forums.solar2d.com/u/pixec)\
**Post date:** [January 5, 2020, 4:47pm UTC](https://forums.solar2d.com/t/while-key-is-pressed/150884/5 "2020-01-05T16:47:53Z")

</div>

lol 😃

---

<div class="post-metadata">

**Author:** ![owlondrugs](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/o/f17d59/32.png) [@owlondrugs](https://forums.solar2d.com/u/owlondrugs)\
**Post date:** [January 5, 2020, 6:33pm UTC](https://forums.solar2d.com/t/while-key-is-pressed/150884/6 "2020-01-05T18:33:05Z")

</div>

> [@XeduR](#):
>
> How? By reading my previous answer and adopting it to yours.

All the same, adding to the variable goes in steps.

---

<div class="post-metadata">

**Author:** ![XeduR](https://forums.solar2d.com/user_avatar/forums.solar2d.com/xedur/32/256_2.png) [@XeduR](https://forums.solar2d.com/u/XeduR)\
**Post date:** [January 5, 2020, 8:03pm UTC](https://forums.solar2d.com/t/while-key-is-pressed/150884/7 "2020-01-05T20:03:28Z")

</div>

I’m not sure what you mean. My code increases or decreases the value as long as a key is held down. Have you even tried running it?
