added const key for more reliable key override

This commit is contained in:
Stefan Friese 2026-06-14 22:14:22 +02:00
parent bc09faf7cb
commit abe8316d5e
1 changed files with 76 additions and 12 deletions

View File

@ -15,40 +15,45 @@ document.addEventListener("DOMContentLoaded", function() {
} }
}); });
window.addEventListener("keydown", function(event) { // window.addEventListener("keydown", function(event) {
input.addEventListener("keydown", function(event) {
if (document.activeElement !== input) return; if (document.activeElement !== input) return;
const cursorPos = input.selectionStart; const cursorPos = input.selectionStart;
const textLength = input.value.length; const textLength = input.value.length;
// Prevent default behavior for specific Ctrl+Key shortcuts // Prevent default behavior for specific Ctrl+Key shortcuts
if (event.ctrlKey && ["w", "n", "p", "h", "e", "a", "k", "u", "d", "r", "t"].includes(event.key.toLowerCase())) { const key = event.key.toLowerCase();
if (
// event.ctrlKey && [" ", "b", "f", "w", "n", "p", "h", "e", "a", "k", "u", "d", "r", "t"].includes(key)) ||
// event.altKey && ["b", "d", "f"].includes(key) {
event.ctrlKey && [" ", "w", "n", "p", "h", "e", "a", "k", "u", "d", "r", "t"].includes(key)) {
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
} }
// Ctrl+A: Move cursor to the beginning // Ctrl+A: Move cursor to the beginning
if (event.ctrlKey && event.key === "a") { if (event.ctrlKey && key === "a") {
input.setSelectionRange(0, 0); input.setSelectionRange(0, 0);
} }
// Ctrl+E: Move cursor to the end // Ctrl+E: Move cursor to the end
else if (event.ctrlKey && event.key === "e") { else if (event.ctrlKey && key === "e") {
input.setSelectionRange(textLength, textLength); input.setSelectionRange(textLength, textLength);
} }
// Ctrl+U: Clear the input field // Ctrl+U: Clear the input field
else if (event.ctrlKey && event.key === "u") { else if (event.ctrlKey && key === "u") {
input.value = ""; input.value = "";
} }
// Ctrl+K: Delete everything after the cursor // Ctrl+K: Delete everything after the cursor
else if (event.ctrlKey && event.key === "k") { else if (event.ctrlKey && key === "k") {
input.value = input.value.substring(0, cursorPos); input.value = input.value.substring(0, cursorPos);
} }
// Ctrl+W: Delete the previous word // Ctrl+W: Delete the previous word
else if (event.ctrlKey && event.key === "w") { else if (event.ctrlKey && key === " ") {
const beforeCursor = input.value.substring(0, cursorPos); const beforeCursor = input.value.substring(0, cursorPos);
const afterCursor = input.value.substring(cursorPos); const afterCursor = input.value.substring(cursorPos);
const newBeforeCursor = beforeCursor.replace(/\S+\s*$/, ""); // Delete last word const newBeforeCursor = beforeCursor.replace(/\S+\s*$/, ""); // Delete last word
@ -57,7 +62,7 @@ document.addEventListener("DOMContentLoaded", function() {
} }
// Ctrl+H: Delete the previous character (Backspace) // Ctrl+H: Delete the previous character (Backspace)
else if (event.ctrlKey && event.key === "h") { else if (event.ctrlKey && key === "h") {
if (cursorPos > 0) { if (cursorPos > 0) {
input.value = input.value.substring(0, cursorPos - 1) + input.value.substring(cursorPos); input.value = input.value.substring(0, cursorPos - 1) + input.value.substring(cursorPos);
input.setSelectionRange(cursorPos - 1, cursorPos - 1); input.setSelectionRange(cursorPos - 1, cursorPos - 1);
@ -65,7 +70,7 @@ document.addEventListener("DOMContentLoaded", function() {
} }
// Ctrl+D: Delete character under cursor (or clear input if empty) // Ctrl+D: Delete character under cursor (or clear input if empty)
else if (event.ctrlKey && event.key === "d") { else if (event.ctrlKey && key === "d") {
if (textLength === 0) { if (textLength === 0) {
console.log("Ctrl+D: No input, simulating EOF"); console.log("Ctrl+D: No input, simulating EOF");
} else if (cursorPos < textLength) { } else if (cursorPos < textLength) {
@ -75,7 +80,7 @@ document.addEventListener("DOMContentLoaded", function() {
} }
// Ctrl+P: Previous command (up) // Ctrl+P: Previous command (up)
else if (event.ctrlKey && event.key === "p") { else if (event.ctrlKey && key === "p") {
if (historyIndex > 0) { if (historyIndex > 0) {
historyIndex--; historyIndex--;
input.value = commandHistory[historyIndex]; input.value = commandHistory[historyIndex];
@ -85,7 +90,7 @@ document.addEventListener("DOMContentLoaded", function() {
} }
// Ctrl+N: Next command (down) // Ctrl+N: Next command (down)
else if (event.ctrlKey && event.key === "n") { else if (event.ctrlKey && key === "n") {
if (historyIndex < commandHistory.length - 1) { if (historyIndex < commandHistory.length - 1) {
historyIndex++; historyIndex++;
input.value = commandHistory[historyIndex]; input.value = commandHistory[historyIndex];
@ -96,10 +101,69 @@ document.addEventListener("DOMContentLoaded", function() {
} }
// Ctrl+R: Prevent page reload (for future reverse search) // Ctrl+R: Prevent page reload (for future reverse search)
else if (event.ctrlKey && event.key === "r") { else if (event.ctrlKey && key === "r") {
console.log("Reverse search triggered (not yet implemented)"); console.log("Reverse search triggered (not yet implemented)");
} }
// Ctrl+B: Move back one character
// else if (event.ctrlKey && key === "b") {
// event.preventDefault();
// input.setSelectionRange(
// Math.max(0, cursorPos - 1),
// Math.max(0, cursorPos - 1)
// );
// }
// Ctrl + F: Move Forward one character
// else if (event.ctrlKey && key === "f") {
// event.preventDefault();
// input.setSelectionRange(
// Math.max(0, cursorPos + 1),
// Math.max(0, cursorPos + 1)
// );
// }
// Alt + B: Move back one word
// else if (event.altKey && key === "b") {
// event.preventDefault();
// const before = input.value.substring(0, cursorPos);
// // Skip whitespace
// let pos = before.length;
// while(pos > 0 && /\s/.test(before[pos - 1])) pos--;
// // Skip word characters
// while (pos > 0 && !/\s/.test(before[pos - 1])) pos--;
// input.setSelectionRange(pos, pos);
// }
// Alt + F: Move forward one word
// else if (event.altKey && key == "f") {
// event.preventDefault();
// const after = input.value.subtring(cursorPos);
// let offset = 0;
// // Skip current word
// while (offset < after.length && !/\s/.test(after[offset])) offset++;
// // Skip following whitespace
// while (offset < after.length && /\s/.test(after[offset])) offset++;
// const newPos = cursorPos + offset;
// input.setSelectionRange(newPos, newPos);
// }
// Alt + D: Delete word after cursor
// else if (event.altkey && key == "d") {
// event.preventDefault();
// const before = input.value.substring(0, cursorPos);
// const after = input.value.substring(cursorPos);
// const newAfter = after.replace(/^\s*\S+\s*/, "");
// input.value = before + newAfter;
// input.setSelectionRange(cursorPos, cursorPos);
// }
// Tab Completion // Tab Completion
else if (event.key === "Tab") { else if (event.key === "Tab") {
event.preventDefault(); event.preventDefault();