## AS: KeyCode

• 11,405 Views
• 45 Replies
Toast
Toast
• Member since: Apr. 2, 2005
• Offline.
Forum Stats
Member
Level 09
Blank Slate
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.

GustTheASGuy
GustTheASGuy
• Member since: Nov. 2, 2005
• Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: KeyCode 2006-01-20 06:24:54

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

Khao
Khao
• Member since: Sep. 20, 2003
• Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: KeyCode 2006-01-20 07:51:33

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!!!

Nice toast!

GustTheASGuy
GustTheASGuy
• Member since: Nov. 2, 2005
• Offline.
Forum Stats
Member
Level 08
Blank Slate
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.

Toast
Toast
• Member since: Apr. 2, 2005
• Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: KeyCode 2006-01-20 10:36:12

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

David-Silver
David-Silver
• Member since: Sep. 15, 2005
• Offline.
Forum Stats
Member
Level 02
Blank Slate
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

TrueDarkness
TrueDarkness
• Member since: Aug. 31, 2004
• Offline.
Forum Stats
Member
Level 27
Blank Slate
Response to AS: KeyCode 2006-01-20 12:12:10

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

lol
Toast
Toast
• Member since: Apr. 2, 2005
• Offline.
Forum Stats
Member
Level 09
Blank Slate
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.

TrueDarkness
TrueDarkness
• Member since: Aug. 31, 2004
• Offline.
Forum Stats
Member
Level 27
Blank Slate
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

Toast
Toast
• Member since: Apr. 2, 2005
• Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: KeyCode 2006-01-20 12:35:07

3. Thanks. :)

GuyWithHisComp
GuyWithHisComp
• Member since: Nov. 10, 2005
• Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS: KeyCode 2006-01-20 12:49:36

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

FlyingPika
FlyingPika
• Member since: Oct. 30, 2003
• Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to AS: KeyCode 2006-01-20 13:28:06

i use asciitable.com alot...

Toast
Toast
• Member since: Apr. 2, 2005
• Offline.
Forum Stats
Member
Level 09
Blank Slate
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.

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to AS: KeyCode 2006-01-22 09:50:02

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

you fail
Rustygames
Rustygames
• Member since: May. 7, 2005
• Offline.
Forum Stats
Supporter
Level 19
Programmer
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

Toast
Toast
• Member since: Apr. 2, 2005
• Offline.
Forum Stats
Member
Level 09
Blank Slate
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

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to AS: KeyCode 2006-01-22 11:04:27

At 1/22/06 10:01 AM, -Toast- wrote: Hehe, don't you understand...? :P
http://www.newground../topic.php?id=417297

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

...I feel null and void after reading those

GeoKureli
GeoKureli
• Member since: Apr. 1, 2003
• Offline.
Forum Stats
Supporter
Level 20
Game Developer
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)

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to AS: KeyCode 2006-01-22 12:30:44

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.

Rantzien
Rantzien
• Member since: Jan. 27, 2005
• Offline.
Forum Stats
Member
Level 15
Blank Slate
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 =/

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to AS: KeyCode 2006-01-22 12:36:38

At 1/22/06 12:35 PM, Rantzien wrote: He didn't say it was more efficient, he said he liked to do that =/

True, point withdrawn

GeoKureli
GeoKureli
• Member since: Apr. 1, 2003
• Offline.
Forum Stats
Supporter
Level 20
Game Developer
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.

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to AS: KeyCode 2006-01-22 13:06:30

At 1/22/06 01:05 PM, ImpotentBoy2 wrote: 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

It only checks once anyway... if(meh) { }

GeoKureli
GeoKureli
• Member since: Apr. 1, 2003
• Offline.
Forum Stats
Supporter
Level 20
Game Developer
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.

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to AS: KeyCode 2006-01-22 13:28:31

At 1/22/06 01:27 PM, ImpotentBoy2 wrote:
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.

wait, are you using multiple if (meh) {} if (meh) {} statements in the same code?

GeoKureli
GeoKureli
• Member since: Apr. 1, 2003
• Offline.
Forum Stats
Supporter
Level 20
Game Developer
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

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.

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to AS: KeyCode 2006-01-22 13:52:30

At 1/22/06 01:43 PM, ImpotentBoy2 wrote: stuff

GeoKureli
GeoKureli
• Member since: Apr. 1, 2003
• Offline.
Forum Stats
Supporter
Level 20
Game Developer
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

lol serv't

DragonFruit-Rock
DragonFruit-Rock
• Member since: Dec. 31, 2005
• Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to AS: KeyCode 2006-02-22 10:20:08

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?

TheKGB
TheKGB
• Member since: Feb. 6, 2006
• Offline.
Forum Stats
Member
Level 10
Blank Slate
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.