From cc28a69d854825a2a61d82c5d0d3f312392e43e7 Mon Sep 17 00:00:00 2001 From: cpsdqs Date: Sat, 23 Sep 2017 02:18:52 +0200 Subject: [PATCH] Add iOS keyboard shortcut bar --- js/term/soft_keyboard.js | 38 +++++++++++++++++++++++++++++++++++++- pages/_head.php | 2 +- pages/term.php | 4 ++-- sass/pages/_term.scss | 25 +++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/js/term/soft_keyboard.js b/js/term/soft_keyboard.js index 08299ba..6cb3a77 100644 --- a/js/term/soft_keyboard.js +++ b/js/term/soft_keyboard.js @@ -4,6 +4,12 @@ module.exports = function (screen, input) { const keyInput = qs('#softkb-input') if (!keyInput) return // abort, we're not on the terminal page + const shortcutBar = document.createElement('div') + shortcutBar.id = 'keyboard-shortcut-bar' + if (navigator.userAgent.match(/iPad|iPhone|iPod/)) { + qs('#screen').appendChild(shortcutBar) + } + let keyboardOpen = false // moves the input to where the cursor is on the canvas. @@ -19,9 +25,13 @@ module.exports = function (screen, input) { keyInput.addEventListener('focus', () => { keyboardOpen = true updateInputPosition() + shortcutBar.classList.add('open') }) - keyInput.addEventListener('blur', () => (keyboardOpen = false)) + keyInput.addEventListener('blur', () => { + keyboardOpen = false + shortcutBar.classList.remove('open') + }) screen.on('cursor-moved', updateInputPosition) @@ -104,4 +114,30 @@ module.exports = function (screen, input) { }) screen.on('open-soft-keyboard', () => keyInput.focus()) + + // shortcut bar + const shortcuts = { + Tab: 0x09, + '←': 0x25, + '↓': 0x28, + '↑': 0x26, + '→': 0x27, + '^C': { which: 0x43, ctrlKey: true } + } + + for (const shortcut in shortcuts) { + const button = document.createElement('button') + button.classList.add('shortcut-button') + button.textContent = shortcut + shortcutBar.appendChild(button) + + const key = shortcuts[shortcut] + button.addEventListener('click', e => { + e.preventDefault() + let fakeEvent = key + if (typeof key === 'number') fakeEvent = { which: key } + fakeEvent.preventDefault = () => {} + input.handleKeyDown(fakeEvent) + }) + } } diff --git a/pages/_head.php b/pages/_head.php index a5d3cd7..9466a12 100644 --- a/pages/_head.php +++ b/pages/_head.php @@ -3,7 +3,7 @@ - + <?= $_GET['PAGE_TITLE'] ?> diff --git a/pages/term.php b/pages/term.php index 101e7be..4ae1af0 100644 --- a/pages/term.php +++ b/pages/term.php @@ -41,6 +41,7 @@

+
@@ -60,8 +61,7 @@