ActionCallback (final Robot robot, final String text) { final ActionCallback result = new ActionCallback(); Runnable typeRunnable = () -> { for (int i = 0; i < text.length(); i++) { final char each = text.charAt(i); if ('\\' == each && i + 1 < text.length()) { final char next = text.charAt(i + 1); boolean processed = true; switch (next) { case 'n', 'r' -> type(robot, KeyEvent.VK_ENTER, 0); case 't' -> type(robot, KeyEvent.VK_TAB, 0); default -> processed = false; } if (processed) { i++; continue; } } type(robot, get(each)); } result.setDone(); }; if (SwingUtilities.isEventDispatchThread()) { ApplicationManager.getApplication().executeOnPooledThread(typeRunnable); } else { typeRunnable.run(); } return result; }