def write(text, delay=<NUM_LIT:0>, restore_state_after=True, exact=None):
if exact is None:<EOL><INDENT>exact = _platform.system() == '<STR_LIT>'<EOL><DEDENT>state = stash_state()<EOL>if exact:<EOL><INDENT>for letter in text:<EOL><INDENT>if letter in '<STR_LIT>':<EOL><INDENT>send(letter)<EOL><DEDENT>else:<EOL><INDENT>_os_keyboard.type_unicode(letter)<EOL><DEDENT>if delay: _time.sleep(delay)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for letter in text:<EOL><INDENT>try:<EOL><INDENT>entries = _os_keyboard.map_name(normalize_name(letter))<EOL>scan_code, modifiers = next(iter(entries))<EOL><DEDENT>except (KeyError, ValueError):<EOL><INDENT>_os_keyboard.type_unicode(letter)<EOL>continue<EOL><DEDENT>for modifier in modifiers:<EOL><INDENT>press(modifier)<EOL><DEDENT>_os_keyboard.press(scan_code)<EOL>_os_keyboard.release(scan_code)<EOL>for modifier in modifiers:<EOL><INDENT>release(modifier)<EOL><DEDENT>if delay:<EOL><INDENT>_time.sleep(delay)<EOL><DEDENT><DEDENT><DEDENT>if restore_state_after:<EOL><INDENT>restore_modifiers(state)<EOL><DEDENT>
Sends artificial keyboard events to the OS, simulating the typing of a given
text. Characters not available on the keyboard are typed as explicit unicode
characters using OS-specific functionality, such as alt+codepoint.

To ensure text integrity, all currently pressed keys are released before
the text is typed, and modifiers are restored afterwards.

- `delay` is the number of seconds to wait between keypresses, defaults to
no delay.
- `restore_state_after` can be used to restore the state of pressed keys
after the text is typed, i.e. presses the keys that were released at the
beginning. Defaults to True.
- `exact` forces typing all characters as explicit unicode (e.g.
alt+codepoint or special events). If None, uses platform-specific suggested
value.