|
|
@ -1,6 +1,6 @@ |
|
|
|
window.initSoftKeyboard = function (screen) { |
|
|
|
window.initSoftKeyboard = function (screen, input) { |
|
|
|
const input = qs('#softkb-input') |
|
|
|
const keyInput = qs('#softkb-input') |
|
|
|
if (!input) return // abort, we're not on the terminal page
|
|
|
|
if (!keyInput) return // abort, we're not on the terminal page
|
|
|
|
|
|
|
|
|
|
|
|
let keyboardOpen = false |
|
|
|
let keyboardOpen = false |
|
|
|
|
|
|
|
|
|
|
@ -8,97 +8,101 @@ window.initSoftKeyboard = function (screen) { |
|
|
|
if (!keyboardOpen) return |
|
|
|
if (!keyboardOpen) return |
|
|
|
|
|
|
|
|
|
|
|
let [x, y] = screen.gridToScreen(screen.cursor.x, screen.cursor.y, true) |
|
|
|
let [x, y] = screen.gridToScreen(screen.cursor.x, screen.cursor.y, true) |
|
|
|
input.style.transform = `translate(${x}px, ${y}px)` |
|
|
|
keyInput.style.transform = `translate(${x}px, ${y}px)` |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
input.addEventListener('focus', () => { |
|
|
|
keyInput.addEventListener('focus', () => { |
|
|
|
keyboardOpen = true |
|
|
|
keyboardOpen = true |
|
|
|
updateInputPosition() |
|
|
|
updateInputPosition() |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
input.addEventListener('blur', () => (keyboardOpen = false)) |
|
|
|
keyInput.addEventListener('blur', () => (keyboardOpen = false)) |
|
|
|
|
|
|
|
|
|
|
|
screen.on('cursor-moved', updateInputPosition) |
|
|
|
screen.on('cursor-moved', updateInputPosition) |
|
|
|
|
|
|
|
|
|
|
|
window.kbOpen = function openSoftKeyboard (open) { |
|
|
|
window.kbOpen = function openSoftKeyboard (open) { |
|
|
|
keyboardOpen = open |
|
|
|
keyboardOpen = open |
|
|
|
updateInputPosition() |
|
|
|
updateInputPosition() |
|
|
|
if (open) input.focus() |
|
|
|
if (open) keyInput.focus() |
|
|
|
else input.blur() |
|
|
|
else keyInput.blur() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Chrome for Android doesn't send proper keydown/keypress events with
|
|
|
|
|
|
|
|
// real key values instead of 229 “Unidentified,” so here's a workaround
|
|
|
|
|
|
|
|
// that deals with the input composition events.
|
|
|
|
|
|
|
|
|
|
|
|
let lastCompositionString = '' |
|
|
|
let lastCompositionString = '' |
|
|
|
let compositing = false |
|
|
|
let compositing = false |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// sends the difference between the last and the new composition string
|
|
|
|
let sendInputDelta = function (newValue) { |
|
|
|
let sendInputDelta = function (newValue) { |
|
|
|
let resend = false |
|
|
|
let resend = false |
|
|
|
if (newValue.length > lastCompositionString.length) { |
|
|
|
if (newValue.length > lastCompositionString.length) { |
|
|
|
if (newValue.startsWith(lastCompositionString)) { |
|
|
|
if (newValue.startsWith(lastCompositionString)) { |
|
|
|
// characters have been added at the end
|
|
|
|
// characters have been added at the end
|
|
|
|
Input.sendString(newValue.substr(lastCompositionString.length)) |
|
|
|
input.sendString(newValue.substr(lastCompositionString.length)) |
|
|
|
} else resend = true |
|
|
|
} else resend = true |
|
|
|
} else if (newValue.length < lastCompositionString.length) { |
|
|
|
} else if (newValue.length < lastCompositionString.length) { |
|
|
|
if (lastCompositionString.startsWith(newValue)) { |
|
|
|
if (lastCompositionString.startsWith(newValue)) { |
|
|
|
// characters have been removed at the end
|
|
|
|
// characters have been removed at the end
|
|
|
|
Input.sendString('\b'.repeat(lastCompositionString.length - |
|
|
|
input.sendString('\b'.repeat(lastCompositionString.length - |
|
|
|
newValue.length)) |
|
|
|
newValue.length)) |
|
|
|
} else resend = true |
|
|
|
} else resend = true |
|
|
|
} else if (newValue !== lastCompositionString) resend = true |
|
|
|
} else if (newValue !== lastCompositionString) resend = true |
|
|
|
|
|
|
|
|
|
|
|
if (resend) { |
|
|
|
if (resend) { |
|
|
|
// the entire string changed; resend everything
|
|
|
|
// the entire string changed; resend everything
|
|
|
|
Input.sendString('\b'.repeat(lastCompositionString.length) + |
|
|
|
input.sendString('\b'.repeat(lastCompositionString.length) + |
|
|
|
newValue) |
|
|
|
newValue) |
|
|
|
} |
|
|
|
} |
|
|
|
lastCompositionString = newValue |
|
|
|
lastCompositionString = newValue |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
input.addEventListener('keydown', e => { |
|
|
|
keyInput.addEventListener('keydown', e => { |
|
|
|
if (e.key === 'Unidentified') return |
|
|
|
if (e.key === 'Unidentified') return |
|
|
|
|
|
|
|
|
|
|
|
input.value = '' |
|
|
|
keyInput.value = '' |
|
|
|
|
|
|
|
|
|
|
|
if (e.key === 'Backspace') { |
|
|
|
if (e.key === 'Backspace') { |
|
|
|
e.preventDefault() |
|
|
|
e.preventDefault() |
|
|
|
Input.sendString('\b') |
|
|
|
input.sendString('\b') |
|
|
|
} else if (e.key === 'Enter') { |
|
|
|
} else if (e.key === 'Enter') { |
|
|
|
e.preventDefault() |
|
|
|
e.preventDefault() |
|
|
|
Input.sendString('\x0d') |
|
|
|
input.sendString('\x0d') |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
input.addEventListener('keypress', e => { |
|
|
|
keyInput.addEventListener('keypress', e => { |
|
|
|
|
|
|
|
// prevent key duplication on iOS (because Safari *does* send proper events)
|
|
|
|
e.stopPropagation() |
|
|
|
e.stopPropagation() |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
input.addEventListener('input', e => { |
|
|
|
keyInput.addEventListener('input', e => { |
|
|
|
e.stopPropagation() |
|
|
|
e.stopPropagation() |
|
|
|
|
|
|
|
|
|
|
|
if (e.isComposing) { |
|
|
|
if (e.isComposing) { |
|
|
|
sendInputDelta(e.data) |
|
|
|
sendInputDelta(e.data) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if (e.inputType === 'insertCompositionText') Input.sendString(e.data) |
|
|
|
if (e.inputType === 'insertCompositionText') input.sendString(e.data) |
|
|
|
else if (e.inputType === 'deleteContentBackward') { |
|
|
|
else if (e.inputType === 'deleteContentBackward') { |
|
|
|
lastCompositionString = '' |
|
|
|
lastCompositionString = '' |
|
|
|
sendInputDelta('') |
|
|
|
sendInputDelta('') |
|
|
|
} else if (e.inputType === 'insertText') { |
|
|
|
} else if (e.inputType === 'insertText') { |
|
|
|
Input.sendString(e.data) |
|
|
|
input.sendString(e.data) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
input.addEventListener('compositionstart', e => { |
|
|
|
keyInput.addEventListener('compositionstart', e => { |
|
|
|
lastCompositionString = '' |
|
|
|
lastCompositionString = '' |
|
|
|
compositing = true |
|
|
|
compositing = true |
|
|
|
console.log('compositionstart') |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
input.addEventListener('compositionend', e => { |
|
|
|
keyInput.addEventListener('compositionend', e => { |
|
|
|
lastCompositionString = '' |
|
|
|
lastCompositionString = '' |
|
|
|
compositing = false |
|
|
|
compositing = false |
|
|
|
input.value = '' |
|
|
|
keyInput.value = '' |
|
|
|
console.log('compositionend') |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
screen.on('open-soft-keyboard', () => input.focus()) |
|
|
|
screen.on('open-soft-keyboard', () => keyInput.focus()) |
|
|
|
} |
|
|
|
} |
|
|
|