# Keylistener issues in Flash 8?

**URL:** <https://forum.kirupa.com/t/keylistener-issues-in-flash-8/185026>\
**Category:** Uncategorized\
**Created:** [April 10, 2006, 1:05pm UTC](https://forum.kirupa.com/t/keylistener-issues-in-flash-8/185026 "2006-04-10T13:05:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ophyr](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@Ophyr](https://forum.kirupa.com/u/Ophyr)\
**Post date:** [April 10, 2006, 1:05pm UTC](https://forum.kirupa.com/t/keylistener-issues-in-flash-8/185026/1 "2006-04-10T13:05:49Z")

</div>

Hi Guys,

I was wondering if anyone had a similiar problem I have ?

I don’t have the code at hand, but it’s not too hard to explain.  
The setup is as follows :

I have a listener for keys, and a switch statement that catches the key events. If I press ‘left’ for example, a variable moveLeft becomes true. If I release the left button, moveLeft will become false.

This seems to work, until I do the following: Press and hold left, press and hold any other button aswell. If I release the buttons, it will not register the keyUp event for some reason. I know the code worked in Flash MX 2004 because I literally copy-pasted it :cross-eye

For now I work with Key.isDown(Key.LEFT), but that looks just plain ugly I.m.h.o…

---

<div class="post-metadata">

**Author:** ![Joppe](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@Joppe](https://forum.kirupa.com/u/Joppe)\
**Post date:** [April 10, 2006, 2:43pm UTC](https://forum.kirupa.com/t/keylistener-issues-in-flash-8/185026/2 "2006-04-10T14:43:21Z")

</div>

Soo… Mind posting the code?

---

<div class="post-metadata">

**Author:** ![Ophyr](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@Ophyr](https://forum.kirupa.com/u/Ophyr)\
**Post date:** [April 10, 2006, 2:46pm UTC](https://forum.kirupa.com/t/keylistener-issues-in-flash-8/185026/3 "2006-04-10T14:46:13Z")

</div>

I was about to, but I have it on an external HDD that I don’t have here 😃 It’s basically 1 on 1 with the description above though, I thought that was enough info, but I’ll post code tonight 🙂

---

<div class="post-metadata">

**Author:** ![Joppe](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@Joppe](https://forum.kirupa.com/u/Joppe)\
**Post date:** [April 10, 2006, 2:54pm UTC](https://forum.kirupa.com/t/keylistener-issues-in-flash-8/185026/4 "2006-04-10T14:54:44Z")

</div>

Ok, its probably enought with the info but I like to see coding and see if I can help 😛

---

<div class="post-metadata">

**Author:** ![Ophyr](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@Ophyr](https://forum.kirupa.com/u/Ophyr)\
**Post date:** [April 10, 2006, 6:13pm UTC](https://forum.kirupa.com/t/keylistener-issues-in-flash-8/185026/5 "2006-04-10T18:13:03Z")

</div>

Well, here’s the code

```auto
    keyListener = new Object();
    
    Key.addListener(keyListener);
        
    keyListener.onKeyDown = function() {
        KeyDown(Key.getCode());
    };
    keyListener.onKeyUp = function() {
        KeyUp(Key.getCode());
    };
        
    KeyDown = function (keyCode) {
        switch (keyCode) {
        case 37 :
            _root.moveLeft = true;
            break;
        case 39 :
            _root.moveRight = true;
            break;
        case 38 :
            _root.jump = true;
            break;
        case 80 :
            togglePause();
            break;
        }
    };
    KeyUp = function (keyCode) {
        switch (keyCode) {
        case 37 :
            _root.moveLeft = false;
            break;
        case 39 :
            _root.moveRight = false;
            break;
        case 38 :
            _root.jump = false;
            break;
        }
    };

```

You can use it like this

```auto
if(_root.moveLeft) doStuff();

```

---

<div class="post-metadata">

**Author:** ![Psykloak1](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@Psykloak1](https://forum.kirupa.com/u/Psykloak1)\
**Post date:** [April 10, 2006, 8:46pm UTC](https://forum.kirupa.com/t/keylistener-issues-in-flash-8/185026/6 "2006-04-10T20:46:57Z")

</div>

Thats a big reason that I stopped using keyListeners and stuff.

The way I do it now (and this mightturn out to be worse but it works great for me) is put an “if (Key.isDown(87))” In an "onClipEvent(enterFrame) " in a movie clip. Like I said, maybe this is not the best way or a coiincedance, but it solved a bunch of problems I had using keyListener

so:

```auto

onClipEvent(enterFrame) {
	if (Key.isDown(87)) //w
		_y=-5;
	if (Key.isDown(65)) //a
		_x=-5;
	if (Key.isDown(83)) //s
		_y=5;
	if (Key.isDown(68)) //d
		_x=5;
}

```

when put in a movie clip would make the charecter move around using WASD
