07-09-2019 13:42
07-09-2019 13:42
I have been working on creating an app for the Fitbit Ionic that can toggle a number with the buttons on the watch (up adds one, down subtracts one). Unfortunately, the code I've put together is not quite working. I feel like I'm missing something simple.
Here is what I have so far in the Javascript:
import document from "document"; document.onkeypress = function(e) { console.log("Key pressed: " + e.key); } const test2 = document.getElementById("test2"); let test0 = 0 let upPress = "Key pressed: up" let downPress = "Key pressed: down" upPress = (evt) => { var num = Number($(test0).val()) + 1; } downPress = (evt) => { var num = Number($(test0).val()) - 1; } test2.text = `${test0}`;
And the Index.gui
<svg class="background"> <text id="test2"></text> </svg>
Would anyone happen to know what I've done wrong here?
Answered! Go to the Best Answer.
07-10-2019 13:25
07-10-2019 13:25
I got it working!
import document from "document"; let test2 = document.getElementById("test2"); let test0 = 0; test2.textContent = test0; function buttonPress(e){ console.log("Key pressed: "+e.key); if(e.key==="up"){ test0++; } if(e.key==="down"){ test0--; } test2.textContent = test0; } document.onkeydown = buttonPress;
07-09-2019 13:57
07-09-2019 13:57
Some random thoughts:
There are also much more concise ways of incrementing and decrementing numeric variables!
Hopefully this will help you to sew it together.
07-09-2019 14:19
07-09-2019 14:19
Would this be a better way to use the onkeypress handler?
document.onkeypress = function(e) { console.log("Key pressed: " + e.key); var num = Number($(test0).val()) + 1; }
Sorry, I'm very new to all this.
07-09-2019 23:54
07-09-2019 23:54
Yes, that's definitely a step in the right direction.
A kind person would probably tell you what the answer is.
I am not a kind person.
Forgetting about programming, can you describe in English what you would like to happen when a key is pressed? Feel free to describe what should happen to variables such as test0 and test2 (which could benefit from clearer names, but that's another story).
07-10-2019 11:09
07-10-2019 11:09
Pretty much, I'm trying to put a number on the screen that starts at 0 and goes up or down based on the up and down buttons. Up for +1 and down for -1.
07-10-2019 11:51
07-10-2019 11:51
Test0 is the number that starts at 0 and changes on button press, Test2 was what I was using to bring it into the SVG.
07-10-2019 13:25
07-10-2019 13:25
I got it working!
import document from "document"; let test2 = document.getElementById("test2"); let test0 = 0; test2.textContent = test0; function buttonPress(e){ console.log("Key pressed: "+e.key); if(e.key==="up"){ test0++; } if(e.key==="down"){ test0--; } test2.textContent = test0; } document.onkeydown = buttonPress;
07-10-2019 14:13
07-10-2019 14:13
Good stuff!
07-10-2019 14:38
07-10-2019 14:38
Any thoughts on how to retain the value even when the app exits out? Or is that an unsupported feature.
07-10-2019 14:46
07-10-2019 14:46
Have a look at onunload and the File System API.