From 86f0c98ed03457f24cd3b517e46b53f6ff3778bd Mon Sep 17 00:00:00 2001 From: Utopiah Date: Wed, 5 Aug 2026 18:14:23 +0200 Subject: [PATCH] sequences --- kbd.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/kbd.js b/kbd.js index dc0beb8..57de3da 100644 --- a/kbd.js +++ b/kbd.js @@ -20,10 +20,18 @@ function onError(error) { } let key, modifiers = [] +let seq = '' + document.addEventListener("keydown", function(event) { if (event.key.length === 1 || event.key === event.code) { // 'a', 'A', '1', '.', '%', 'Escape', 'Backspace' etc key = event.key + if (key == 'Escape') { + seq = '' + modifiers = [] + return + } + if ( event.key.length === 1) seq += key // does not take modifiers into account per key, e.g. IoT works but not ^i^o^t } else { // 'Alt', 'Control', 'Shift' modifiers.push( event.key ) @@ -31,12 +39,20 @@ document.addEventListener("keydown", function(event) { console.log( key, modifiers ) if (blocked) return if (key && modifiers.length > 0 ) - userActions.filter( a => key == a.key && a.mods && a.mods[0] == modifiers[0] ).map( a => eval( a.action ) ) + userActions.filter( a => a.key && key == a.key && a.mods && a.mods[0] == modifiers[0] ).map( a => doAction( a.action ) ) // take single modifiers into account, must be more restrictve else - userActions.filter( a => key == a.key && typeof ( a.mods ) === 'undefined' ).map( a => eval( a.action ) ) + userActions.filter( a => a.key && key == a.key && typeof ( a.mods ) === 'undefined' ).map( a => doAction( a.action ) ) + userActions.filter( a => a.seq == seq ).map( a => doAction( a.action ) ) }) +function doAction( action ){ + console.log( 'executing', action ) // for debugging + eval(action) + seq = '' + modifiers = [] +} + document.addEventListener("keyup", function(event) { if (event.key.length === 1 || event.key === event.code) { // 'a', 'A', '1', '.', '%', 'Escape', 'Backspace' etc @@ -50,6 +66,11 @@ document.addEventListener("keyup", function(event) { // could then grow userActions by the user themselves, save in localStorage, etc without having to rebuild extension // note that i is different from I (which is here captured as I + Shift) let userActions = [ + { seq: 'gtm', action: 'window.open("https://twitter-archive.benetou.fr/")'}, + { seq: 'gg', action: 'window.scrollTo(0, 0)'}, + { key: 'G', mods: ['Shift'], action: 'window.scrollTo(0, document.body.scrollHeight)'}, + { key: 'E', mods: ['Shift'], action: 'window.location += "?action=edit"'}, + // this now prevent any binding on g { key: 'r', action: 'window.location.reload()'}, { key: 'x', action: 'autoscroll()'}, { key: 'b', action: 'history.back()'},