I am building a chat application and I am trying to make sure that any time a user presses a key, the key they press ends up getting entered into a specific element (an editable HTML document ui.rte1Content in an iframe ui.rteIFrame, to be exact). This should happen no matter which element on the page currently has focus. I only care about supporting Firefox and Chrome.
So far, I've registered a handler on the window.onkeydown event:
function captureKeyDown(e) {
console.log("Capturing: " + e.keyCode);
// We ignore certain keys such as page up, as well as keys pressed with
// alt or ctrl, except for a few select combos like ctrl+b
if(!ignoreKeys.contains(e.keyCode) && !e.altKey &&
(!e.ctrlKey || (e.ctrlKey && validCtrlKeys.contains(e.keyCode)))) {
ui.rte1Content.focus(); // Chrome prefers this.
if(!rteHasFocus) {
ui.rteIFrame.focus(); // Firefox prefers this
}
//alert("A commented out alert");
}
}
This works perfectly in Firefox: rteIFrame gets focus, the key that's been pressed shows up, and we carry on. However, in Chrome (23.0.1271.95), while rte1Content correctly gets focus in time for the NEXT letter to show up, the initial letter that first triggered the event disappears into the ether.
For what it's worth, if I uncomment out the alert statement, the letter shows up just fine in Chrome. I have no idea why this is.
Does anyone have any suggestions about how I can get this to work?