/** The part of a key event the composer reads. */ export interface ComposerKey { key: string; shiftKey: boolean; keyCode?: number; nativeEvent?: { isComposing?: boolean }; } /** * Whether the keyboard is a touch one, where Enter is a new line and the * arrow button sends. * * @returns {boolean} True on a coarse pointer. */ export const isTouchKeyboard = (): boolean => typeof window !== 'undefined' && typeof window.matchMedia === 'function' && window.matchMedia('(pointer: coarse)').matches; /** * Whether Enter sends the question: Shift+Enter is a new line, and while an * input method is composing a word Enter picks the candidate (a merchant * typing with diacritics or in Japanese sent half a word otherwise). * * @param {ComposerKey} event - The key event. * @param {boolean} touch - The keyboard is a touch one. * @returns {boolean} True when the key sends. */ export const sendsOnEnter = (event: ComposerKey, touch: boolean): boolean => { if (event.key !== 'Enter' || event.shiftKey || touch) return false; return !(event.nativeEvent?.isComposing || event.keyCode === 229); };