Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
how to check key press once

aiming_bullets
Guest
Quote
2021-03-28 04:58:21

hii all, I want to check the keypress using javascript. but I want to check it once only. Even if I hold the key it will be registered as one keypress, not continuous keypresses. I want to increase a variable if a key is pressed but it will add so many value if I hold the key what i want to achieve is if a key is pressed down even if holding down add increase the variable by one only.

Anyone knows how to do this either by using scripting or with the behavior when key is pressed.


aiming_bullets
Guest
Quote
2021-09-08 07:03:52

Still need an answer for this anybody?


sven
Guest
Quote
2021-09-08 09:17:01

Technic is same.
https://www.ambiera.com/forum.php?t=8713


sven
Registered User
Quote
2021-09-08 15:31:07

I misread the question..

in javascript i do it like this..

/*
<behavior jsname="behavior_IF_KEY_PRESSED" description="IF_KEY_PRESSED">
<property name="Keycode" type="int" default="13" />
<property name="Action" type="action"/>
</behavior>
*/

key_p = [250];
for(var i=0; i<250; ++i) key_p.push(0);

key_l = [250];
for(var i=0; i<250; ++i) key_l.push(0);



ccbRegisterKeyDownEvent("keyPressedDown");
function keyPressedDown(keyCode)
{
key_p[keyCode]=1;
//print("pressed key KEYCODE" +keyCode);
}

ccbRegisterKeyUpEvent("keyPressedUp");
function keyPressedUp(keyCode)
{
key_p[keyCode]=0;
key_l[keyCode] =0;
}

behavior_IF_KEY_PRESSED = function()
{
};

behavior_IF_KEY_PRESSED.prototype.onAnimate = function()
{

if (key_p[this.Keycode] == 1 && key_l[this.Keycode] == 0)
{
key_l[this.Keycode] =1;
ccbInvokeAction(this.Action);
}
return true;
}



If you want to use this script then you need keycode..
ENTER key is 13
so you can use it if key pressed 13 then call action.. and it will be called only once per click.


Aiming bullets
Guest
Quote
2021-09-09 07:49:11

Thanks a lot sven, but for my case I don't want to check it with key up event, as I am also doing something when key left up.


Sven
Guest
Quote
2021-09-09 08:41:37

You can use my script to check both..key up or down once..


Aiming_bullets
Guest
Quote
2021-09-09 09:02:47

You are not getting me, as your script uses key left up and key down.

I have one event which throws an object only once when a key is down and when the key is left up the object moves back to the hand.

But right now, with any script, it only throws the object, and even when holding down it makes the object move back instantly.
but I want it to move back only when I really leave the key. I don want it to trigger the action when holding down.

if the key is held down perform nothing.


Sven
Guest
Quote
2021-09-09 09:04:04

I will edit script later and post it..
So you get key up and down action to execute only one time per klick..if thats what you need.


sven
Registered User
Quote
2021-09-09 15:20:15

This script will:
Call action ONLY ONE TIME if key pressed down...
(no matter if key is held down)
AND if relased it calls action ONLY ONE TIME.
(no matter if key left up forever)

If i still misunderstood your question then i hope somebody can answer it for you.

/*
<behavior jsname="behavior_IF_KEY_PRESSED" description="IF_KEY_PRESSED">
<property name="Keycode" type="int" default="13" />
<property name="Actied" type="action"/>
<property name="ActionKeyRelased" type="action"/>
</behavior>
*/

key_p = [250];
for(var i=0; i<250; ++i) key_p.push(0);
key_l = [250];
for(var i=0; i<250; ++i) key_l.push(0);
key_lu = [250];
for(var i=0; i<250; ++i) key_lu.push(2);


ccbRegisterKeyDownEvent("keyPressedDown");
function keyPressedDown(keyCode)
{
key_p[keyCode]=1;
//print("pressed key KEYCODE" +keyCode);
}

ccbRegisterKeyUpEvent("keyPressedUp");
function keyPressedUp(keyCode)
{
key_p[keyCode]=0;
key_l[keyCode] =0;
key_lu[keyCode] =0;
}

behavior_IF_KEY_PRESSED = function()
{
};

behavior_IF_KEY_PRESSED.prototype.onAnimate = function()
{
//IF KEY PRESSED DOWN :EXECUTE ACTION ONLY ONCE..
if (key_p[this.Keycode] == 1 && key_l[this.Keycode] == 0)
{
key_l[this.Keycode] =1;
ccbInvokeAction(this.Actied);
print(this.Keycode +"pressed");
}

//IF KEY RELASED :EXECUTE ACTION ONLY ONCE..
if (key_p[this.Keycode] == 0 && key_lu[this.Keycode] == 0)
{
key_lu[this.Keycode] =1;
ccbInvokeAction(this.ActionKeyRelased);
print(this.Keycode +"relased");
}

return true;
}



aiming_bullets
Guest
Quote
2021-09-09 19:27:52

this is exactly, what I wanted. Thanks, @sven you are a genius. this works perfectly.


miglenser
Registered User
Quote
2021-09-14 15:38:41

Thanks for these tips!


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Internation?l" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact