You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
3.3 KiB
108 lines
3.3 KiB
7 years ago
|
const { qs } = require('../utils')
|
||
7 years ago
|
|
||
|
module.exports = function (screen, input) {
|
||
7 years ago
|
const keyInput = qs('#softkb-input')
|
||
|
if (!keyInput) return // abort, we're not on the terminal page
|
||
7 years ago
|
|
||
7 years ago
|
let keyboardOpen = false
|
||
7 years ago
|
|
||
7 years ago
|
// moves the input to where the cursor is on the canvas.
|
||
|
// this is because most browsers will always scroll to wherever the focused
|
||
|
// input is
|
||
7 years ago
|
let updateInputPosition = function () {
|
||
7 years ago
|
if (!keyboardOpen) return
|
||
7 years ago
|
|
||
7 years ago
|
let [x, y] = screen.gridToScreen(screen.cursor.x, screen.cursor.y, true)
|
||
7 years ago
|
keyInput.style.transform = `translate(${x}px, ${y}px)`
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
7 years ago
|
keyInput.addEventListener('focus', () => {
|
||
7 years ago
|
keyboardOpen = true
|
||
7 years ago
|
updateInputPosition()
|
||
7 years ago
|
})
|
||
7 years ago
|
|
||
7 years ago
|
keyInput.addEventListener('blur', () => (keyboardOpen = false))
|
||
7 years ago
|
|
||
7 years ago
|
screen.on('cursor-moved', updateInputPosition)
|
||
7 years ago
|
|
||
7 years ago
|
qs('#term-kb-open').addEventListener('click', e => {
|
||
|
e.preventDefault()
|
||
|
keyInput.focus()
|
||
7 years ago
|
})
|
||
|
|
||
7 years ago
|
// 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.
|
||
|
|
||
7 years ago
|
let lastCompositionString = ''
|
||
7 years ago
|
|
||
7 years ago
|
// sends the difference between the last and the new composition string
|
||
7 years ago
|
let sendInputDelta = function (newValue) {
|
||
7 years ago
|
let resend = false
|
||
7 years ago
|
if (newValue.length > lastCompositionString.length) {
|
||
|
if (newValue.startsWith(lastCompositionString)) {
|
||
|
// characters have been added at the end
|
||
7 years ago
|
input.sendString(newValue.substr(lastCompositionString.length))
|
||
7 years ago
|
} else resend = true
|
||
7 years ago
|
} else if (newValue.length < lastCompositionString.length) {
|
||
|
if (lastCompositionString.startsWith(newValue)) {
|
||
|
// characters have been removed at the end
|
||
7 years ago
|
input.sendString('\b'.repeat(lastCompositionString.length -
|
||
7 years ago
|
newValue.length))
|
||
7 years ago
|
} else resend = true
|
||
|
} else if (newValue !== lastCompositionString) resend = true
|
||
7 years ago
|
|
||
|
if (resend) {
|
||
|
// the entire string changed; resend everything
|
||
7 years ago
|
input.sendString('\b'.repeat(lastCompositionString.length) +
|
||
7 years ago
|
newValue)
|
||
|
}
|
||
7 years ago
|
lastCompositionString = newValue
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
keyInput.addEventListener('keydown', e => {
|
||
7 years ago
|
if (e.key === 'Unidentified') return
|
||
7 years ago
|
|
||
7 years ago
|
keyInput.value = ''
|
||
7 years ago
|
|
||
7 years ago
|
e.stopPropagation()
|
||
|
input.handleKeyDown(e)
|
||
7 years ago
|
})
|
||
7 years ago
|
|
||
7 years ago
|
keyInput.addEventListener('keypress', e => {
|
||
|
// prevent key duplication on iOS (because Safari *does* send proper events)
|
||
7 years ago
|
e.stopPropagation()
|
||
|
})
|
||
|
|
||
7 years ago
|
keyInput.addEventListener('input', e => {
|
||
7 years ago
|
e.stopPropagation()
|
||
7 years ago
|
|
||
7 years ago
|
if (e.isComposing && 'data' in e) {
|
||
7 years ago
|
sendInputDelta(e.data)
|
||
7 years ago
|
} else if (e.isComposing) {
|
||
|
// Firefox Mobile doesn't support InputEvent#data, so here's a hack
|
||
|
// that just takes the input value and uses that
|
||
|
sendInputDelta(keyInput.value)
|
||
7 years ago
|
} else {
|
||
7 years ago
|
if (e.inputType === 'insertCompositionText') input.sendString(e.data)
|
||
7 years ago
|
else if (e.inputType === 'deleteContentBackward') {
|
||
7 years ago
|
lastCompositionString = ''
|
||
|
sendInputDelta('')
|
||
7 years ago
|
} else if (e.inputType === 'insertText') {
|
||
7 years ago
|
input.sendString(e.data)
|
||
7 years ago
|
}
|
||
|
}
|
||
7 years ago
|
})
|
||
7 years ago
|
|
||
7 years ago
|
keyInput.addEventListener('compositionstart', e => {
|
||
7 years ago
|
lastCompositionString = ''
|
||
|
})
|
||
7 years ago
|
|
||
7 years ago
|
keyInput.addEventListener('compositionend', e => {
|
||
7 years ago
|
lastCompositionString = ''
|
||
7 years ago
|
keyInput.value = ''
|
||
7 years ago
|
})
|
||
7 years ago
|
|
||
7 years ago
|
screen.on('open-soft-keyboard', () => keyInput.focus())
|
||
7 years ago
|
}
|