# platform/desktop/textinput (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/platform/desktop/textinput/

Desktop text input. (winit 0.30 window events → engine text input queue, system clipboard)
- Covers both the role of the DOM <input> (set_use_dominput(true)) of the original UIInputField and that of document keydown (canvas mode).
  - KeyboardInput: editing keys (Backspace, Delete, arrows, Home, End, Enter, Tab), Ctrl/Cmd + A/C/V/X, characters of KeyEvent.text → text input queue.
  - Ime: Preedit becomes composition start / composition text / composition end, and Commit becomes committed text. (the Preedit cursor is a byte position, so it is converted to a character position)
  - Every frame reads the previous frame's field state, and turns IME on and positions the candidate window only when the focused field uses platform input (use_dominput).
    When focus moves to another field, or the field commits the composition text because it was pressed inside during composition, IME is turned off and on to cut the composition.
    (the field has already committed the cut composition text, so the same Commit sent again by the OS is discarded)
- The clipboard is read and written with arboard. (winit has no clipboard. On Linux the content only stays while a Clipboard object is alive, so one is held per thread)

Example: Write to the clipboard and read it back (not run since it changes the system clipboard)

```rust
use vanilla::platform;

platform::write_clipboard_text("vanilla");
assert_eq!(platform::read_clipboard_text().as_deref(), Some("vanilla"));
platform::read_clipboard_text_with_callback(Box::new(|text: String| {
	println!("{}", text);
}));
```

## Functions and constants

- `pub fn write_clipboard_text(text: &str)` (method, since 0.3.2): Writes text to the system clipboard.
  - Skips if the clipboard cannot be opened, and logs to standard error if it cannot write.
  - `text` &str: Text to write.
- `pub fn read_clipboard_text() -> Option<String>` (method, since 0.3.2): Reads text from the system clipboard. (None if there is no text or it cannot be read)
  - returns Option<String>: Clipboard text. None if there is no text or the clipboard cannot be opened or read.
- `pub fn read_clipboard_text_with_callback(callback: Box<dyn FnOnce(String)>)` (method, since 0.4.0): Reads the system clipboard text and passes it to a callback. (in place of the original readClipboard, desktop reads it on the spot and calls right away. Empty text if it cannot be read)
  - `callback` Box<dyn FnOnce(String)>: Function to receive the text read. (called once before returning)
- `pub fn read_optional_clipboard_text_with_callback(callback: Box<dyn FnOnce(Option<String>)>)` (method, since 0.4.0): Reads the system clipboard text and passes it to a callback. (in place of the original engine.readClipboardText, desktop reads it on the spot and calls right away. None if it cannot be read)
  - `callback` Box<dyn FnOnce(Option<String>)>: Function to receive the text read. (called once before returning, None if it cannot be read)
