00:00
00:00
Newgrounds Background Image Theme

Binguumajinn just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS: KeyCode 2006-01-20 06:18:31


'Better than having sex with a noob'

AS: KeyCode

So all of you know how to check if Space or Enter is pressed, by simply applying the following code:
if(Key.isDown(Key.SPACE))

But you have always wondered how to use WASD and 1 2 3. That's why I'm making this tutorial. It will teach you how to use different keys for your games, better than up down left right.

KeyCodes: How to use?

This is very simple and alike to other keys. Simply use:
if(Key.isDown(<keycode here>))

What are the keycodes?

Numbers

The following keycode are codes you need to use for all the numbers appearing on your keyboard: 1,2,3,4,5,6,7,8,9,0.
1 = 49.
2 = 50.
3 = 51.
4 = 52.
5 = 53.
6 = 54.
7 = 55.
8 = 56.
9 = 57.
0 = 58.

Example:
if(Key.isDown(50)){
trace("Yay!");
}

That would trace 'yay' if the key '2' is pressed.

KeyCodes: Alphabet

Now this is very simple, just like the numbers every letter of the alphabet has its own code.

a = 65
b = 66
c = 67
d = 68
e = 69
f = 70
g = 71
h = 72
i = 73
j = 74
k = 75
l = 76
m = 77
n = 78
o = 79
p = 80
q = 81
r = 82
s = 83
t = 84
u = 85
v = 86
w = 87
x = 88
y = 89
z = 90

Example:
if(Key.isDown(65)){
trace("You pressed 'A' ");
}

Output for if you press A:
You pressed 'A'

That's it, you're done.

Have a nice day.

-Toast.

Bai.


BBS Signature

Response to AS: KeyCode 2006-01-20 06:24:54


Did you know 'i//' would divide the var i with itself? :D


BBS Signature

Response to AS: KeyCode 2006-01-20 07:53:33


You hurt my feelings. ;o;

Alright, I'll get back to doing something useful then.

BBS Signature

Response to AS: KeyCode 2006-01-20 10:36:12


Good idea, I'll make a AS: // ;)


BBS Signature

Response to AS: KeyCode 2006-01-20 12:11:18


Great script Toast, you should of included this though (mini tut I'm about to post):

You can also easily make something happen when a key is released. By doing so you would use the keyUp method.
onClipEvent(keyUp){
//stuff
}

I don't see a need to explain that code, it is self explanatory, but what it does is register than when any key is released and not pressed, then you can add your actions so it tells something to do something.

How would I specify that for a certain key?

Simple, this is where variables come in. Simply make it so when a key is pressed, a variable is defined (use Toat's tutorial above).
For example:
onClipEvent(enterFrame){
up=0
if(Key.isDown(Key.UP)){
up=1
}
}
onClipEvent(keyUp){
if(up==1){
//stuff
}
}

That means that when you press the key UP, the variable 'up' is defined as 1. With that said, when any key is released and up is = to 1 (from when you pressed UP) then something happens.

So yeah that's pretty much it, sorry if what I said doesn't make much sense, I hope it did. I don't write tutorials much as you can tell, but hope that helps. It is a really good way for something to happen when you release a key.

Oh, great tutroial Toast, that will help a lot of n00bies :P

Response to AS: KeyCode 2006-01-20 12:12:10


Oops sorry, that was my alt account. above post was posted by me

lol

Response to AS: KeyCode 2006-01-20 12:21:42


1. I didn't post any script...

2. Nah, this is a KeyCode tutorial, not Key is down and key is up.


BBS Signature

Response to AS: KeyCode 2006-01-20 12:31:17


At 1/20/06 12:21 PM, -Toast- wrote: 1. I didn't post any script...

2. Nah, this is a KeyCode tutorial, not Key is down and key is up.

oops.

1. Didn't mean to say "great script" meant tutorial.

2. True, oh well it's okay, my fault, good job it is a very helpful tutorial :P

Response to AS: KeyCode 2006-01-20 12:35:07


3. Thanks. :)


BBS Signature

Response to AS: KeyCode 2006-01-20 12:49:36


4. Here is a simple Keygetter I made: link [in my sig]


BBS Signature

Response to AS: KeyCode 2006-01-20 13:28:06


i use asciitable.com alot...

Response to AS: KeyCode 2006-01-22 09:30:58


4. That pwns. I think there should be a section in AS: Main for none NG topic AS tutorials.


BBS Signature

Response to AS: KeyCode 2006-01-22 09:57:17


At 1/20/06 07:51 AM, Khao wrote:
At 1/20/06 06:24 AM, -Gust- wrote: Did you know 'i//' would divide the var i with itself? :D
did you know nobody fucking care so stop posting this in every AS thread dumbass!!!

Whoa too much coffee for someone
Crap tutorial toast stop trying


- Matt, Rustyarcade.com

Response to AS: KeyCode 2006-01-22 10:01:02


At 1/22/06 09:50 AM, Sekky wrote:
At 1/20/06 06:24 AM, -Gust- wrote: Did you know 'i//' would divide the var i with itself? :D
1) no it won't, because // is a comment line

2) i divided by itself is 1, hence it's fucking redundant anyway

Hehe, don't you understand...? :P
http://www.newground../topic.php?id=417297

http://www.newground../topic.php?id=417284


BBS Signature

Response to AS: KeyCode 2006-01-22 11:58:12


one thing i like to do in flash when dealing lots of keycodes is, i declare a variable = to the Key.isDown condition. id put this right below the start of the clipEvent

_global.a=Key.isDown(65)

then when i wanna use it i just say
if(a){}
its great when you use alot of ifs of the same kind(like for doubletap keys)

Response to AS: KeyCode 2006-01-22 12:35:52


At 1/22/06 12:30 PM, Sekky wrote: how is that anymore efficient than if (Key.isDown(65)) { }

He didn't say it was more efficient, he said he liked to do that =/


BBS Signature

Response to AS: KeyCode 2006-01-22 13:05:38


At 1/22/06 12:30 PM, Sekky wrote:
At 1/22/06 11:58 AM, ImpotentBoy2 wrote: _global.a=Key.isDown(65)
if(a){}
how is that anymore efficient than if (Key.isDown(65)) { }

it's actually less efficient doing it that way.

why is it less efficient, because it has more lines?
well this way only checks if its down once, instead of checking over and over it just refers to the boolean. so i think it would bemore efficient. but when i tested it, i tried haveing like 40 ifs with
_x++;_x--;_rotation=random(360)
and they took less than 1 millisecond to do. but every once and a while the one with Key.isDown(65) in each if would say 1 millisecond instead of 0 and they were tested simultaneously so bleh.

Response to AS: KeyCode 2006-01-22 13:27:28


At 1/22/06 01:06 PM, Sekky wrote: It only checks once anyway... if(meh) { }

are you sure about that? out of 158 tests, my way took shorter timer to process 58 times, the regular way was shorter 7 times. most of the time they both took 0 milliseconds saince i guess i didnt try anything too computer intesive.

Response to AS: KeyCode 2006-01-22 13:43:11


At 1/22/06 01:28 PM, Sekky wrote: wait, are you using multiple if (meh) {} if (meh) {} statements in the same code?

this is the code im using to check

onClipEvent (load) {
var ties:Number = 0;
var wins:Number = 0;
var loss:Number = 0;
var said:Boolean = false;
}
onClipEvent (enterFrame) {
var t:Number = getTimer();
//time it started my ways test
var a:Boolean = Key.isDown(65);
for (var i = 0; i < 5000; i++) {
if (a) {
_x++;
_x--;
_rotation = random(360);
}
}
if (a) {
var myTime:Number = getTimer() - t;
//time it took for my ways test
}
t = getTimer();
//time it started other ways test
for (i = 0; i < 5000; i++) {
if (Key.isDown(65)) {
_x++;
_x--;
_rotation = random(360);
}
}
if (Key.isDown(65)) {
var otherTime:Number = getTimer() - t;
//time it took for other ways test, all code afterwards is not counted in that number so it should be 100% accurate.
myTime < otherTime ? wins++ : myTime > otherTime ? loss++ : ties++;
//tallys up score
said = false;
//allows it to trace score
} else if (!a && (wins || losses || ties) && !said) {
trace([wins, loss, ties]);
said = true;
//so it only traces once
}
_rotation = 0;
}

i changed the for loop from 50 to 5000 to get a more accurate answer.

Response to AS: KeyCode 2006-01-22 14:33:23


At 1/22/06 01:52 PM, Sekky wrote:
At 1/22/06 01:43 PM, ImpotentBoy2 wrote: stuff
Gotcha, my bad

lol serv't

Response to AS: KeyCode 2006-02-22 10:20:08


this was SOOOOOOO helpful! thanks!


If a man that always tells the truth comes up to you and says that another man always tells lies, and the man who always lies come up to you and says "I'm lying", then is he?

BBS Signature

Response to AS: KeyCode 2006-02-22 22:44:13


Sweet tut Toast. I've been wondering how to use letter and number keys. Reminds me of graphing calc programming.

Response to AS: KeyCode 2006-03-25 06:27:59


with the:
if(Key.isDown(65)){
trace("yay");
}
How would I ad code to the above to go to a certain frame label and play what on that timeline. for example I have a boxer on the main timeline (a MC) and inside that movie clip is another movie clip called fighter(instance name) which is key framed out with frame labels like punch (label) kick(label).

I'm having trouble writing the above code to make my boxer punch when the A(65) key is hit. can you help me with this code?

P.S. where do I put the code? (in a frame on the main timeline or in the boxer MC or in the fighter Movie Clip?)

If you can help I thank you soooo very much! im pulling my hair out. lol
Mike

Response to AS: KeyCode 2006-04-02 11:06:20


You could just:
if( Key.isDown("A".charCodeAt(0)) ) {
//
}

Response to AS: KeyCode 2006-04-09 12:40:46


Other Actionscript keycodes

Backspace
8

Tab
9

Clear
12

Enter
13

Shift
16

Control
17

Alt
18

Caps Lock
20

Esc
27

Spacebar
32

Page Up
33

Page Down
34

End
35

Home
36

Left Arrow
37

Up Arrow
38

Right Arrow
39

Down Arrow
40

Insert
45

Delete
46

Help
47

Num Lock
144

; :
186

= +
187

- _
189

/ ?
191

` ~
192

[ {
219

\ |
220

] }
221

" '
222

Response to AS: KeyCode 2006-06-14 19:34:48


I'm having a hard time understanding this.. I'm a real noob when it comes to actionscripting. What I want is a code that will take me to a certain frame (or scene) when a button is pressed at the right time (kinda like the Naked Hero games).

So, from what I've gathered, I'd have to use:

if(Key.isDown(65)){
gotoAndPlay("Scene 1", 1);
}

That would take the flash to scene 1, frame 1 when the viewer presses the "a" key, right?

Well I tried and nothing happened! I didn't get any error report, just nothing happened when I tested it.. What am I doing wrong?

Response to AS: KeyCode 2006-06-14 19:38:49


(sorry for double post)

do I need to put it in a movie clip or in a keyframe?? Maybe that's what I'm doing wrong....

Response to AS: KeyCode 2006-07-06 00:11:33


button/code

The following table lists the function keys on a standard keyboard, with the corresponding ASCII
key code values that are used to identify the keys in ActionScript:

letter keypad code

A 65
B 66
C 67
D 68
E 69
F 70
G 71
H 72
I 73
J 74
K 75
L 76
M 77
N 78
O 79
P 80
Q 81
R 82
S 83
T 84
U 85
V 86
W 87
X 88
Y 89
Z 90
number key code
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
Numeric keypad key Key code

Numbpad 0 96
Numbpad 1 97
Numbpad 2 98
Numbpad 3 99
Numbpad 4 100
Letter or number key Key code

Function keys

Numbpad 5 101
Numbpad 6 102
Numbpad 7 103
Numbpad 8 104
Numbpad 9 105
Multiply 106
Add 107
Enter 13
Subtract 109
Decimal 110
Divide 111

F1 112
F2 113
F3 114
F4 115
F5 116
F6 117
F7 118
F8 119
F9 120
F10 This key is reserved by the system and cannot be used in ActionScript.
F11 122
F12 123
F13 124
F14 125
F15 126

Other keys
The following table lists keys on a standard keyboard other than letters, numbers, numeric
keypad keys, or function keys, with the corresponding ASCII key code values that are used to
identify the keys in ActionScript:
Key Key code

Backspace 8
Tab 9
Clear 12
Enter 13
Shift 16
Control 17
Alt 18
Caps Lock 20
Esc 27
Spacebar 32
Page Up 33
Page Down 34
End 35
Home 36
Left Arrow 37
Up Arrow 38
Right Arrow 39
Down Arrow 40
Insert 45
Delete 46
Help 47
Num Lock 144
; : 186
= + 187
- _ 189
/ ? 191
` ~ 192
[ { 219
\ | 220
] } 221
" ' 222

Response to AS: KeyCode 2007-08-02 16:49:30


Um, none of this seems to be working for me,
i put:

if(Key.isDown(65)){
play();
}

on the first keyframe where the stop(); action is...
I load the movie I hit A and it doesnt play the movie. What am i doing wrong?
(i know i am such a newb)

Response to AS: KeyCode 2007-08-02 17:20:16


onClipEvent(enterFrame){
if(Key.isDown(65)){
play();
}
}

Paste that in the action panel of your movie clip.


BBS Signature