While key is pressed

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)

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 )

 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?

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

lol :smiley:

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

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?