# Problem with Key events

**URL:** <https://forum.kirupa.com/t/problem-with-key-events/11292>\
**Category:** flash\
**Created:** [January 15, 2003, 2:30am UTC](https://forum.kirupa.com/t/problem-with-key-events/11292 "2003-01-15T02:30:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pom](https://avatars.discourse-cdn.com/v4/letter/p/f07891/32.png) [@pom](https://forum.kirupa.com/u/pom)\
**Post date:** [January 15, 2003, 2:30am UTC](https://forum.kirupa.com/t/problem-with-key-events/11292/1 "2003-01-15T02:30:52Z")

</div>

OK folks, I have a problem with that game I’m doing for the battle (don’t laugh). I have this function in the main timeline:

```auto
function listenForKeys(){
	if (Key.isDown(Key.RIGHT)){
		hero.xmov+=hero.moveIncrement;
	}
	if (Key.isDown(Key.LEFT)){
		hero.xmov-=hero.moveIncrement;
	}
	if (Key.isDown(Key.UP)){
		hero.ymov-=hero.moveIncrement;
	}
	if (Key.isDown(Key.DOWN)){
		hero.ymov+=hero.moveIncrement;
	}
	if (Key.isDown(Key.SPACE)){
		missileLaunch();
	}
}

```

and I call it onEnterFrame, so that when I press and hold space, a new missile is launched. Which is not very realistic. What I’d like to do is set a maximum frequency for the missiles.

I thought about setInterval, but I would need to detect the moment I release the space key to clear the interval.

Does anybody see an easy way of doing that?

pom :trout:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 15, 2003, 2:50am UTC](https://forum.kirupa.com/t/problem-with-key-events/11292/2 "2003-01-15T02:50:38Z")

</div>

```auto

Key.addListener(this);
this.onKeyUp = function(){
	if(!Key.isDown(Key.SPACE)) clearInterval(missle_interval)
}

```

?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 15, 2003, 3:01am UTC](https://forum.kirupa.com/t/problem-with-key-events/11292/3 "2003-01-15T03:01:32Z")

</div>

Well, the problem is that I have the space bar and the 4 arrows. So each time I release the arrow keys, that will clear the interval, no?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 15, 2003, 3:08am UTC](https://forum.kirupa.com/t/problem-with-key-events/11292/4 "2003-01-15T03:08:11Z")

</div>

no it wont clear the interval unless space is not pressed [if(!Key.isDown(Key.SPACE))]. If you release an arrow key, yes, the onKeyUp will be called, but the interval will no be cleared unless the SPACE is not being pressed… and if its not the key being released, it wont matter because the interval wont be set anyway.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 16, 2003, 12:15am UTC](https://forum.kirupa.com/t/problem-with-key-events/11292/5 "2003-01-16T00:15:16Z")

</div>

:trout: \<-- me  
Dammit you’re right. Now why did I think I couldn’t do it that way? :hangover: :hangover:

Thanks Sen 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 16, 2003, 2:10am UTC](https://forum.kirupa.com/t/problem-with-key-events/11292/6 "2003-01-16T02:10:14Z")

</div>

np 🙂

glad you got it all figured out
