robotjs: typeString() and keyTap() slow even with 1ms delay

robot.setKeyboardDelay(1); robot.typeString('*cough');

is much slower than having *cough set in the clipboard and doing

robot.keyToggle('control','down') robot.keyTap('v');

I think it could be the blocking nature of the module, and the fact that my PC is very slow.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 14
  • Comments: 28 (4 by maintainers)

Commits related to this issue

Most upvoted comments

https://github.com/octalmage/robotjs/blob/master/src/keypress.c#L17

#if defined(IS_WINDOWS)
	#define WIN32_KEY_EVENT_WAIT(key, flags) \
		(win32KeyEvent(key, flags), Sleep(DEADBEEF_RANDRANGE(63, 125)))
#elif defined(USE_X11)
	#define X_KEY_EVENT(display, key, is_press) \
		(XTestFakeKeyEvent(display, \
		                   XKeysymToKeycode(display, key), \
		                   is_press, CurrentTime), \
		 XSync(display, false))
	#define X_KEY_EVENT_WAIT(display, key, is_press) \
		(X_KEY_EVENT(display, key, is_press), \
		 microsleep(DEADBEEF_UNIFORM(62.5, 125.0)))
#endif

tapKey uses toggleKeyCode which calls WIN32_KEY_EVENT_WAIT once for a key and twice for a key with modifiers and therefore it can generate an additional delay of max 250ms. i dunno why there is a extra sleep call. i forked the repository and removed it and it works fine. < 2ms per call instead of 300ms +


// 输入字符
async function string(str) {
  await copyString(str);
  await tap('v', 'control');
  await sleep(20);
}

Note to others with this issue, installing directly from git seems to work:

npm install git+https://github.com/octalmage/robotjs.git

(This is undesirable because, in general, the git source is not necessarily the same as the npm package contents. Also because it doesn’t specify a version constraint. But it’s better than nothing and in this case seems to work …)

@fttx nope. i searched for weeks until i swapped to this package https://nutjs.dev/

The temporary fix i used was copy text and paste.

  • Anybody know what the MIT-MAGIC-COOKIE-1 thing is?
  • The title should be changed to “Linux/Windows”… Im having the issue in Ubuntu LTS
  • The master branch silently breaks typing on linux all together! 😦
  • #530 should be closed. duplicate
  • #559 should be closed as it doesn’t address the root cause. That said, removing deadbeef is a quick fix for linux… it does break the sleep option and makes it type super fast.

It uses X_KEY_EVENT_WAIT on linux and therefore the sleep is the same as on windows.

@lucasguaru You could always try to patch it with patch-package if you need to have it fixed locally.

@ricardopolo sometimes I want to hide the fact that I’m using a bot. Typing impossibly fast is a dead giveaway. That said, typeString isn’t designed to be slow… typeStringDelayed is. Everything looks great with #560