ui: use event.key instead of event.keyCode for various keybindings

This commit is contained in:
jean-emmanuel 2025-02-22 10:04:28 +01:00
parent 107ca1d295
commit c554a23c99
10 changed files with 23 additions and 23 deletions

View file

@ -40,7 +40,7 @@ class CodeEditor {
this.closeKey = ((e)=>{
// this handler may remain dangling if closing with ctrl+enter
// we ignore this as it has no effect
if (e.keyCode !== 27) return
if (e.key != 'Escape') return
this.field.classList.remove('fullscreen')
this.editor.setOptions({maxLines: 30})
this.editor.fullscreen = false
@ -52,7 +52,7 @@ class CodeEditor {
this.closeClick = ((e)=>{
if (e.target === this.field) {
this.closeKey({keyCode: 27})
this.closeKey({key: 'Escape'})
}
}).bind(this)

View file

@ -46,11 +46,11 @@ class ColorPicker extends UiWidget {
this.opened = 0
this.escKeyHandler = ((e)=>{
if (e.keyCode==27) this.close()
if (e.key == 'Escape') this.close()
}).bind(this)
this.enterKeyHandler = ((e)=>{
if (e.keyCode == 13) this.confirm()
if (e.key == 'Enter') this.confirm()
}).bind(this)
}

View file

@ -69,10 +69,10 @@ class UiConsole extends UiSidePanel {
this.widget.hash = 'CONSOLE'
this.input.addEventListener('keydown', (event)=>{
if (event.keyCode === 13 && !event.shiftKey) {
if (event.key === 'Enter' && !event.shiftKey) {
event.preventDefault()
this.inputValidate()
} else if (event.keyCode === 38) {
} else if (event.key == 'ArrowUp') {
if (this.cursor < this.history.length - 1) {
if (this.input.value.substr(0, this.input.selectionStart).split('\n').length !== 1) return
@ -81,7 +81,7 @@ class UiConsole extends UiSidePanel {
this.input.value = this.history[this.cursor]
event.preventDefault()
}
} else if (event.keyCode === 40) {
} else if (event.key == 'ArrowDown') {
if (this.cursor > 0) {
var nLines = this.input.value.split('\n').length
if (this.input.value.substr(0, this.input.selectionStart).split('\n').length !== nLines) return
@ -90,7 +90,7 @@ class UiConsole extends UiSidePanel {
this.input.value = this.history[this.cursor]
event.preventDefault()
}
} else if (event.keyCode === 9 && this.input.value !== '') {
} else if (event.key == 'DOM_VK_TAB' && this.input.value !== '') {
var cur = this.input.selectionStart,
val = this.input.value
this.input.value = val.slice(0, cur) + ' ' + val.slice(cur)

View file

@ -95,7 +95,7 @@ module.exports = function UiFilebrowser(options, callback) {
function keyDownHandler(e){
if (e.keyCode === 13) {
if (e.key == 'Enter') {
if (save) {
document.addEventListener('keyup', keyUpHandler)
} else {
@ -103,7 +103,7 @@ module.exports = function UiFilebrowser(options, callback) {
}
} else if (e.target === saveInput) {
return
} else if (e.keyCode === 8) {
} else if (e.key == 'DOM_VK_BACK_SPACE') {
list.childNodes[0].firstElementChild.checked = true
submit()
} else if (e.key.length === 1) {
@ -131,7 +131,7 @@ module.exports = function UiFilebrowser(options, callback) {
}
function keyUpHandler(e){
if (e.keyCode === 13) {
if (e.key == 'Enter') {
setTimeout(submit, 100)
document.removeEventListener('keyup', keyUpHandler)
}

View file

@ -330,7 +330,7 @@ class UiInspector extends UiWidget {
if (event.target.tagName === 'TEXTAREA') {
if (event.keyCode === 13 &&
if (event.key == 'Enter' &&
((!event.shiftKey && !event.target._ace) ||
(event[codeEditorModKey] && event.target._ace))
) {
@ -338,7 +338,7 @@ class UiInspector extends UiWidget {
event.preventDefault()
DOM.dispatchEvent(event.target, 'change')
} else if (event.keyCode === 27) {
} else if (event.key == 'Escape') {
event.preventDefault()
event.target.value = event.target.textContent

View file

@ -56,13 +56,13 @@ class UiModal extends UiWidget {
if (this.escKey) {
this.escKeyHandler = ((e)=>{
if (e.keyCode==27) this.close()
if (e.key == 'Escape') this.close()
}).bind(this)
}
if (this.enterKey) {
this.enterKeyHandler = ((e)=>{
if (e.keyCode == 13) this.enterKey.call(this, e)
if (e.key == 'Enter') this.enterKey.call(this, e)
}).bind(this)
}

View file

@ -31,12 +31,12 @@ class Zoom extends EventEmitter {
document.addEventListener('keydown', (event)=>{
if (event[mod]) {
if (event.keyCode == 96 || event.keyCode == 48) {
if (event.key == 'DOM_VK_0' || event.key == 'Numpad0') {
this.setGlobalZoom(INITIALZOOM)
this.setLocalZoom(1)
}
else if (event.keyCode == 107 || event.keyCode == 109) {
this.setGlobalZoom((event.keyCode == 107 ? 0.1 : -0.1) + PXSCALE)
else if (event.key == '+' || event.key == '-') {
this.setGlobalZoom((event.key == '+' ? 0.1 : -0.1) + PXSCALE)
}
}

View file

@ -90,8 +90,8 @@ class Input extends Canvas {
}
this.input.addEventListener('keydown', (e)=>{
if (e.keyCode === 13) this.blur() // enter
else if (e.keyCode === 27) this.blur(false) // esc
if (e.key == 'Enter') this.blur()
else if (e.key == 'Escape') this.blur(false)
else if (asYouType) {
setTimeout(()=>{
this.inputChange()

View file

@ -49,12 +49,12 @@ class TextArea extends Widget {
}, {element: this.input})
this.input.addEventListener('keydown', (e)=>{
if (e.keyCode === 13 && !e.shiftKey) {
if (e.key == 'Enter' && !e.shiftKey) {
// enter
e.preventDefault()
this.blur()
}
else if (e.keyCode === 27) {
else if (e.key == 'Escape') {
// esc
this.blur(false)
this.input.value = this.value

View file

@ -123,7 +123,7 @@ class Modal extends Panel {
this.escapeKeyHandler = ((e)=>{
if (e.keyCode==27) this.setValue(0, {sync:true, send:true})
if (e.key == 'Escape') this.setValue(0, {sync:true, send:true})
}).bind(this)
this.value = 0