# IME-safe keyboard submission

An input method editor can use Enter to accept a candidate. A text input that also sends on Enter needs to leave that keystroke available to the editor.

```tsx
'use client'

import { formatSendShortcutDisplay, shouldSubmit } from '@ancrel/workspace/keyboard'

export default function App() {
  return (
    <form onSubmit={event => {
      event.preventDefault()
      const form = event.currentTarget
      const result = form.elements.namedItem('sent') as HTMLOutputElement
      result.value = String(new FormData(form).get('message'))
    }}>
      <label>
        Message
        <textarea name="message" onKeyDown={event => {
          if (shouldSubmit(event, 'enter')) {
            event.preventDefault()
            event.currentTarget.form?.requestSubmit()
          }
        }} />
      </label>
      <button type="submit">Send {formatSendShortcutDisplay('enter')}</button>
      <output name="sent" aria-label="Last sent message" />
    </form>
  )
}
```

Expected:

### Send a message

Type a draft in Message. Enter during IME candidate selection confirms the candidate and leaves Last sent message untouched.
Shift+Enter inserts a newline. An ordinary Enter or a click on Send copies the current draft to Last sent message.

## Attach the React handlers

Call `useImeSafeSubmit(mode, onSubmit)` from a component and spread the returned handlers onto the text input. The hook tracks composition start and end, handles browsers that report the confirming Enter after composition ends, and prevents the default only on submission. Keep draft validation and sending in the callback.

In `enter` mode, plain Enter submits. In `mod-enter` mode, Command+Enter submits on Apple platforms and Ctrl+Enter submits elsewhere. Shift+Enter remains available for a newline in both modes. Use `formatSendShortcutDisplay` for the corresponding key hint.

## Work with DOM events

Import `shouldSubmit` from `@ancrel/workspace/keyboard` and pass a DOM keyboard event or an event-shaped object. It checks both wrapper and native composition flags, the legacy 229 code and the Process key. Use `guardImeEnter(handler)` when only composing Enter should be withheld from an existing handler. Other keys pass through.

For deterministic shortcut labels in server-rendered content, pass the same `isMac` option on the server and during hydration. Automatic detection reads client hints, platform, then user agent. An environment without a navigator uses the non-Mac shortcut.
