Forum Topic: AS: KeyCode

(5,977 views • 44 replies)

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
None

Toast

Reply To Post Reply & Quote

Posted at: 1/20/06 06:18 AM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,414

'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.

I put on my robe and wizard hat

Why don't you check out my check out my userpage?

BBS Signature

None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 1/20/06 06:24 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 9,716

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

haXe AliceML Box2dLite Learn AS
#ngprogramming at irc.freenode.net
OVER NINE THOUSAAAAND!!!


None

Khao

Reply To Post Reply & Quote

Posted at: 1/20/06 07:51 AM

Khao EVIL LEVEL 19

Sign-Up: 09/20/03

Posts: 2,767

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!


None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 1/20/06 07:53 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 9,716

You hurt my feelings. ;o;

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

haXe AliceML Box2dLite Learn AS
#ngprogramming at irc.freenode.net
OVER NINE THOUSAAAAND!!!


None

Toast

Reply To Post Reply & Quote

Posted at: 1/20/06 10:36 AM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,414

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

I put on my robe and wizard hat

Why don't you check out my check out my userpage?

BBS Signature

None

David-Silver

Reply To Post Reply & Quote

Posted at: 1/20/06 12:11 PM

David-Silver EVIL LEVEL 02

Sign-Up: 09/15/05

Posts: 43

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


None

TrueDarkness

Reply To Post Reply & Quote

Posted at: 1/20/06 12:12 PM

TrueDarkness EVIL LEVEL 27

Sign-Up: 08/31/04

Posts: 4,718

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

lol

None

Toast

Reply To Post Reply & Quote

Posted at: 1/20/06 12:21 PM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,414

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

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

I put on my robe and wizard hat

Why don't you check out my check out my userpage?

BBS Signature

None

TrueDarkness

Reply To Post Reply & Quote

Posted at: 1/20/06 12:31 PM

TrueDarkness EVIL LEVEL 27

Sign-Up: 08/31/04

Posts: 4,718

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


None

Toast

Reply To Post Reply & Quote

Posted at: 1/20/06 12:35 PM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,414

3. Thanks. :)

I put on my robe and wizard hat

Why don't you check out my check out my userpage?

BBS Signature

None

GuyWithHisComp

Reply To Post Reply & Quote

Posted at: 1/20/06 12:49 PM

GuyWithHisComp LIGHT LEVEL 24

Sign-Up: 11/10/05

Posts: 4,027

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

BBS Signature

None

FlyingPika

Reply To Post Reply & Quote

Posted at: 1/20/06 01:28 PM

FlyingPika LIGHT LEVEL 02

Sign-Up: 10/30/03

Posts: 91

i use asciitable.com alot...


None

Toast

Reply To Post Reply & Quote

Posted at: 1/22/06 09:30 AM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,414

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

I put on my robe and wizard hat

Why don't you check out my check out my userpage?

BBS Signature

None

Sekky

Reply To Post Reply & Quote

Posted at: 1/22/06 09:50 AM

Sekky EVIL LEVEL 24

Sign-Up: 03/17/03

Posts: 6,196

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

None

Rustygames

Reply To Post Reply & Quote

Posted at: 1/22/06 09:57 AM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,429

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


None

Toast

Reply To Post Reply & Quote

Posted at: 1/22/06 10:01 AM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,414

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

I put on my robe and wizard hat

Why don't you check out my check out my userpage?

BBS Signature

None

Sekky

Reply To Post Reply & Quote

Posted at: 1/22/06 11:04 AM

Sekky EVIL LEVEL 24

Sign-Up: 03/17/03

Posts: 6,196

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


None

ImpotentBoy2

Reply To Post Reply & Quote

Posted at: 1/22/06 11:58 AM

ImpotentBoy2 LIGHT LEVEL 18

Sign-Up: 04/01/03

Posts: 5,039

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)


None

Sekky

Reply To Post Reply & Quote

Posted at: 1/22/06 12:30 PM

Sekky EVIL LEVEL 24

Sign-Up: 03/17/03

Posts: 6,196

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.


None

Rantzien

Reply To Post Reply & Quote

Posted at: 1/22/06 12:35 PM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,422

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

None

Sekky

Reply To Post Reply & Quote

Posted at: 1/22/06 12:36 PM

Sekky EVIL LEVEL 24

Sign-Up: 03/17/03

Posts: 6,196

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


None

ImpotentBoy2

Reply To Post Reply & Quote

Posted at: 1/22/06 01:05 PM

ImpotentBoy2 LIGHT LEVEL 18

Sign-Up: 04/01/03

Posts: 5,039

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.


None

Sekky

Reply To Post Reply & Quote

Posted at: 1/22/06 01:06 PM

Sekky EVIL LEVEL 24

Sign-Up: 03/17/03

Posts: 6,196

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) { }


None

ImpotentBoy2

Reply To Post Reply & Quote

Posted at: 1/22/06 01:27 PM

ImpotentBoy2 LIGHT LEVEL 18

Sign-Up: 04/01/03

Posts: 5,039

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.


None

Sekky

Reply To Post Reply & Quote

Posted at: 1/22/06 01:28 PM

Sekky EVIL LEVEL 24

Sign-Up: 03/17/03

Posts: 6,196

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?


None

ImpotentBoy2

Reply To Post Reply & Quote

Posted at: 1/22/06 01:43 PM

ImpotentBoy2 LIGHT LEVEL 18

Sign-Up: 04/01/03

Posts: 5,039

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.


None

Sekky

Reply To Post Reply & Quote

Posted at: 1/22/06 01:52 PM

Sekky EVIL LEVEL 24

Sign-Up: 03/17/03

Posts: 6,196

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

Gotcha, my bad


None

ImpotentBoy2

Reply To Post Reply & Quote

Posted at: 1/22/06 02:33 PM

ImpotentBoy2 LIGHT LEVEL 18

Sign-Up: 04/01/03

Posts: 5,039

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


Happy

DragonFruit-Rock

Reply To Post Reply & Quote

Posted at: 2/22/06 10:20 AM

DragonFruit-Rock NEUTRAL LEVEL 13

Sign-Up: 12/31/05

Posts: 1,245

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

None

TheKGB

Reply To Post Reply & Quote

Posted at: 2/22/06 10:44 PM

TheKGB NEUTRAL LEVEL 10

Sign-Up: 02/06/06

Posts: 199

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


All times are Eastern Standard Time (GMT -5) | Current Time: 12:36 PM

<< Back

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!