1 Input Methods and Clipboard
dxtr edited this page 2026-10-14 03:57:21 +02:00

Input Methods and the Clipboard

Input methods

An input method composes text from what is typed, such as Chinese or Japanese: the user types, sees the text being composed, and chooses the result from the input method's candidate list. Windows' input methods work through imm32, which cl-win32 binds.

A window's input method is enabled when the window is made. Turn it off while none of the window's text fields has the keyboard focus, so that keys reach the window as they are typed, and on again when one has:

(win32:disable-input-method window)      ; keys come as they are typed
(win32:enable-input-method window)       ; the input method composes text
(win32:input-method-enabled-p window)    ; => T

Disabling drops any text being composed.

Drawing the composition yourself

By default the input method draws the text being composed in a window of its own. To draw it in place, in the text field:

  • In +wm-ime-setcontext+, clear +isc-showuicompositionwindow+ from lparam before the default handling, and answer +wm-ime-startcomposition+ with 0, so the input method draws no composition window of its own.
  • In +wm-ime-composition+, read input-method-composition, the text and the index of its caret, when lparam holds +gcs-compstr+, and input-method-result, the text chosen, when it holds +gcs-resultstr+. Answer the message with 0: otherwise Windows sends the result again as +wm-char+.
  • +wm-ime-endcomposition+ ends the composition.
  • set-input-method-caret tells the input method where the caret is, as a rectangle in client pixels: its candidate list goes beside it without covering it. input-method-caret returns the rectangle given last.
((= message win32:+wm-ime-setcontext+)
 (win32:default-window-procedure
  window message wparam (logandc2 lparam win32:+isc-showuicompositionwindow+)))
((= message win32:+wm-ime-startcomposition+) 0)
((= message win32:+wm-ime-composition+)
 (when (logtest lparam win32:+gcs-resultstr+)
   (insert-text (win32:input-method-result window)))
 (when (logtest lparam win32:+gcs-compstr+)
   (multiple-value-bind (text caret) (win32:input-method-composition window)
     (show-composition text caret)))
 0)

insert-text and show-composition stand for your own code. cancel-input-method-composition drops the text being composed, as when the text field loses the focus. Call these functions from the window's thread.

The clipboard

(setf (win32:clipboard-text window) "Copied")
(win32:clipboard-text window)          ; => "Copied"

clipboard-text returns the text on the clipboard, or nil if it holds none, and setf puts text there, in place of what it held. Both take a window: Windows makes it the clipboard's owner when text is put there.

  • Text is Windows' Unicode text (CF_UNICODETEXT). Windows has CR LF between lines; cl-win32 neither adds nor removes them.
  • While another program has the clipboard open, both try again for up to :timeout seconds, 1 by default, and then signal win32-error.
  • While the session is locked, Windows gives no program the clipboard, and both signal win32-error.
  • A window procedure of the owner that runs meanwhile has its errors signalled as usual.