From dd482d6d5a9fc31e9a9c34bfe7eb839d4a83f271 Mon Sep 17 00:00:00 2001 From: G-Fourteen Date: Thu, 11 Sep 2025 15:48:47 -0600 Subject: [PATCH] Improve Enter key handling --- chat-core.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/chat-core.js b/chat-core.js index 7b8be07..f99e166 100644 --- a/chat-core.js +++ b/chat-core.js @@ -28,15 +28,26 @@ window.aiInstructionPromise = fetch("ai-instruct.txt") // Utility: allow Enter to send messages and Shift+Enter for new lines window.setupEnterToSend = function(textarea, sendCallback) { if (!textarea || typeof sendCallback !== "function") return; - const handler = (e) => { + + let composing = false; + textarea.addEventListener("compositionstart", () => { composing = true; }); + textarea.addEventListener("compositionend", () => { composing = false; }); + + const onKeyDown = (e) => { const key = e.key || e.keyCode; const isEnter = key === "Enter" || key === 13; - if (isEnter && !e.shiftKey && !e.isComposing && !e.repeat) { + + // Only send on plain Enter (no modifiers) and never while composing text + if (isEnter && + !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey && + !composing && !e.repeat) { e.preventDefault(); sendCallback(); } }; - textarea.addEventListener("keydown", handler); + + // Capture = true to run before other bubbling listeners + textarea.addEventListener("keydown", onKeyDown, { capture: true }); }; document.addEventListener("DOMContentLoaded", () => {