Work with keys.
16 posts • Page 1 of 1
Work with keys.
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:
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:
Dream of Algodoo as game development engine...
-
Kilinich - [Best bug reporter 2010]
- Posts: 2098
- Joined: Mon Aug 31, 2009 8:27 pm
- Location: South Russia
Re: Work with keys.
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!
Good turorial!
-
Xray - Posts: 500
- Joined: Sun Jun 17, 2012 6:12 am
- Location: USA
Re: Work with keys.
Also, you could use keys.isDown to work with mouse.
For example - put that in any object's code:
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...
-
Kilinich - [Best bug reporter 2010]
- Posts: 2098
- Joined: Mon Aug 31, 2009 8:27 pm
- Location: South Russia
Re: Work with keys.
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.
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.
Yeah! thanks to you, Kilinich!
very good tutorial
very good tutorial
-
DrAgon - Posts: 31
- Joined: Tue Feb 12, 2013 4:03 pm
Re: Work with keys.
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
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.
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).
"_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.
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")
Script I used:
onKey (e)=> {e.this.text = e.this.text + e.keychar}
When i press button (example: "A"), nothing become, but if I press it again ("A") it type two times ("AA")
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.
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.
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...
-
Kilinich - [Best bug reporter 2010]
- Posts: 2098
- Joined: Mon Aug 31, 2009 8:27 pm
- Location: South Russia
Re: Work with keys.
Hi, what is the correct way to catch e.keycode as value to be checked when a key is pressed?
Looks like this
or
or
don't work while
works
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
-
The average Joe - Posts: 14
- Joined: Thu Sep 24, 2015 6:35 pm
Re: Work with keys.
@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!
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!
-
Xray - Posts: 500
- Joined: Sun Jun 17, 2012 6:12 am
- Location: USA
Re: Work with keys.
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
-
The average Joe - Posts: 14
- Joined: Thu Sep 24, 2015 6:35 pm
Re: Work with keys.
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.
-
Xray - Posts: 500
- Joined: Sun Jun 17, 2012 6:12 am
- Location: USA
Re: Work with keys.
Well, that "fix things in round brackets" behaviour hit me hard, can't find the words to describe it
Any other pitfalls like this one?
Any other pitfalls like this one?
-
The average Joe - Posts: 14
- Joined: Thu Sep 24, 2015 6:35 pm
Re: Work with keys.
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!
-
Xray - Posts: 500
- Joined: Sun Jun 17, 2012 6:12 am
- Location: USA
16 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 4 guests