# How to execute stuff as long as a key is pressed down

**URL:** https://discourse.libcinder.org/t/how-to-execute-stuff-as-long-as-a-key-is-pressed-down/2034
**Category:** Using Cinder
**Created:** [July 29, 2022, 3:53pm UTC](https://discourse.libcinder.org/t/how-to-execute-stuff-as-long-as-a-key-is-pressed-down/2034 "2022-07-29T15:53:40Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Archspect](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/archspect/32/991_2.png) [@Archspect](https://discourse.libcinder.org/u/Archspect)
#### Post date: [July 29, 2022, 3:53pm UTC](https://discourse.libcinder.org/t/how-to-execute-stuff-as-long-as-a-key-is-pressed-down/2034/1 "2022-07-29T15:53:40Z")

</div>

I was trying to execute some code (In this case move the texture a couple of pixels) but I ran into a problem. There are functions `keyDown()` and `keyUp()` but I couldn’t find anything like `keyHold()` or `keyPressed()`. How is it possible to execute some code only when a key is pressed down and stop when it’s released?  
Here is my failed attempt at achieving it. (I had no idea how to)

```auto
#include <cinder/app/App.h>
#include <cinder/app/RendererGl.h>
#include <cinder/Rand.h>
#include <cinder/ImageIo.h>
#include <cinder/app/KeyEvent.h>
#include <cinder/gl/draw.h>
#include <cinder/gl/wrapper.h>
#include <filesystem>
#include <glm/fwd.hpp>

using namespace ci;
using namespace ci::app;

class BasicApp : public App {
  public:
	void setup() override;
	void update() override;
	void draw() override;
	void resize() override;
	void keyUp(KeyEvent keyEvent) override;
	void keyDown(KeyEvent keyEvent) override;

	gl::TextureRef texture;
	float x,y,z;
	bool isKeyUp;
};

void prepareSettings( BasicApp::Settings* settings ) 
{
	settings->setWindowSize(1000,700);
}

void BasicApp::setup() 
{
	fs::current_path("/home/archspect/Code/Cinder/BasicApp/");
	texture = gl::Texture::create(loadImage("assets/texture.png"));
}

void BasicApp::resize()
{
	gl::setMatricesWindow(getWindowSize());
}

void BasicApp::update() {}

void BasicApp::keyUp(KeyEvent keyEvent)
{
	if(keyEvent.KEY_UP)
	{
		isKeyUp = true;
	}
}

void BasicApp::keyDown(KeyEvent keyEvent)
{
	if (keyEvent.KEY_UP)
	{	
		isKeyUp = false;
		if (!isKeyUp)
		{
			x++;
			y++;
		}
	}
}
void BasicApp::draw()
{
	gl::clear();
	gl::draw(texture, vec3(x,y,z));
	
}
CINDER_APP( BasicApp, RendererGl, prepareSettings )

```

---

<div class="post-metadata">

### Author: ![lithium](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/lithium/32/38_2.png) [@lithium](https://discourse.libcinder.org/u/lithium)
#### Post date: [July 30, 2022, 3:41am UTC](https://discourse.libcinder.org/t/how-to-execute-stuff-as-long-as-a-key-is-pressed-down/2034/2 "2022-07-30T03:41:58Z")

</div>

You’re very close, you just need to move your logic for what happens when `isKeyUp == true` to somewhere that is called every frame, like `BasicApp::update()` or `BasicApp::draw()`.

Also, the way to check for specific key events is:

```cpp
if ( keyEvent.getCode() == KeyEvent::KEY_UP )`
{
//...
}

```

Right now you’re just evaluating whether the constant value for `KEY_UP` is not zero, which is always `true` 🙂

A
