Robot / robot-js

Native system automation for node.js
http://getrobot.net
zlib License
196 stars 23 forks source link

keyboard not working in games #62

Open philipfeldmann opened 6 years ago

philipfeldmann commented 6 years ago

Hey there. Dispatching keys is not working in games for me. keyboard.click(robot.KEY_Q); for example is not doing anything when using it in a game window, it works when trying to dispatch it to a text-field though (even works with the ingame chat of the game).

Any suggestion or a fix?

Suprisingly, I've done something similar before with Java's Robot class and it worked just fine.

dkrutsko commented 6 years ago

What game is this? Does it work with the Java Robot class in this game?

philipfeldmann commented 6 years ago

League of Legends, running in borderless mode. Yes it does work with Java Robot, which is really surprising to me. Also surprising that, when opening chat, the key will get dispatched to the ingame-chat correctly. It's just not working for invoking ingame commands.

(Also clicking with the mouse works perfectly fine)

dkrutsko commented 6 years ago

Interesting, the keypress logic is the same as Java, so you can almost rule out in-game protections. would something like keyboard.click ('q') work. Perhaps you need shift? In which case it'll be keyboard.click ('+q').

philipfeldmann commented 6 years ago

I pretty much copied from the documentation but just in case, I tried the following: (I imported as Robot so it's capitalized)

import Robot from 'robot-js';
// mouse stuff works even in game
    const mouse = Robot.Mouse();
    const pos = res[0];

    Robot.Mouse.setPos(pos.x, pos.y);
// this works in an editor for example but does nothing in the game
    const keyboard = Robot.Keyboard();
    keyboard.click('q')
    keyboard.click(Robot.KEY_Q)
    keyboard.press(Robot.KEY_Q)
    keyboard.release(Robot.KEY_Q)

Like I said it's working for the chat but unfortunately that's not really helpful.

dkrutsko commented 6 years ago

I remember looking at a game a while back where if the window was not in focus, it would ignore all input, this doesn't apply in this case. It also very foolishly ignored injected input which prevented certain hardware from working, so they had to patch it.

I wonder if anybody else has ran into this problem, perhaps it has some special logic to ignore node.exe specifically, I wouldn't put it past them. To properly test it, I'd build an application using the C++ version of robot and see if it works. I never played League but maybe it's something I'll be able to install.

philipfeldmann commented 6 years ago

Well for LoL, when the game is not in focus, even when in the front, it will not accept any keyboard input, not even from the keyboard. But I've made sure it's focused and regular keyboard input, same as the java robot input works. Unfortunately I don't really have a good idea about c++ but if I find the time I'll see if I can compile a small demo to check if it works.

Anyway, thanks for your effort.

dkrutsko commented 6 years ago

Could you please also tell me what OS you're using?

philipfeldmann commented 6 years ago

Sure. Windows 10 Professional and Node 10.6. Could it probably have something to do with LoL's client (not the game though) being an electron app? Some collision or something.

Laureckis commented 5 years ago

Any news on this?

I'm working on a game controller, and planned to use this for programmable macros, but some games ignore the keyboard commands (as above, it works in chat input field).

Doesn't work on Guild Wars 2, but does work fine in Rimworld.

Using something like Joy2Key (which seems C++ based, but doesn't offer a library) works ok in games.

Windows 10, Node 10.

p120ph37 commented 4 years ago

I had some issues with keypresses too. See my PR here: https://github.com/Robot/robot/pull/116 You can also use my fork, which includes the patch by setting your dependency to "robot-js": "github:p120ph37/robot-js#prebuilt"