diff --git a/app/static/js/app.js b/app/static/js/app.js index 28faa82..6464164 100644 --- a/app/static/js/app.js +++ b/app/static/js/app.js @@ -636,9 +636,11 @@ function setupEventListeners() { sendMessage(); }); - // Handle Enter key (send) vs Shift+Enter (new line) + // Handle Enter key (send) vs Shift+Enter (new line) - desktop only. + // Touch devices send via the button so an accidental Enter on the virtual + // keyboard doesn't fire a half-typed message. input.addEventListener('keydown', function(e) { - if (e.key === 'Enter' && !e.shiftKey) { + if (e.key === 'Enter' && !e.shiftKey && !isTouchPrimaryDevice()) { e.preventDefault(); sendMessage(); } diff --git a/app/static/js/dm.js b/app/static/js/dm.js index 0427ec1..93a9e7b 100644 --- a/app/static/js/dm.js +++ b/app/static/js/dm.js @@ -437,9 +437,10 @@ function setupEventListeners() { if (input) { input.addEventListener('input', updateCharCounter); - // Enter key to send + // Enter key to send (desktop only - touch devices send via the button + // so an accidental Enter on the virtual keyboard doesn't fire a half-typed message) input.addEventListener('keydown', function(e) { - if (e.key === 'Enter' && !e.shiftKey) { + if (e.key === 'Enter' && !e.shiftKey && !isTouchPrimaryDevice()) { e.preventDefault(); sendMessage(); } diff --git a/app/static/js/message-utils.js b/app/static/js/message-utils.js index aa6f3c8..ba43eee 100644 --- a/app/static/js/message-utils.js +++ b/app/static/js/message-utils.js @@ -3,6 +3,16 @@ * Handles mention badges, URL links, and image previews */ +/** + * Detect whether the primary input is touch-based (phones/tablets), as opposed + * to a device with a precise pointer (mouse/trackpad). Used to decide whether + * Enter should send a message or just insert a newline. + * @returns {boolean} + */ +function isTouchPrimaryDevice() { + return window.matchMedia('(pointer: coarse)').matches; +} + /** * Process message content to handle mentions, URLs, and images * @param {string} content - Raw message content