简体   繁体   中英

Robot.js tap key non-synchronously

I'm working with the package robot.js and need to run lots of key tap commands (often over 50 a second) with my program.

So far it goes a bit like this:

...for in elements

setTimeout(() => {
    console.log(element.deltaTime) // Logs the time that the key should be pressed at (in ms)
    if(shifted(element.key)){
        robot.keyTap(element.key,"shift")
    }else{
        robot.keyTap(element.key)
    }
}, element.deltaTime);

That's not the exact program here because it would take too long to explain, but it gets most of the problem. Basically, I'm trying to run lots of key taps in a short amount of time.

The problem here is that robot.js takes its sweet time hitting the keys, and delays the execution of other keys until it's finished, leaving a really slow result. Commenting out the robot.keyTap() function runs the program much faster, but you know, doesn't tap the keys.

Any help is appreciated, I really want to get this working fast.

According to this github issue , this problem exhibits on Windows systems - calling .keyTap can result in a blocking delay of tens or hundreds of milliseconds.

While the fix has been merged into the master repo, the NPM version has not been updated and published, and so those using RobotJS will still encounter it for now - unless they install the fixed version manually by running:

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

Hopefully the above won't be needed indefinitely, and eventually a new version will be published, allowing a simple npm install robotjs to get around this problem in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM