# On Key Press Function :)

**URL:** <https://forum.kirupa.com/t/on-key-press-function/7397>\
**Category:** flash\
**Created:** [October 30, 2002, 3:07am UTC](https://forum.kirupa.com/t/on-key-press-function/7397 "2002-10-30T03:07:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbzfan33](https://avatars.discourse-cdn.com/v4/letter/d/9de0a6/32.png) [@dbzfan33](https://forum.kirupa.com/u/dbzfan33)\
**Post date:** [October 30, 2002, 3:07am UTC](https://forum.kirupa.com/t/on-key-press-function/7397/1 "2002-10-30T03:07:19Z")

</div>

I’m making a game 🙂 with my beginners knowledge.  
and i was wondering why my code doesn’t work.  
i want it so that when you press the key c it will go to (and play)  
frame 15

here’s my code

c.onKeyDown = function() {  
gotoAndPlay(15);  
};

why doesn’t it work?

is it case sensative? if so that’s what the problem is…

---

<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:** [October 30, 2002, 5:11am UTC](https://forum.kirupa.com/t/on-key-press-function/7397/2 "2002-10-30T05:11:39Z")

</div>

You are using it all wrong :-\

```auto
//create object
keyListener = new Object();
//use onKeyDown function
keyListener.onKeyDown = function() {
	//get the Ascii code
	keypressed = Key.getAscii();
	//trace the returned value of the letter pressed
	trace(keypressed);
	//if the letter press is 99 ("c")
	if (keypressed == 99) {
		//trace that you pressed "c"
		trace("you pressed the right key");
	}
};
//add listener to keys for this to work
Key.addListener(keyListener);

```

That should work (untested) to help you understand how it works. Actually, it should help me understand how it works as well considering I never worked with it before :-\

PS: This is case sensitive since the code for lowercase “c” is 99 and the code for the uppercase “C” is 67.

[edit]I checked the code and it actually works![/edit]

---

<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:** [October 30, 2002, 5:28am UTC](https://forum.kirupa.com/t/on-key-press-function/7397/3 "2002-10-30T05:28:30Z")

</div>

☹  
I can’t belive i can’t get this.  
It works but can you tell me the exact code to make it go to fram 15 and play?

---

<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:** [October 30, 2002, 5:31am UTC](https://forum.kirupa.com/t/on-key-press-function/7397/4 "2002-10-30T05:31:30Z")

</div>

Change the if statement in the above code to look like this…

```auto
if (keypressed == 99) {
	gotoAndPlay(15);
	}

```

Also, you can remove the code that says…

```auto
trace(keypressed);

```

It is right above the if statement. I just put that there in case you needed to know the code for any other letter you could just press it and see.

---

<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:** [October 30, 2002, 5:51am UTC](https://forum.kirupa.com/t/on-key-press-function/7397/5 "2002-10-30T05:51:24Z")

</div>

And if you want it to work on either uppercase or lowercase “c” change the if statement to look like this…

```auto
	if (keypressed == 99 or keypressed == 67) {
		trace("you pressed the right key");
	}

```

Sorry if I am being annoying, but I just figured all this out now too and I am trying to figure out how it works and such by experimenting with my original code.

---

<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:** [October 30, 2002, 6:01am UTC](https://forum.kirupa.com/t/on-key-press-function/7397/6 "2002-10-30T06:01:35Z")

</div>

thanks,

what’s the code i use for if c or C is pressed it won’t do that stuff?

---

<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:** [October 30, 2002, 6:07am UTC](https://forum.kirupa.com/t/on-key-press-function/7397/7 "2002-10-30T06:07:04Z")

</div>

Well if you don’t want it to do anything when pressed, then you don’t have to add any statement about c or C.

I erred in my last one

I left the trace in, but it should really be gotoAndPlay(15);
