Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

onkeypress is sometime missed when display.onchange is used

I have an App that uses both document.onkeypress = function(e) and display.onchange = function(). Sometimes display.onchange is triggered multiple times in quick succession and document.onkeypress is not triggered in response to some key presses.  Is there a way ensure that all key presses are recognised? 

 

Here is an example that doesn't recognise any keys after the first one and the screen is turned off:

 

import clock from "clock";
import document from "document";
import { preferences } from "user-settings";
import * as util from "../common/utils";
import { display } from "display";

// Update the clock every minute
clock.granularity = "minutes";

// Get a handle on the <text> element
const myLabel = document.getElementById("myLabel");

// Update the <text> element every tick with the current time
clock.ontick = (evt) => {
let today = evt.date;
let hours = today.getHours();
if (preferences.clockDisplay === "12h") {
// 12h format
hours = hours % 12 || 12;
} else {
// 24h format
hours = util.zeroPad(hours);
}
let mins = util.zeroPad(today.getMinutes());
myLabel.text = `${hours}:${mins}`;
}

//detect Screen changing state and turn off again
display.onchange = function() {
console.log("Screen state change detected");
}

// Toggle Blank screen if two keys are pressed
document.onkeypress = function(e) {
console.log("Key pressed: " + e.key);
display.on = false;
}

 

 

Thanks,

Brett. 

Best Answer
0 Votes
2 REPLIES 2

onchange should fire multiple times, once when the screen comes on, once when it goes off.

display.onchange = function() {
  if (display.on) {
    console.log("Screen on");
  } else {
    console.log("Screen off");
  }
}

With regard to the keypress, do you mean it's not triggering each time you touch the button? Is it affected by the screen on/off?

 

Best Answer
0 Votes

Hi Jon, 

 

I have done a bit more research and clarified the problem in a later post https://community.fitbit.com/t5/SDK-Development/Unable-to-reliably-detect-keypress-when-display-is-o...

 

Whats happening is that button presses are reliably detected when the screen is on but some are missed when the screen is off. The code in the post above highlights the problem. This makes it difficult to reliably detect when the user presses certain combinations of keys if the screen is off. 

 

Thanks,
Brett. 

Best Answer
0 Votes