Work with keys.
Posted: 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:
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: