Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

made webterm use keycodes to detect special/control keys #1505

Merged
merged 1 commit into from
Feb 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions app/userland/webterm/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,36 @@ window.html.render = render

const TAB_COMPLETION_RENDER_LIMIT = 15


// Look up control/navigation keys via keycode.
//
// Taken from https://github.com/ccampbell/mousetrap/blob/2f9a476ba6158ba69763e4fcf914966cc72ef433/mousetrap.js#L39-L62,
// licensed under the Apache-2.0.
const KEYCODE_MAP = {
8: 'backspace',
9: 'tab',
13: 'enter',
16: 'shift',
17: 'ctrl',
18: 'alt',
20: 'capslock',
27: 'esc',
32: 'space',
33: 'pageup',
34: 'pagedown',
35: 'end',
36: 'home',
37: 'left',
38: 'up',
39: 'right',
40: 'down',
45: 'ins',
46: 'del',
91: 'meta',
93: 'meta',
224: 'meta'
}

class WebTerm extends LitElement {
static get styles () {
return [css]
Expand Down Expand Up @@ -726,20 +756,21 @@ class WebTerm extends LitElement {
// =

onKeyDown (e) {
let keycode = KEYCODE_MAP[e.which];
if (e.code === 'KeyL' && e.ctrlKey) {
e.preventDefault()
this.clearHistory()
} else if (e.code === 'ArrowUp' || (e.code === 'KeyP' && e.ctrlKey)) {
} else if ((keycode === 'up') || (e.code === 'KeyP' && e.ctrlKey)) {
e.preventDefault()
this.shadowRoot.querySelector('.prompt input').value = this.commandHist.prevUp()
} else if (e.code === 'ArrowDown' || (e.code === 'KeyN' && e.ctrlKey)) {
} else if ((keycode === 'down') || (e.code === 'KeyN' && e.ctrlKey)) {
e.preventDefault()
this.shadowRoot.querySelector('.prompt input').value = this.commandHist.prevDown()
} else if (e.code === 'Escape') {
} else if ((keycode === 'esc') || (e.code === 'Escape')) {
e.preventDefault()
this.shadowRoot.querySelector('.prompt input').value = ''
this.commandHist.reset()
} else if (e.code === 'Tab') {
} else if ((keycode === 'tab') || (e.code === 'Tab')) {
// NOTE: subtle behavior here-
// we prevent default on keydown to maintain focus
// we trigger tabcomplete on keyup to make sure it runs after
Expand All @@ -750,9 +781,10 @@ class WebTerm extends LitElement {
}

onPromptKeyUp (e) {
if (e.code === 'Enter') {
let keycode = KEYCODE_MAP[e.which];
if ((keycode === 'enter') || (e.code === 'Enter')) {
this.evalPrompt()
} else if (e.code === 'Tab') {
} else if ((keycode === 'tab') || (e.code === 'Tab')) {
this.triggerTabComplete()
} else {
this.readTabCompletionOptions()
Expand Down