PDA

View Full Version : keydown2


cbk1994
02-14-2008, 03:22 AM
I'm using keydown2 for several things on Utopia. One for guns system, one for melee.

Gun system checks 's', melee checks 'n'.

After a while (for some reason it seems to do this more on Macintosh computer, or when idling), the checks just stop working. Reconnecting does not help. You have to close Graal and open it back up.

This is what we have for one of them:


if ( keydown2( getkeycode( "s" ), false ) || keydown2( getkeycode( "n" ), false ) )


Funny thing is that when "S" fails, "N" fails.

Like I said, it might not happen more on Macs, but it seems to me like it. I know it DOES happen on Windows though.

What am I doing wrong? Is it a glitch with something else? Thanks!

xAndrewx
02-14-2008, 09:54 AM
You'd need that in a timeout. Is it in one?

You're also checking that "S" and "N" are not pressed in that example.

Crow
02-14-2008, 02:54 PM
You're also checking that "S" and "N" are not pressed in that example.

Not really. Extract from the wiki:

keydown2( keycode,ignorecase) checks if a key has been pressed (ignorecase must be
false to check for shift,ctrl,alt)

napo_p2p
02-14-2008, 08:09 PM
I would make sure that is in a timeout (I think you have it in one already, though). Also, I would set the second parameter to 'true' because you want it to ignore the case. It fails when players press capital S and N.

cbk1994
02-14-2008, 11:45 PM
Thanks everyone, but I've already tried all of these.

It's completely unrelated to capitalization -- we have tried many things. All the keydown2 events on the server (for the player) just die, even on different scripts.

Like I said, unrelated to caps lock, etc at all.

YES, it is in a timeout, Andrew! I'm not that dumb.

I'm sure it's some little thing I've missed -- I'll recheck tonight ... I just can't understand why they all would stop working at the same time IN DIFFERENT SCRIPTS.

Chompy
02-14-2008, 11:50 PM
YES, it is in a timeout

Why are you using a timeout though?


function onKeyPressed(code, key) {
switch (lowercase(key)) {
case "s":
player.chat = "S";
break;
case "n":
player.chat = "N";
break;
}
}

cbk1994
02-14-2008, 11:56 PM
Why are you using a timeout though?


function onKeyPressed(code, key) {
switch (lowercase(key)) {
case "s":
player.chat = "S";
break;
case "n":
player.chat = "N";
break;
}
}


Because you can't use that to detect when a key is held down.

Chompy
02-15-2008, 12:01 AM
Because you can't use that to detect when a key is held down.

well, you kind of can:


//#CLIENTSIDE
function onCreated()
setTimer(0.1);
function onTimeout() {
// or whatever to detect if it's not held down
if (!keydown(5)) this.canhit = true;
setTimer(0.1);
}
function onKeyPressed(code, key) {
switch (lowercase(key)) {
case "s":
if (this.canhit) {
player.chat = "s";
this.canhit = false;
}
break;
}
}

cbk1994
02-15-2008, 12:30 AM
well, you kind of can:


//#CLIENTSIDE
function onCreated()
setTimer(0.1);
function onTimeout() {
// or whatever to detect if it's not held down
if (!keydown(5)) this.canhit = true;
setTimer(0.1);
}
function onKeyPressed(code, key) {
switch (lowercase(key)) {
case "s":
if (this.canhit) {
player.chat = "s";
this.canhit = false;
}
break;
}
}


That would never work. x.x
If we're having a timeout anyway, why not just use keydown2?
Also should use key.lower() instead of lowercase( key )

Chompy
02-15-2008, 12:44 AM
That would never work. x.x*
If we're having a timeout anyway, why not just use keydown2?**
Also should*** use key.lower() instead of lowercase( key )

*) It does, I use it at Symp

**) Well, onKeyPressed() will always be called, was just a thought tho..

***) explain :)

napo_p2p
02-15-2008, 01:51 AM
I have the same keydown2 setup on AEON, and it works fine... I just have the second param set to 'true'.

Also, as a side note:
When you are using the second param for onKeyPressed, you do not need to put capitalization into account :P.

Inverness
02-15-2008, 03:10 AM
TGraalVar.lower(), converts the string variable to lowercase. upper() does the opposite.

cbk1994
02-15-2008, 03:57 AM
*) It does, I use it at Symp

**) Well, onKeyPressed() will always be called, was just a thought tho..

***) explain :)

Except that it can't detect if you WANT holding down, like on automatic.

onKeyPressed() is called at first, then repeatedly after like a second.

lowercase( ) is GS1 existing for compatibility. str.lower() is gs2.

Inverness
02-15-2008, 10:54 PM
Because you can't use that to detect when a key is held down.

function GraalControl.onKeyDown(keycode, keystring, scancode) {
}
function GraalControl.onKeyUp(keycode, keystring, scancode) {
}

Would that not do it?

cbk1994
02-15-2008, 11:17 PM
When I originally tried it it turned out to be very buggy. I'll try it out again, thanks.

Inverness
02-15-2008, 11:34 PM
When I originally tried it it turned out to be very buggy. I'll try it out again, thanks.That method would only work when you have the GraalControl selected; meaning that you're able to move your player and such.

Another thing, I noticed that onKeyPressed() does not detect Ctrl+Key while GuiControl.onKeyDown() does.

pokeSMOT
02-16-2008, 12:19 AM
//#CLIENTSIDE
function onCreated()
Why onCreated() and not just Created()? What does "on" do?
setTimer(0.1);
What is the timer used for? and if it's declared within a function, why not just call the function for later use?
function onTimeout() {
What is the difference between "setTimer" and "onTimeout"?
// or whatever to detect if it's not held down
if (!keydown(5)) this.canhit = true;
Does this mean if whatever key '5' represents, pressing the opposite will activate "this.canhit = true;"? And what does that part mean?
setTimer(0.1);
Couldn't the whole function be called?
}
function onKeyPressed(code, key) {
Are "code" and "key" some sort of preset parameters or something? What do those mean? Cause I don't see them defined anywhere..
switch (lowercase(key)) {
Is capslock relevant here, or is this for something else?
case "s":
I remember case's from C++, but the next part is what troubles me..
if (this.canhit) {
Is this still asking if while it is true?
player.chat = "s";
So if "this.canhit" was set to true, then it makes the player say "s"? But then how is this.canhit even determinded? By pressing any key other than '5'?
this.canhit = false;
And I'm guessing that absolutely nothing happens if this.canhit is false, cause there's nothing after it except closing brackets
}
break;
And this simply halts the script? no?
}
}

Sorry to whomever takes the time out to read through and answer this post, but I'm really trying to learn this stuff. I find gs2 quite fascinating.

napo_p2p
02-16-2008, 03:33 AM
I'll try my best to explain it in a way you can understand. Let me know if you still have a question.

//#CLIENTSIDE
function onCreated()
Why onCreated() and not just Created()? What does "on" do?
setTimer(0.1);
'created' is the name of an event. In GS2, when an event happens, the function onEVENTNAMEHERE() is called (if the function is defined).

What is the timer used for? and if it's declared within a function, why not just call the function for later use?
function onTimeout() {
What is the difference between "setTimer" and "onTimeout"?
// or whatever to detect if it's not held down
if (!keydown(5)) this.canhit = true;
The difference between setTimer() and onTimeOut() is that using onTimeOut() starts the timeout immediately, while setTimer() lets you define how long you want the script to wait before calling onTimeOut(). In Chompy's example he probably should have used onTimeOut() instead of the first setTimer().

Does this mean if whatever key '5' represents, pressing the opposite will activate "this.canhit = true;"?
You're very warm. As long key '5' (which is the S key) is not pressed, then this.canhit will be set to true. So, even if nothing is being pushed, this.canhit will be set to true.


And what does that part mean?
setTimer(0.1);
Couldn't the whole function be called?
In this case, you want a little delay between the next time onTimeOut() is called. You could call onTimeOut() directly, but this will result in it being called too many times too quickly (LAG!).

function onKeyPressed(code, key) {
Are "code" and "key" some sort of preset parameters or something? What do those mean? Cause I don't see them defined anywhere..
keypressed is an event that passes two parameters when it is called. In GS2, you can give the parameters temporary variables. In this case, Chompy chose to name the variables 'code' and key'. If he didn't name the variables, he would have to access them using params[0] and params[1].

switch (lowercase(key)) {
Is capslock relevant here, or is this for something else?
You actually do not need to account for caps in this example.

And this simply halts the script? no?
In switch statements, you need the break at the end of each case to prevent the script from going to the next case. In this example, it isn't really needed as there are no other cases. It's alright to include the break though, so in case you do add to the switch statement later, you won't have to worry about forgetting to add the break.

Inverness
02-16-2008, 04:55 AM
onKeyPressed() is actually passing three parameters: keymodifier, keystring, and scancode.

Chompy
02-16-2008, 01:06 PM
Err, I just remembered why I lower cased the key.. I made the battle system on symp.. err 5-6 months ago? Anyways, I thought that the event would return "s" and "S". So using

switch(key) {
case "s":
break;
case "S":
break;
}

Would be unnecesary so I just lowercased the key so I would only need the "s" part..

But back then I didn't check if it returned "s" and "S", I just added it because I thought the event would return "s" and "S"..

Crow
02-16-2008, 01:47 PM
Err, I just remembered why I lower cased the key.. I made the battle system on symp.. err 5-6 months ago? Anyways, I thought that the event would return "s" and "S". So using

switch(key) {
case "s":
break;
case "S":
break;
}

Would be unnecesary so I just lowercased the key so I would only need the "s" part..

But back then I didn't check if it returned "s" and "S", I just added it because I thought the event would return "s" and "S"..

If it returns "s" and "S", doing the following is probably easier:

switch (key) {
case "s":
case "S":
//stuff
break;
}

zokemon
02-16-2008, 04:26 PM
If it returns "s" and "S", doing the following is probably easier:

switch (key) {
case "s":
case "S":
//stuff
break;
}

Hehe.

coreys
02-16-2008, 05:23 PM
I usually don't use switch/case statements for my onKeyPressed() events, since I usually just have one key press per script that matters, so I just generally go with if (lowercase(key) == whatever)...

Crow
02-16-2008, 09:49 PM
Hehe.

Didnt even read that. Nice one :D

I basically always use switch, because i think it reads better.



Edit: Reading through that thread a little more, it looks like I did read it. But I cant remember :/