Page 1 of 1

Work with keys.

PostPosted: Mon Feb 24, 2014 12:52 am
by Kilinich
There are few new functions in algodoo 2.1 to work with keys (much easier than before).

First is keys.isDown(keyCode) which is replace old lasers way.
Usage is simple - if key "up" pressed keys.isDown("up") return true. In any script. And it's pretty fast even in every-tick scripts.

Second is new event "onKey" which is different. This is a procedure with object as argument:
onKey = (e) => { your code } It calls every time you press or release key (then you hold key it repeats just like in text editor).

e has few properties:
e.pressed (true/false) - true on key press, false on release
e.keycode (string) - name of the key ex: "backspace"
e.keychar (string) - char of pressed key
e.handled - you could set it "true" to disable that key in interface
e.this - object with onKey code itself

The problem is in design mode some keys like "a" or "s" handled by GUI and you need to switch to player mode.
I've find pretty good solution - set App.GUI.playMode = {sim.running} in any onSpawn script.

That demo could help you to see how it works:
Rating: rated 5.6
Filesize: 74.67 kB
Comments: 12
Ratings: 2
download

Re: Work with keys.

PostPosted: Mon Feb 24, 2014 2:46 am
by Xray
I often use app.gui.playMode = sim.running in update. No need to put brackets around sim.running. I use that in scenes that require the user to click on objects. With most tools hidden, they cannot accidentally ( or on purpose ) damage the scene while it is running.

Good turorial! :thumbup:

Re: Work with keys.

PostPosted: Mon Feb 24, 2014 8:05 am
by Kilinich
Also, you could use keys.isDown to work with mouse.
For example - put that in any object's code:
Code: Select all
update = (e)=>{e.this.color = keys.isDown("mouse_left") ? [1, 1, 1, 1] : [1, 1, 1, 0]}

Re: Work with keys.

PostPosted: Mon Feb 24, 2014 9:40 pm
by electronicboy
It seems that any key which is handled with the GUI, can be picked up after a delay. Not sure if this is intended, but I think best practice would be to send it soon as, but what can you do.

Also, I think that Kilinich's play-mode code is better, I'm a FOSS kind of guy, I love open source software, and if I was a developer, I'd spend more time playing with the code. The code that Kilinich posted allows you to just click the play-mode button in order to disable it once and for all, which allows people to play around, which is one of the many ways people progress in games like these. They figure how other peoples stuff works, and builds upon it.

Nice demo, anyways. :)

Re: Work with keys.

PostPosted: Sat Apr 05, 2014 11:41 pm
by DrAgon
Yeah! thanks to you, Kilinich! :clap:
very good tutorial :thumbup:

Re: Work with keys.

PostPosted: Mon Aug 24, 2015 12:44 pm
by JakubKubo
I want make typewritter can i use this functions?

This properties:
e.pressed - button hold?
e.keycode - name of button pressed
e.keychar - char of button pressed
e.handled - do not understand
e.this - do not understand too

Work only if they are on "onKey:" collum?
Collum "onKey" is activated when i press,or hold, or release?
I try to make typewritter but it write every letter two times or more can you help me :?:

Re: Work with keys.

PostPosted: Tue Aug 25, 2015 3:28 pm
by FRA32
These lines only work in the onKey collum as the event data(e) only contains these values if it was created by the onKey listener. If you want to make a type writer add a limitation on the typign speed, else you write every frame. Do this by adding a _typewait variable to your writer, and write in your poststep box

"_typewait > 0 ? {_typewait = _typewait - 1} : {}"

Then you can write in the onKey box "_typewait == 0 ? {e.this.text = e.this.text + e.keychar; _typewait = 5} : {}

remember to define _typewait first using the small black box on the top left of the script menu by typing "_typewait = 10";

Also, e.this is the reference of the object that has the script, every script owns that as a data ressource of the involved objects. What e.handled does is that if you press key it wont trigger the coresponding object in your interface in the bottom left of the algodoo screen(where the shapes and fixtures and stuff can be selected).

Re: Work with keys.

PostPosted: Wed Aug 26, 2015 10:48 am
by JakubKubo
No, i havenĀ“t problem with typing speed, i have another problem:
When i press button (example: "A"), nothing become, but if I press it again ("A") it type two times ("AA") :crazy:

Script I used:
onKey (e)=> {e.this.text = e.this.text + e.keychar}

Re: Work with keys.

PostPosted: Wed Aug 26, 2015 10:55 am
by JakubKubo
So e.handled blocked hot keys? (C = circle. X = box, etc?) :?:

Re: Work with keys.

PostPosted: Thu Aug 27, 2015 2:28 pm
by Kilinich
JakubKubo wrote:So e.handled blocked hot keys? (C = circle. X = box, etc?) :?:

Some of them, like SPACE or F11 etc. try it.

Re: Work with keys.

PostPosted: Thu Oct 01, 2015 8:06 pm
by The average Joe
Hi, what is the correct way to catch e.keycode as value to be checked when a key is pressed?
Looks like this
Code: Select all
onKey (e)=> {(e.keycode == "down") ? motorSpeed(-=)(0.2) : {}}

or
Code: Select all
onKey (e)=> {(e.keycode == "down" && motorSpeed > 0) ? motorSpeed(-=)(0.2) : {}}

or
Code: Select all
onKey (e)=> {(motorSpeed > 0) ? motorSpeed(-=)(0.2) : {}}

don't work while
Code: Select all
onKey (e)=> {motorSpeed(-=)(0.2)}

works

:problem:

Re: Work with keys.

PostPosted: Fri Oct 02, 2015 6:28 am
by Xray
@The average Joe -- I can't get any of your scripts shown above to work. I believe it's due to the invalid math operation "(-=)" (a minus sign followed by an equal sign). When I changed the script in your first example to the following, it worked just fine!
e.keycode == "down" ? {motorSpeed = motorSpeed - 0.2} : {}

All of your other code examples which have the "(-=)" do not work either. I don't understand why they work for you, as you said they do! :crazy:

Re: Work with keys.

PostPosted: Fri Oct 02, 2015 11:12 am
by The average Joe
Current version of Algodoo automatically wraps signs and values in brackets for me. Tried and with classic 'if' statement instead of 'ternary' - wrapped again and another brackets wrap the whole 'if' condition ... I don't know only last one works and generally every single value assigned to variable like that - shorthand operator like in C, in this case equal to itself minus some value :wtf:

Re: Work with keys.

PostPosted: Fri Oct 02, 2015 5:20 pm
by Xray
Typically, when Thyme wraps any part of your code in parentheses it means that it found something that it does not understand, and it needs to be fixed before it can work. Maybe "-=" works in C, but it definitely does not work in Thyme.

Re: Work with keys.

PostPosted: Fri Oct 02, 2015 10:07 pm
by The average Joe
Well, that "fix things in round brackets" behaviour hit me hard, can't find the words to describe it :crazy:

Any other pitfalls like this one?

Re: Work with keys.

PostPosted: Sat Oct 03, 2015 1:28 am
by Xray
HA HA HA HA! There's a lot of things like this in Algodoo and Thyme that words can't describe! You happened to find just one of them! :lol: