Skip to main content
Version: Latest

Special Keys Guide for Brobot

Overviewโ€‹

Brobot uses SikuliX's key representation system for special keyboard keys. When automating keyboard input, special keys like ENTER, ESC, TAB, and function keys must be represented using their proper Unicode escape sequences or string representations.

Key Representationsโ€‹

Common Special Keysโ€‹

Key NameSikuliX ConstantUnicode/String ValueUsage Example
EnterKey.ENTER"\n"Confirm dialogs, submit forms
EscapeKey.ESC"\u001b"Close windows, cancel operations
TabKey.TAB"\t"Navigate between fields
SpaceKey.SPACE" "Space character
BackspaceKey.BACKSPACE"\b"Delete previous character
DeleteKey.DELETE"\u007f" or "\ue006"Delete next character
Key NameSikuliX ConstantUnicode ValueUsage Example
Up ArrowKey.UP"\ue000"Navigate up in lists
Right ArrowKey.RIGHT"\ue001"Move cursor right
Down ArrowKey.DOWN"\ue002"Navigate down in lists
Left ArrowKey.LEFT"\ue003"Move cursor left
Page UpKey.PAGE_UP"\ue004"Scroll up one page
Page DownKey.PAGE_DOWN"\ue005"Scroll down one page
HomeKey.HOME"\ue008"Go to beginning
EndKey.END"\ue007"Go to end
InsertKey.INSERT"\ue009"Toggle insert mode

Function Keysโ€‹

Key NameSikuliX ConstantUnicode Value
F1Key.F1"\ue011"
F2Key.F2"\ue012"
F3Key.F3"\ue013"
F4Key.F4"\ue014"
F5Key.F5"\ue015"
F6Key.F6"\ue016"
F7Key.F7"\ue017"
F8Key.F8"\ue018"
F9Key.F9"\ue019"
F10Key.F10"\ue01a"
F11Key.F11"\ue01b"
F12Key.F12"\ue01c"

Modifier Keysโ€‹

Key NameSikuliX ConstantUnicode ValuePlatform Notes
ShiftKey.SHIFT"\ue020"All platforms
ControlKey.CTRL"\ue021"Windows/Linux
AltKey.ALT"\ue022"All platforms
Command/MetaKey.CMD or Key.META"\ue023"macOS Command key
WindowsKey.WIN"\ue042"Windows key

Prerequisites and Setupโ€‹

Before using special keys in your automation, ensure you have the basic Brobot setup:

Required Importsโ€‹

// Core Brobot imports
import io.github.jspinak.brobot.action.Action;
import io.github.jspinak.brobot.action.basic.type.TypeOptions;
import io.github.jspinak.brobot.model.state.StateString;
import io.github.jspinak.brobot.model.buildWith.ObjectCollection;

// SikuliX imports
import org.sikuli.script.Key;

// Spring imports
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Action Injection Patternโ€‹

All Brobot automation requires the Action class to be injected:

@Component
public class MyAutomation {

@Autowired
private Action action;

// Your automation methods here
}

This pattern is required for all examples in this guide.

Usage in Brobotโ€‹

Use the SikuliX Key class constants which are more readable and maintainable:

import io.github.jspinak.brobot.action.Action;
import org.sikuli.script.Key;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class KeyboardExample {

@Autowired
private Action action;

public void pressSpecialKeys() {
// Simple form - recommended for single keys
action.type(Key.ENTER); // Press ENTER
action.type(Key.ESC); // Press ESC
action.type(Key.TAB); // Press TAB
action.type(Key.SPACE); // Press SPACE
}
}

// In StateString definitions
StateString enterKey = new StateString.Builder()
.setString(Key.ENTER) // Uses Key.ENTER constant
.setName("Enter Key")
.build();

StateString escapeKey = new StateString.Builder()
.setString(Key.ESC) // Uses Key.ESC constant
.setName("Escape Key")
.build();

Method 2: Direct Unicode String (Alternative)โ€‹

You can also use the Unicode escape sequence directly if you prefer not to import the SikuliX Key class:

import io.github.jspinak.brobot.model.state.StateString;
import io.github.jspinak.brobot.annotations.State;

// In a State class - define special keys as StateStrings
@State
public class AmountState {
private final StateString enter;
private final StateString escape;

public AmountState() {
// Enter key for confirming
enter = new StateString.Builder()
.setString("\n") // Unicode for ENTER
.setName("Enter Key")
.build();

// Escape key for closing
escape = new StateString.Builder()
.setString("\u001b") // Unicode for ESC
.setName("Escape Key")
.build();
}
}

Method 3: In Transition Classesโ€‹

Use special keys in your transition methods:

import io.github.jspinak.brobot.action.Action;
import io.github.jspinak.brobot.action.basic.type.TypeOptions;
import io.github.jspinak.brobot.annotations.TransitionSet;
import io.github.jspinak.brobot.annotations.OutgoingTransition;
import io.github.jspinak.brobot.model.buildWith.ObjectCollection;
import org.sikuli.script.Key;
import org.springframework.beans.factory.annotation.Autowired;

@TransitionSet(state = DialogState.class)
public class DialogTransitions {

@Autowired
private Action action;

@OutgoingTransition(activate = {MainScreenState.class})
public boolean closeDialog() {
TypeOptions options = new TypeOptions.Builder()
.withBeforeActionLog("Closing dialog with ESC key...")
.withSuccessLog("Dialog closed")
.withFailureLog("Failed to close dialog")
.build();

// Type the ESC key using perform() with options
return action.perform(options,
new ObjectCollection.Builder().withStrings(Key.ESC).build()
).isSuccess();
}

public boolean submitForm() {
// Simple form without options
return action.type(Key.ENTER).isSuccess();
}
}

Method 4: Combining with Regular Textโ€‹

You can combine special keys with regular text:

import io.github.jspinak.brobot.action.Action;
import org.sikuli.script.Key;
import org.springframework.beans.factory.annotation.Autowired;

@Component
public class FormFiller {

@Autowired
private Action action;

public void fillLoginForm() {
// Type text and press Tab to move to next field
action.type("username" + Key.TAB + "password");

// Type text and press Enter to submit
action.type("search term" + Key.ENTER);
}
}

Method 5: Using StateString for Reusable Keysโ€‹

Create reusable special key objects:

import io.github.jspinak.brobot.model.state.StateString;
import org.sikuli.script.Key;

public class CommonKeys {
public static final StateString ENTER = new StateString.Builder()
.setString(Key.ENTER)
.setName("Enter Key")
.build();

public static final StateString ESCAPE = new StateString.Builder()
.setString(Key.ESC)
.setName("Escape Key")
.build();

public static final StateString TAB = new StateString.Builder()
.setString(Key.TAB)
.setName("Tab Key")
.build();
}

// Usage
action.type(CommonKeys.ENTER);

Complete Working Exampleโ€‹

Here's a full, runnable example showing special keys in a complete Brobot component:

package com.example.automation;

import io.github.jspinak.brobot.action.Action;
import io.github.jspinak.brobot.action.basic.type.TypeOptions;
import io.github.jspinak.brobot.model.state.StateString;
import io.github.jspinak.brobot.model.buildWith.ObjectCollection;
import org.sikuli.script.Key;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DialogAutomation {

@Autowired
private Action action;

// Define reusable key constants
private final StateString ENTER_KEY = new StateString.Builder()
.setString(Key.ENTER)
.setName("Enter Key")
.build();

private final StateString ESC_KEY = new StateString.Builder()
.setString(Key.ESC)
.setName("Escape Key")
.build();

/**
* Submit form by pressing Enter
*/
public boolean submitForm() {
return action.type(ENTER_KEY).isSuccess();
}

/**
* Close dialog by pressing Escape
*/
public boolean closeDialog() {
return action.type(ESC_KEY).isSuccess();
}

/**
* Navigate form fields with Tab
*/
public void fillForm(String username, String password) {
action.type(username);
action.type(Key.TAB); // Move to next field
action.type(password);
action.type(Key.ENTER); // Submit
}

/**
* Type with timing control and logging
*/
public void typeWithOptions(String text) {
TypeOptions options = new TypeOptions.Builder()
.withBeforeActionLog("Typing: " + text)
.setPauseBeforeBegin(0.5)
.setPauseAfterEnd(1.0)
.setTypeDelay(0.1)
.build();

// Using perform() with ObjectCollection for options
action.perform(options,
new ObjectCollection.Builder().withStrings(text).build());
}
}

This example shows:

  • โœ… Proper Action injection with @Autowired
  • โœ… Correct StateString builder using setString()
  • โœ… Reusable key constants
  • โœ… Multiple usage patterns
  • โœ… Complete, compilable class
  • โœ… TypeOptions with logging and timing

Keyboard Shortcuts and Combinationsโ€‹

Using Modifier Keysโ€‹

For keyboard shortcuts with modifiers (Ctrl, Alt, Shift), Brobot provides several approaches:

Note: The Action class doesn't expose low-level keyDown/keyUp methods directly. For complex key combinations, use one of these patterns:

Method 1: SikuliX Key Modifiers (Simplest)

import org.sikuli.script.Key;
import org.sikuli.script.Region;

// Note: This requires direct SikuliX Region access
// Check current Brobot API for keyboard combination support
Region region = new Region(0, 0, 100, 100);
region.type("c", Key.CTRL); // Ctrl+C
region.type("v", Key.CTRL); // Ctrl+V

Method 2: Action Chains (For Advanced Scenarios)

For complex keyboard operations, consider using ConditionalActionChain or contact Brobot maintainers for the current keyboard shortcut API.

For current keyboard shortcut implementation, see:

Common Pitfalls and Solutionsโ€‹

Pitfall 1: Using String Literals Instead of Unicodeโ€‹

โŒ Wrong:

action.type("ESC", options);  // This types the letters E, S, C
action.type("ENTER", options); // This types the letters E, N, T, E, R

โœ… Correct:

action.type("\u001b", options); // ESC key
action.type("\n", options); // ENTER key

Pitfall 2: Platform Differencesโ€‹

Some keys behave differently across platforms:

  • Use Key.CMD on macOS instead of Key.CTRL for command shortcuts
  • Key.WIN is Windows-specific
  • Line endings: Windows uses \r\n, Unix/Mac use \n

Pitfall 3: Timing Issuesโ€‹

Special keys may need pauses for the application to process them:

import io.github.jspinak.brobot.action.basic.type.TypeOptions;
import io.github.jspinak.brobot.model.buildWith.ObjectCollection;
import org.sikuli.script.Key;

TypeOptions options = new TypeOptions.Builder()
.setPauseBeforeBegin(0.5) // Wait before typing
.setPauseAfterEnd(1.0) // Wait after typing
.setTypeDelay(0.1) // Delay between keystrokes
.build();

// Apply timing to action
action.perform(options,
new ObjectCollection.Builder().withStrings(Key.ENTER).build());

Testing Special Keysโ€‹

When testing automation that uses special keys:

  1. Mock Mode: Special keys work in mock mode for testing
  2. Logging: Use verbose logging to verify correct key sequences
  3. Visual Feedback: Enable visual feedback to see what keys are being "pressed"
import io.github.jspinak.brobot.action.Action;
import io.github.jspinak.brobot.action.basic.type.TypeOptions;
import io.github.jspinak.brobot.model.buildWith.ObjectCollection;
import org.sikuli.script.Key;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Test
public void testSpecialKey() {
TypeOptions debugOptions = new TypeOptions.Builder()
.withBeforeActionLog("Pressing special key: ESC")
.withSuccessLog("Special key pressed successfully")
.withFailureLog("Failed to press special key")
.build();

ActionResult result = action.perform(debugOptions,
new ObjectCollection.Builder().withStrings(Key.ESC).build());

assertTrue(result.isSuccess());
}

For comprehensive testing strategies, see the Mock Mode Guide.

Best Practicesโ€‹

  1. Use Constants: Define special keys as constants or StateStrings for reusability
  2. Document Intent: Always comment what special key you're using and why
  3. Test Cross-Platform: If your automation runs on multiple OS, test special keys on each
  4. Handle Failures: Special keys may fail if the window loses focus
  5. Use Appropriate Delays: Some applications need time to process special keys

Reference Table - All Special Keysโ€‹

CategoryKeyUnicodeCommon Use
ControlEnter\nSubmit, confirm
Escape\u001bCancel, close
Tab\tNext field
Space Select, space
Backspace\bDelete back
Delete\u007fDelete forward
NavigationUp\ue000Move up
Down\ue002Move down
Left\ue003Move left
Right\ue001Move right
Home\ue008Start of line
End\ue007End of line
Page Up\ue004Previous page
Page Down\ue005Next page
FunctionF1-F12\ue011-\ue01cVarious
ModifierShift\ue020Uppercase, select
Ctrl\ue021Shortcuts
Alt\ue022Menu access
Cmd/Meta\ue023macOS commands

Getting Startedโ€‹

Core Conceptsโ€‹

Action Configurationโ€‹

Testingโ€‹

External Resourcesโ€‹