Use proper soft keyboard events
...or as close as it gets because the way input events work on mobile is awful.
This commit is contained in:
@@ -23,16 +23,63 @@ $.ready(() => {
|
|||||||
else input.blur()
|
else input.blur()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let lastCompositionString = '';
|
||||||
|
let compositing = false;
|
||||||
|
|
||||||
|
let sendInputDelta = function (newValue) {
|
||||||
|
let resend = false;
|
||||||
|
if (newValue.length > lastCompositionString.length) {
|
||||||
|
if (newValue.startsWith(lastCompositionString)) {
|
||||||
|
// characters have been added at the end
|
||||||
|
Input.sendString(newValue.substr(lastCompositionString.length))
|
||||||
|
} else resend = true;
|
||||||
|
} else if (newValue.length < lastCompositionString.length) {
|
||||||
|
if (lastCompositionString.startsWith(newValue)) {
|
||||||
|
// characters have been removed at the end
|
||||||
|
Input.sendString('\b'.repeat(lastCompositionString.length -
|
||||||
|
newValue.length))
|
||||||
|
} else resend = true;
|
||||||
|
} else if (newValue !== lastCompositionString) resend = true;
|
||||||
|
|
||||||
|
if (resend) {
|
||||||
|
// the entire string changed; resend everything
|
||||||
|
Input.sendString('\b'.repeat(lastCompositionString.length) +
|
||||||
|
newValue)
|
||||||
|
}
|
||||||
|
lastCompositionString = newValue;
|
||||||
|
}
|
||||||
|
|
||||||
input.addEventListener('keydown', e => {
|
input.addEventListener('keydown', e => {
|
||||||
if (e.key === 'Backspace') {
|
if (e.key === 'Unidentified') return;
|
||||||
e.preventDefault()
|
|
||||||
e.stopPropagation()
|
e.preventDefault();
|
||||||
Input.sendString('\b')
|
input.value = '';
|
||||||
|
|
||||||
|
if (e.key === 'Backspace') Input.sendString('\b')
|
||||||
|
else if (e.key === 'Enter') Input.sendString('\x0d')
|
||||||
|
})
|
||||||
|
input.addEventListener('input', e => {
|
||||||
|
e.stopPropagation();
|
||||||
|
|
||||||
|
if (e.isComposing) {
|
||||||
|
sendInputDelta(e.data);
|
||||||
|
} else {
|
||||||
|
if (e.data) Input.sendString(e.data);
|
||||||
|
else if (e.inputType === 'deleteContentBackward') {
|
||||||
|
lastCompositionString
|
||||||
|
sendInputDelta('');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
input.addEventListener('keypress', e => {
|
input.addEventListener('compositionstart', e => {
|
||||||
e.stopPropagation()
|
lastCompositionString = '';
|
||||||
})
|
compositing = true;
|
||||||
|
});
|
||||||
|
input.addEventListener('compositionend', e => {
|
||||||
|
lastCompositionString = '';
|
||||||
|
compositing = false;
|
||||||
|
input.value = '';
|
||||||
|
});
|
||||||
|
|
||||||
Screen.on('open-soft-keyboard', () => input.focus())
|
Screen.on('open-soft-keyboard', () => input.focus())
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -333,9 +333,9 @@ class TermScreen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// schedule a draw in the next tick
|
// schedule a draw in the next tick
|
||||||
scheduleDraw () {
|
scheduleDraw (aggregateTime = 1) {
|
||||||
clearTimeout(this._scheduledDraw);
|
clearTimeout(this._scheduledDraw);
|
||||||
this._scheduledDraw = setTimeout(() => this.draw(), 1)
|
this._scheduledDraw = setTimeout(() => this.draw(), aggregateTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
getFont (modifiers = {}) {
|
getFont (modifiers = {}) {
|
||||||
@@ -788,7 +788,7 @@ class TermScreen {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.scheduleDraw();
|
this.scheduleDraw(16);
|
||||||
this.emit('load');
|
this.emit('load');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -71,15 +71,6 @@
|
|||||||
window.noAutoShow = true;
|
window.noAutoShow = true;
|
||||||
termInit(); // the screen will be loaded via ajax
|
termInit(); // the screen will be loaded via ajax
|
||||||
Screen.load('%j:labels_seq%');
|
Screen.load('%j:labels_seq%');
|
||||||
|
|
||||||
// auto-clear the input box
|
|
||||||
$('#softkb-input').on('input', function(e) {
|
|
||||||
setTimeout(function(){
|
|
||||||
var str = $('#softkb-input').val();
|
|
||||||
$('#softkb-input').val('');
|
|
||||||
Input.sendString(str);
|
|
||||||
}, 1);
|
|
||||||
});
|
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
console.error("Fail, reloading in 3s…");
|
console.error("Fail, reloading in 3s…");
|
||||||
@@ -87,10 +78,4 @@
|
|||||||
location.reload(true);
|
location.reload(true);
|
||||||
}, 3000);
|
}, 3000);
|
||||||
}
|
}
|
||||||
|
|
||||||
function kbOpen(yes) {
|
|
||||||
var i = qs('#softkb-input');
|
|
||||||
if (yes) i.focus();
|
|
||||||
else i.blur();
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user