# Activating AS with the release of a keyboard button

**URL:** https://forum.kirupa.com/t/activating-as-with-the-release-of-a-keyboard-button/24531
**Category:** flash
**Created:** [July 1, 2003, 10:13pm UTC](https://forum.kirupa.com/t/activating-as-with-the-release-of-a-keyboard-button/24531 "2003-07-01T22:13:26Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![elion](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@elion](https://forum.kirupa.com/u/elion)
#### Post date: [July 1, 2003, 10:13pm UTC](https://forum.kirupa.com/t/activating-as-with-the-release-of-a-keyboard-button/24531/1 "2003-07-01T22:13:26Z")

</div>

I’m trying to make something where if the viewer holds in an arrow key on their keyboard, it will make a movie clip move across the screen with the AS for it to go to the next frame when the key is pressed. When the arrow key is released, is there any way I could activate an action at that time?

---

<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: [July 1, 2003, 10:19pm UTC](https://forum.kirupa.com/t/activating-as-with-the-release-of-a-keyboard-button/24531/2 "2003-07-01T22:19:22Z")

</div>

Here I think I see what you’re trying to accomplish:

```auto
onClipEvent (load) {
keyList = new Object();
Key.addListener(keyList);
}
onClipEvent (onKeyDown) {
delete onEnterFrame;
if (keyList.getCode == Key.LEFT) {
_x -= 5;
}
//...and yada yada yada, i hope you get the picture
}
onClipEvent (onKeyUp) {
this.onEnterFrame = function () {
//insert your usual enterFrame code here
}
}

```

Now, I haven’t tested this code, so it may or may not work. What it basically does is moves when you press the arrow keys (assuming you were able to add the extra code for right, up, and down), and when you release the key, it will assign the onEnterFrame function.

---

<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: [July 1, 2003, 10:24pm UTC](https://forum.kirupa.com/t/activating-as-with-the-release-of-a-keyboard-button/24531/3 "2003-07-01T22:24:21Z")

</div>

I’m a little confused… ☹ This was my original script…

onClipEvent (enterFrame) {  
if(Key.isDown(Key.LEFT)){  
\_x=\_x-5;  
tellTarget (\_root.char) {  
nextFrame();  
}  
}  
}

I think i figured how to put it all togeather, but as you said the code may be wrong. Heres the output.

Scene=Scene 1, Layer=Layer 1, Frame=1: Line 5: Invalid movie clip event specified.  
onClipEvent (onKeyDown) {

Scene=Scene 1, Layer=Layer 1, Frame=1: Line 12: Invalid movie clip event specified.  
onClipEvent (onKeyUp) {

---

<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: [July 1, 2003, 10:32pm UTC](https://forum.kirupa.com/t/activating-as-with-the-release-of-a-keyboard-button/24531/4 "2003-07-01T22:32:05Z")

</div>

Whoops, those _are_ invalid movie clip events. (those debug windows have yet to fail me :)). Here’s the fixed code:

```auto
onClipEvent (load) {
keyList = new Object();
Key.addListener(keyList);
}
onClipEvent (keyDown) {
delete onEnterFrame;
if (keyList.getCode == Key.LEFT) {
_x -= 5;
}
//...and yada yada yada, i hope you get the picture
}
onClipEvent (keyUp) {
this.onEnterFrame = function () {
//insert your usual enterFrame code here
}
}

```

---

<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: [July 1, 2003, 10:37pm UTC](https://forum.kirupa.com/t/activating-as-with-the-release-of-a-keyboard-button/24531/5 "2003-07-01T22:37:37Z")

</div>

Well, thats fixed now, but theres another problem :-. The keyDown script isn’t activating…

---

<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: [July 2, 2003, 2:29am UTC](https://forum.kirupa.com/t/activating-as-with-the-release-of-a-keyboard-button/24531/6 "2003-07-02T02:29:49Z")

</div>

Just keeping the thread alive… :-\

---

<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: [July 2, 2003, 2:34am UTC](https://forum.kirupa.com/t/activating-as-with-the-release-of-a-keyboard-button/24531/7 "2003-07-02T02:34:30Z")

</div>

Can you be more specif on what u are trying to do?  
🙂

---

<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: [July 2, 2003, 2:40am UTC](https://forum.kirupa.com/t/activating-as-with-the-release-of-a-keyboard-button/24531/8 "2003-07-02T02:40:05Z")

</div>

Trying to learn how it would be possibal for me to make a side scrolling game. (Right now, I have a stick figure that stands in place, then when an arrow key is held in I want it to begin moving and have the movement animation, which is what the whole nextFrame thing is for (That part works).) But right now, I’m having troubles with this script. I tried modifying a bit, but it didn’t really fix it much. The main problem is telling the movie clip to go to a certain frame when the button is released.

---

<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: [July 2, 2003, 2:44am UTC](https://forum.kirupa.com/t/activating-as-with-the-release-of-a-keyboard-button/24531/9 "2003-07-02T02:44:59Z")

</div>

OOH! I figured it out myself!! (-: Thats a first…
