Fixed the ability to interact with a SerialPort control on iOS devices

This commit is contained in:
Jeff Parsons 2015-12-03 13:19:30 -08:00
commit 7e523d567e
2 changed files with 26 additions and 26 deletions

View file

@ -345,6 +345,16 @@ Keyboard.SIMCODE = {
ALT: Keyboard.KEYCODE.ALT + Keyboard.KEYCODE.ONDOWN,
CAPS_LOCK: Keyboard.KEYCODE.CAPS_LOCK + Keyboard.KEYCODE.ONDOWN,
ESC: Keyboard.KEYCODE.ESC + Keyboard.KEYCODE.ONDOWN,
/*
* It seems that a recent change to Safari on iOS (first noticed in iOS 9.1) treats SPACE
* differently now, at least with regard to <textarea> controls, and possibly only readonly
* or hidden controls, like the hidden <textarea> we overlay on the Video <canvas> element.
*
* Whatever the exact criteria are, Safari on iOS now performs SPACE's default behavior
* after the onkeydown event but before the onkeypress event. So we must now process SPACE
* as an ONDOWN key, so that we can call preventDefault() and properly simulate the key at
* the time the key goes down.
*/
SPACE: Keyboard.KEYCODE.SPACE + Keyboard.KEYCODE.ONDOWN,
F1: Keyboard.KEYCODE.F1 + Keyboard.KEYCODE.ONDOWN,
F2: Keyboard.KEYCODE.F2 + Keyboard.KEYCODE.ONDOWN,

View file

@ -343,16 +343,6 @@ SerialPort.prototype.setBinding = function(sHTMLType, sBinding, control)
switch (sBinding) {
case SerialPort.sIOBuffer:
/*
* TODO: Figure out how to make this control activate the soft keyboard on iOS; since
* this control has the "readonly" attribute by default, iOS refuses to activate the
* keyboard, and adding the "contenteditable" attribute doesn't override that behavior.
*
* Removing the "readonly" attribute works, but then every key press results in double
* characters; presumably that could be overcome by processing all keys in the onKeyDown()
* handler instead, but that would require keyCode mapping tables, because down events
* are not the same as press events.
*/
this.bindings[sBinding] = this.controlIOBuffer = control;
/*
@ -380,33 +370,33 @@ SerialPort.prototype.setBinding = function(sHTMLType, sBinding, control)
}
return true;
};
control.onkeypress = function onKeyPress(event) {
/*
* Browser-independent keyCode extraction; refer to keyPress() and the other key event
* Browser-independent keyCode extraction; refer to onKeyPress() and the other key event
* handlers in keyboard.js.
*/
event = event || window.event;
var keyCode = event.which || event.keyCode;
serial.sendRBR([keyCode]);
/*
* This additional check for SPACE (keyCode 0x20) and subsequent preventDefault()
* prevents SPACE from bubbling up to the document event handlers, where the default
* behavior is typically to scroll the entire page -- a real nuisance.
*
* Keyboard.onKeyPress() has a similar issue, but it seems to be limited to Safari on iOS,
* first noticed on iOS 9.1. The problem there is that Safari's default SPACE behavior
* occurs BEFORE the onkeypress handler is called, so we would have to call preventDefault()
* in the onkeydown handler, but then the onkeypress handler would no longer be called.
*
* So, to resolve the Keyboard.onKeyPress() issue, we now define SPACE as an ONDOWN key,
* so that onKeyDown() will process the SPACE key immediately and automatically invoke
* preventDefault().
* Since we're going to remove the "readonly" attribute from the <textarea> control
* (so that the soft keyboard activates on iOS), instead of calling preventDefault() for
* selected keys (eg, the SPACE key, whose default behavior is to scroll the page), we must
* now call it for *all* keys, so that the keyCode isn't added to the control immediately,
* on top of whatever the machine is echoing back, resulting in double characters.
*/
if (keyCode == 0x20) {
if (event.preventDefault) event.preventDefault();
}
if (event.preventDefault) event.preventDefault();
return true;
};
/*
* Now that we've added an onkeypress handler that calls preventDefault() for ALL keys, the control
* itself no longer needs the "readonly" attribute; we primarily need to remove it for iOS browsers,
* so that the soft keyboard will activate, but it shouldn't hurt to remove the attribute for all browsers.
*/
control.removeAttribute("readonly");
return true;
default: