Core Concepts
Keyboard Input
Simulate keyboard input to type text, press keys, and trigger keyboard shortcuts in your automation scripts.
Try it out
Watch as text is typed character by character
import { keyboard } from "@nut-tree/nut-js";
// Type a string character by character
await keyboard.type("Hello, nut.js!");What you can do
Type Text
Type strings of text character by character
keyboard.type("Hello World")Press Keys
Press and release individual keys or key combinations
keyboard.type(Key.Enter)Keyboard Shortcuts
Trigger shortcuts like Ctrl+C, Cmd+V, Alt+Tab
keyboard.type(Key.LeftControl, Key.C)Hold Keys
Press and hold keys for drag operations or gaming
keyboard.pressKey(Key.LeftShift)Configurable Speed
Control typing speed with configurable delays
keyboard.config.autoDelayMs = 100Cross-Platform
Works on Windows, macOS, and Linux
// Same API everywhereQuick Reference
type
keyboard.type(...input: (string | Key)[])Type text or press keys. Accepts strings and Key enum values.
pressKey
keyboard.pressKey(...keys: Key[])Press and hold one or more keys. Must be released with releaseKey.
releaseKey
keyboard.releaseKey(...keys: Key[])Release one or more keys that were previously pressed.
Typing Text
The type method is the primary way to simulate keyboard input. It can type strings of text or press individual keys.
Typing Strings
Type a string of text character by character:
import { keyboard } from "@nut-tree/nut-js";
// Type a simple string
await keyboard.type("Hello, World!");
// Type unicode characters
await keyboard.type("nut.js rocks! 🚀✨");Typing Keys
Press individual keys using the Key enum:
import { keyboard, Key } from "@nut-tree/nut-js";
// Press Enter
await keyboard.type(Key.Enter);
// Press Escape
await keyboard.type(Key.Escape);
// Press Tab
await keyboard.type(Key.Tab);
// Press arrow keys
await keyboard.type(Key.Up);
await keyboard.type(Key.Down);
await keyboard.type(Key.Left);
await keyboard.type(Key.Right);Keyboard Shortcuts
Trigger keyboard shortcuts by passing multiple keys to type. The keys are pressed simultaneously, simulating a keyboard shortcut.
import { keyboard, Key } from "@nut-tree/nut-js";
// Copy (Ctrl+C / Cmd+C)
await keyboard.type(Key.LeftControl, Key.C); // Windows/Linux
await keyboard.type(Key.LeftSuper, Key.C); // macOS
// Paste (Ctrl+V / Cmd+V)
await keyboard.type(Key.LeftControl, Key.V); // Windows/Linux
await keyboard.type(Key.LeftSuper, Key.V); // macOS
// Select All (Ctrl+A / Cmd+A)
await keyboard.type(Key.LeftControl, Key.A);
// Undo (Ctrl+Z / Cmd+Z)
await keyboard.type(Key.LeftControl, Key.Z);
// Save (Ctrl+S / Cmd+S)
await keyboard.type(Key.LeftControl, Key.S);
// Open Spotlight on macOS
await keyboard.type(Key.LeftSuper, Key.Space);
// Alt+Tab to switch windows
await keyboard.type(Key.LeftAlt, Key.Tab);
// Alt+F4 to close window (Windows)
await keyboard.type(Key.LeftAlt, Key.F4);Platform-Specific Shortcuts
Key.LeftSuper for the Command key on macOS or the Windows key on Windows. This allows you to write cross-platform scripts that work on all platforms.Press and Hold Keys
For more control over key states, use pressKey and releaseKey. This is useful for holding modifier keys during mouse operations or gaming scenarios.
import { keyboard, Key } from "@nut-tree/nut-js";
// Hold Shift while clicking (for multi-select)
await keyboard.pressKey(Key.LeftShift);
// ... perform mouse clicks here ...
await keyboard.releaseKey(Key.LeftShift);
// Hold Ctrl while clicking (for non-contiguous select)
await keyboard.pressKey(Key.LeftControl);
// ... perform mouse clicks here ...
await keyboard.releaseKey(Key.LeftControl);
// Press and release Alt+F4
await keyboard.pressKey(Key.LeftAlt, Key.F4);
await keyboard.releaseKey(Key.LeftAlt, Key.F4);Holding Keys
pressKey and releaseKey with any Key, it actually only makes sense to use them with modifier keys like Key.LeftShift. There's no automatic key repeat when pressing and holding e.g. Key.A. If you want to simulate key repeat, you can use keyboard.type with setInterval instead.Always Release Keys
pressKey. Unreleased keys will remain "stuck" until released, which can cause unexpected behavior in your system.Configuration
Configure keyboard behavior using the keyboard.config object.
import { keyboard } from "@nut-tree/nut-js";
// Set delay between keystrokes (in milliseconds)
keyboard.config.autoDelayMs = 100;
// Type slowly (100ms between each character)
await keyboard.type("Typing slowly...");
// Type faster
keyboard.config.autoDelayMs = 10;
await keyboard.type("Typing quickly!");
// No delay (instant)
keyboard.config.autoDelayMs = 0;
await keyboard.type("Instant typing");Default Delay
autoDelayMs value provides a balance between speed and reliability. Some applications may miss keystrokes if typing is too fast.Key Reference
The Key enum provides constants for all keyboard keys. Here are the most commonly used keys:
Modifier Keys
// Shift
Key.LeftShift, Key.RightShift
// Control
Key.LeftControl, Key.RightControl
// Alt / Option
Key.LeftAlt, Key.RightAlt
// Super (Windows key / Command key)
Key.LeftSuper, Key.RightSuperSpecial Keys
// Navigation
Key.Enter, Key.Tab, Key.Escape, Key.Space
Key.Backspace, Key.Delete
Key.Home, Key.End, Key.PageUp, Key.PageDown
// Arrow Keys
Key.Up, Key.Down, Key.Left, Key.Right
// Function Keys
Key.F1, Key.F2, Key.F3, Key.F4, Key.F5, Key.F6
Key.F7, Key.F8, Key.F9, Key.F10, Key.F11, Key.F12Real-World Examples
Here are practical examples showing how to use keyboard input in real automation scenarios.
Open Application via Spotlight (macOS)
Use Spotlight to quickly open an application
Scenario: You want to open Calculator on macOS without knowing its exact location.
import { keyboard, Key } from "@nut-tree/nut-js";
// Open Spotlight
await keyboard.type(Key.LeftSuper, Key.Space);
// Wait for Spotlight to appear
await new Promise(r => setTimeout(r, 500));
// Type application name
await keyboard.type("calculator");
// Press Enter to open
await keyboard.type(Key.Enter);Fill a Form
Navigate and fill form fields using Tab and typing
Scenario: You need to fill out a form with multiple fields.
import { keyboard, Key } from "@nut-tree/nut-js";
// Assuming first field is focused
await keyboard.type("John Doe");
// Tab to next field (email)
await keyboard.type(Key.Tab);
await keyboard.type("john@example.com");
// Tab to next field (phone)
await keyboard.type(Key.Tab);
await keyboard.type("555-1234");
// Tab to submit button and press Enter
await keyboard.type(Key.Tab);
await keyboard.type(Key.Enter);Copy and Paste Text
Select all text, copy it, and paste elsewhere
Scenario: You want to copy content from one application and paste it into another.
import { keyboard, Key } from "@nut-tree/nut-js";
// Select all text
await keyboard.type(Key.LeftControl, Key.A);
// Copy selected text
await keyboard.type(Key.LeftControl, Key.C);
// Switch to another window
await keyboard.type(Key.LeftAlt, Key.Tab);
await new Promise(r => setTimeout(r, 300));
// Paste text
await keyboard.type(Key.LeftControl, Key.V);Hold Shift for Multi-Select
Select multiple items by holding Shift while clicking
Scenario: You want to select a range of files in a file manager.
import { keyboard, mouse, straightTo, Point, Key, Button } from "@nut-tree/nut-js";
// Click first item
await mouse.move(straightTo(new Point(200, 100)));
await mouse.click(Button.LEFT);
// Hold Shift and click last item to select range
await keyboard.pressKey(Key.LeftShift);
await mouse.move(straightTo(new Point(200, 300)));
await mouse.click(Button.LEFT);
await keyboard.releaseKey(Key.LeftShift);
// All items between first and last are now selectedImportant Notes
Concurrent Input Sequences
Best Practices
- Use appropriate delays between operations for reliability
- Always release keys that you press with
pressKey - Test your scripts on the target system to ensure compatibility
- Consider using image search to verify UI state before typing