Work with keys.

About advanced scenes, and the Thyme scripting language used in Algodoo.

Work with keys.

Postby Kilinich » Mon Feb 24, 2014 12:52 am

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
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Work with keys.

Postby Xray » Mon Feb 24, 2014 2:46 am

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:
User avatar
Xray
 
Posts: 499
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Work with keys.

Postby Kilinich » Mon Feb 24, 2014 8:05 am

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]}
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Work with keys.

Postby electronicboy » Mon Feb 24, 2014 9:40 pm

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. :)
When asking for help, READ THE STICKIES!
electronicboy
 
Posts: 1694
Joined: Mon Aug 31, 2009 6:18 pm

Re: Work with keys.

Postby DrAgon » Sat Apr 05, 2014 11:41 pm

Yeah! thanks to you, Kilinich! :clap:
very good tutorial :thumbup:
User avatar
DrAgon
 
Posts: 31
Joined: Tue Feb 12, 2013 4:03 pm

Re: Work with keys.

Postby JakubKubo » Mon Aug 24, 2015 12:44 pm

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 :?:
JakubKubo
 
Posts: 18
Joined: Thu Mar 12, 2015 6:27 pm

Re: Work with keys.

Postby FRA32 » Tue Aug 25, 2015 3:28 pm

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).
FRA32
 
Posts: 227
Joined: Wed Dec 03, 2014 9:51 pm

Re: Work with keys.

Postby JakubKubo » Wed Aug 26, 2015 10:48 am

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}
JakubKubo
 
Posts: 18
Joined: Thu Mar 12, 2015 6:27 pm

Re: Work with keys.

Postby JakubKubo » Wed Aug 26, 2015 10:55 am

So e.handled blocked hot keys? (C = circle. X = box, etc?) :?:
JakubKubo
 
Posts: 18
Joined: Thu Mar 12, 2015 6:27 pm

Re: Work with keys.

Postby Kilinich » Thu Aug 27, 2015 2:28 pm

JakubKubo wrote:So e.handled blocked hot keys? (C = circle. X = box, etc?) :?:

Some of them, like SPACE or F11 etc. try it.
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Work with keys.

Postby The average Joe » Thu Oct 01, 2015 8:06 pm

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:
User avatar
The average Joe
 
Posts: 14
Joined: Thu Sep 24, 2015 6:35 pm

Re: Work with keys.

Postby Xray » Fri Oct 02, 2015 6:28 am

@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:
User avatar
Xray
 
Posts: 499
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Work with keys.

Postby The average Joe » Fri Oct 02, 2015 11:12 am

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:
User avatar
The average Joe
 
Posts: 14
Joined: Thu Sep 24, 2015 6:35 pm

Re: Work with keys.

Postby Xray » Fri Oct 02, 2015 5:20 pm

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.
User avatar
Xray
 
Posts: 499
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Work with keys.

Postby The average Joe » Fri Oct 02, 2015 10:07 pm

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?
User avatar
The average Joe
 
Posts: 14
Joined: Thu Sep 24, 2015 6:35 pm

Re: Work with keys.

Postby Xray » Sat Oct 03, 2015 1:28 am

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:
User avatar
Xray
 
Posts: 499
Joined: Sun Jun 17, 2012 6:12 am
Location: USA


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 1 guest

cron