View Single Post
  #1  
Old 06-30-2002, 05:17 PM
GrowlZ1010 GrowlZ1010 is offline
defunct
Join Date: May 2002
Posts: 187
GrowlZ1010 is on a distinguished road
Post GrowlZ's Tutorial #1: The Keyboard

Yes, Graal now has full keyboard detection. Neat, huh? For those of you who want to use this handy new feature, but don't know quite how yet, here's your opportunity to learn.

To understand this properly, you need to understand that the keyboard has two basic types of key: character keys and operation keys. Character keys are buttons that produce a symbol or letter when pressed, for example the "S" or "5" keys. Operation keys are keys that cause the computer to do a special function, for example: "Caps Lock", "Tab" (which sometimes prints a space, but which Graal views as an operation key), "Return" and all the cursor keys (Up, Down, Left and Right). Graal's "keypressed" keyboard detection can only handle character keys. With that cleared up, let's begin.

There's one new event, keypressed. This is for detecting when a character key is pressed. So:

NPC Code:

if (created){
setimg book15.png;
hide;
}
if (keypressed){
show;
}


... will cause a NPC to show itself when a character key is pressed. Simple enough.

But how, you may ask, do you find out which key has actually been pressed? Here's when we find out about the two new messagecodes, #p(0) and #p(1).

NOTE: Yes, I know #p messagecodes were formerly used for triggeraction parameters. In fact, they still are. #p(2) will still give you a triggeraction param, rather than a key name. But #p(0) and #p(1), for all practical purposes, are now keyboard messagecodes. I haven't been able to test what happens if a NPC that has an action triggered on it checks #p(0) (will it return a triggeraction parameter, or a keycode?) because triggeraction always sucks and refuses to work for me. If anyone can find out the result, I'd appreciate it. And now, back to the tutorial.

#p(0) returns the KEYCODE of the character. A keycode is a number assigned to a character to distinguish it from the others: for example, the character "p" has the keycode 80, whereas the character "s" has the keycode of 83.

IMPORTANT! Keycodes differ between case. A small letter "p" won't return the same keycode as the capital "P".

#p(1) returns the actual NAME of the character. For example, the name of the character "p" is, surprisingly enough, "p". "q" has the name "q", "f" is "f", and "@" comes out as "@". The character's name is basically just the character itself.

Small thing to consider here: Graal doesn't seem to be able to recognize the name of the "," character. So try and leave that out of your #p(1) scripts.

So to test if the key "p" has been pressed using #p(0), it would be:

NPC Code:

if (keypressed && strequals(#p(0),80)){
setplayerprop #c,You pressed "p"!;
}


To do the same with #p(1):

NPC Code:

if (keypressed && strequals(#p(1),p)){
setplayerprop #c,You pressed "p"!;
}


Keyboard detection has also added two new functions, keycode(character) and keydown2(keycode,ignorecase<false/true>).

Keycode(character) lets you check the keycode of any character you want. You can use this to bypass #p(0) altogether, using keycode(#p(1)), but why would you want to do that? :P

Keydown2(keycode,ignorecase<false/true>) is a little more complicated. This function actually uses the other function, keycode(character), to find out the button you want to press. Now, as I've told you earlier, case matters when we're talkin' about keycodes... or at least, it does without the help of keydown2. The ignorecase part has two states: "true" or "false". If ignorecase is "true", then no matter whether the key is a or A, p or P, f or F, it will always be treated as the same letter. Here's an example of how you would use keydown2.

NPC Code:

if (created){
timeout = 0.05;
}
if (timeout){
if (keydown2(keycode(A),true)){
setplayerprop #c,You pressed "a" or "A"!;
}
timeout = 0.05;
}


In case you're wondering, it's in a timeout loop because functions have to be checked by something else. They can't do themselves when something happens.

This ends my keyboard tutorial: anything you need clarified, comments, suggestions, complaints, etc., just post 'em here. Or say what you thought with the poll. I'm listening!

The attatchment below is a Graalscript to help you check keycodes for characters. It requires Graal v.2.14 or later to work properly.
Attached Files
File Type: graal keypress.graal (1.3 KB, 223 views)
Reply With Quote