Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
keycode events

veganpete
Registered User
Quote
2023-05-25 10:59:41

How would I create a simple scripted action to simulate keycode events please?

eg:

Behaviour= "every few seconds (100ms)";
Action= "register keycode(72)"

Thank you.


just_in_case
Moderator
Quote
2023-05-25 12:56:03

It depends on how you want to acess them, i beleive there are many extensions in the community that allows you to have the action on specific key press.

or if you want to do it directly using scripting, you can then simply register the KeyDown or KeyUp event provided by JS API of CopperCube.

here is the Code from Documentation:-


ccbRegisterKeyDownEvent("keyPressedDown");

function keyPressedDown(keyCode)
{
if(keyCode == 70){ ccbSetCopperCubeVariable("F_Down","true");]
}


what the above code will do is if you press F key it will set the variable "F_Down" to true, the variable remain true even after the keyleft up, if you want to set it false on key left up, then simply use the KeyUp event to set variable to false.

I know it will be hard for you to learn Javascript but I am planning a video series specifically for javascript beginners in CopperCube.


VP
Guest
Quote
2023-05-25 18:10:58

Thank you, that's perfect!

ccbSetCopperCubeVariable("F_Down","true");

That was the part I was looking for - I didn't realise you could use "F_Down","true", that's great. I'll give it a go.

Is there a list of all the properties that can be used in the API anywhere - I only ever find a few examples in the help files but I've never found the full list of internal variables and properties anywhere.

Yes please, some beginner tutorials for coding would be really appreciated. Even if I only ever learn to convert what I do in non-code to be able to ru it as an "execute special script" (instead of a behaviour with tens of actions attached) would be 100 times easier for me to manage/edit.


VP
Guest
Quote
2023-05-25 18:36:11

Can't get it to work.

I tired:

"every few seconds, execute javascript: "cbSetCopperCubeVariable("F_Down","true");"

Then I set an action:, When "F" key is pressed down, toggle visibility of node.

I expected the node to toggle visibility every few seconds - but nothing happens.

I'll wait for the tutorial. Thanks again.


VP
Guest
Quote
2023-05-25 19:14:31

Oh, hang on - I just realised you're only explaining how to set a variable when a key is pressed, rather than explaining how to pass a keycode event using coding. Is that even possible using the Coppercube API?


just_in_case
Moderator
Quote
2023-05-26 11:49:43

actually, I did posted the whole code for you and yup I was explaining the usage to you.

So in order to use the above code, simply attach a behavior "before first drawing do something" and to that behavior attach an action "Execute javascript" and inside that action copy paste the above code that I have sent in the earlier post.

and now in your game when you press "F" key it will set the variable "F_down" to true, you can change the variable name to anything that you want.

And if you want to check another key press rather than "F" then simply change the numeric value "70" in the code with ther keycode value for the key that you want to use.

You don't need to use on Key pressed behavior then.

I don't know what you really wanna do with the every few seconds do something.

Do you want that when you press a key, then the object will start blinking, or start toggling its visibility state every few seconds?

Above code that I sent you is same as using the when a key is pressed down do something, so if you don't wanna use the code then you can use that approach.

And I mentioned to apply this on before first drawing do something, for easier usage, you can also use it however you want, like in on proximity or when reaching a specific level, or when a variable has a value and all. you need to register it once only. and then it will work continuously.

The above example is just for KeyDown event, you probably also want to use KeyUp event along with it, if you are using this.

You will get to know this, when I will start creating and pushing tutorial videos for javascript. It's hard to explain the usage in text.


veganpete
Registered User
Quote
2023-05-26 18:02:37

I can't explain what I mean properly - the forum wont let me post, it often says "Looks like you are postings spam, please edit your post and try again."

It doesn't matter. I don't think it's possible to do what I'm asking for.

Thanks anyway.


eoh
Guest
Quote
2023-05-26 18:08:52

i think i get what you need

simulate key press

i dont think its possible in cc i might be wrong


eoh
Guest
Quote
2023-05-26 18:14:04

on second thought it might be possible with javascript


Guest
Guest
Quote
2023-05-26 19:01:37

@VP

Here's an example of doing what you want in pure code (no actions/behaviors) if that might help you. Here's an example file:

https://files.catbox.moe/xvyy2n....

Here's just the code:


var cube = ccbGetSceneNodeFromName("cubeMesh1"); // get cube node
var startTime = Date.now(); // get ms when app starts
var step = 250; // change to control flicker rate
var interval = step; // interval to add step amount to
var key; // var to hold key code
var pressed = false; // boolean for key press or not

// function to set/unset cube visible
function flicker(node) {
var a = ccbGetSceneNodeProperty(node, "Visible"); // get node

// toggle visiblity
if (a) {
ccbSetSceneNodeProperty(node, "Visible", false);
} else {
ccbSetSceneNodeProperty(node, "Visible", true);
}
}

// key down event
function keyDown(keyCode) {
key = keyCode;
pressed = true;
}

// key up event
function keyUp(keyCode) {
key = keyCode;
pressed = false;
}

// "game loop"
function game() {
var now = Date.now(); // ms when loop starts
var timeMs = now - startTime; // current time

if (timeMs >= interval) {
if (key == 70 && pressed == true) {
flicker(cube); // flicker the cube

interval += step; // increment interval
} else {
ccbSetSceneNodeProperty(cube, "Visible", true);
}
}
}

// register key events
ccbRegisterKeyDownEvent("keyDown");
ccbRegisterKeyUpEvent("keyUp");

// registers "game loop"
ccbRegisterOnFrameEvent(game);


You can control the flicker rate by changing the step variable to whatever you want. Works like Every Few Seconds Do Something behavior. If you have any questions I can answer them.


VP
Guest
Quote
2023-05-26 20:42:15

Thank you Guest and just_in_case! I appreciate your time to write all that for me.

That does make sense to me, but it's not exactly what I want - it's not really the flickering I need when a key is pressed. I just used "every few seconds" as an example to explain - but I ended up confusing everyone.

As EOH says, I actually just want to pretend a key has being pressed, rather than having to physically press a key.

Another example is kinda like a "macro" kelogger software; it records and plays back keypresses: without needing to physically touch the keyboard. Essentially, I want coppercube to "playback" a single keypress - or pass a single keycode.

There's probably just a simple "special variable" for this, I just can't find it anywhere in the documentation.

Thanks everyone, please don't waste too much of you time on this, it was just an idea I had for something - but I can work without it.


Guest
Guest
Quote
2023-05-26 22:53:31

No worries. Not certain about why you would want to fake a key press. It must have something to do with making visual scripting easier. For someone who programs like me, it's a bit counterintuitive for me to think why you would want to fake the user's input. If you can happen to explain why then a new behavior and/or action could be written to help you out.


VP
Guest
Quote
2023-05-26 23:02:36

Thanks Guest, I know it sounds odd, but that's correct.

Basically, I want to be able to control reshade shaders from inside coppercube. So I can bind a keypress to reshade to enable a specific video overlay (activated with key-combo "ctrl+u" for example).

Then, in coppercube, if I can trigger the "ctrl+u" keycodes with an "on proximity" event (for example), I can enable/disable the visual-effects in time with the game's events - without the player needing to physically press the corresponding keys.

I think I can probably create a workaround (detecting a single, coloured pixel in the top-left of the screen to trigger an auto-hot-key binding) - but I figured a direct keycode would be a lot easier, if possible.

Thanks again.


Aiming_bullets
Guest
Quote
2023-05-27 02:20:09

I don't know but , maybe you can try using ccbEmulateKey() command, just_in_case said that this will simulate a key press. I am not sure but you can give it a shot.


just_in_case
Moderator
Quote
2023-05-27 05:05:44

@VP, if you want to emulate keypress then you should have asked that directly, I mean be specific to what you ask for.

Yes it it totally possible to simulate or emulate keypress.

just like @aiming_bullets mentioned, you can use the recently intorduced ccbEmulateKey(keyCode, pressedDown) command.

You can use it with either on proximity or every few seconds, in the execute javascript action.

the usage is very simple and is just one line of code.

let say you want to emulate "F" key and you want to emulate the state like when it is press down.

then simply in your execute javascript action, either in every few seconds command or anywhere else put this code.

 ccbEmulateKey(70, true)


you can change the keycode "70" to the keycode that you want to use, for mouse keys keycodes are ( 1 = left mouse button, 2 = right mouse button, 4 = middle mouse button).
you can find the javascript keycode here on this website if you don't know which one is for which key.
https://www.toptal.com/developer...

If you want to simulate key up event, then change the second parameter "true" to "false" and it will emulate if the key is left up.

This will then work as if you pressed that key in your game, and if you game has any behavior like when a key is pressed F, and anything inside that behavior.

it will be going to execute that behavior. Hope that helps. let me know if you need a .ccb example file.


but that might not work with external things like re-shade and all, the other way around is to use system() command to simulate the keypress, that will actually work with third party integrations but can be quite complex and may require additional libraries or tools.

Also, if you ever want to get your queries and answers in realtime then you can always text me on Discord. I remain active there most of the time.


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Int?rnational" (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