4

I am trying to send keyboard inputs to Minecraft to move the player, however when I try using SendKeys.SendWait("W"); nothing happens. If I open the chat in Minecraft it types "W" in chat, however outside of chat my inputs seem to be ignored. Thanks.

Edit: I have tried using SendInput as well as InputSimulator both having the same effect.

Jason A
  • 111
  • 1
  • 9
  • 1
    It's most likely because of DirectX it doesn't support passing window messages.. try this https://archive.codeplex.com/?p=inputsimulator or look into SendInput – SSpoke Mar 19 '19 at 20:34
  • Possibly related: https://stackoverflow.com/questions/33793410/simulating-input-key-pressed-hold-and-release . – chadnt Mar 19 '19 at 20:37
  • 1
    @SSpoke Thanks for the reply, but it seems I am having the same issue using InputSimulator.Keyboard.KeyPress – Jason A Mar 19 '19 at 21:15

2 Answers2

7

Basically Windows has three protection ring. By doing SendKeys you are sending a ring 3 command to the application. However DirectX only listens to ring 0 and ring 1 (possibly ring 2) commands to reduce the fraction delay caused by command passing through a driver to application.

So in order to make DirectX games react to the event you sent you must send it at driver level. You can simulate a ring 2 driver input by pinvoke WINDOWS api SendInput with scan code (don't use virtual code).

If scan code doesn't work then the game might be blocking ring 2 commands for anti-hacking purpose. In that case you would need to write a driver + a virtual hardware to send ring 1 commands directly. (do not try this if you are not experienced. a Blue screen of death or even corrupted system may result if a mistake is made)

Jlalonde
  • 483
  • 4
  • 13
Steve
  • 11,696
  • 7
  • 43
  • 81
6

I solved it by using InputSimulatorPlus

https://github.com/TChatzigiannakis/InputSimulatorPlus

InputSimulator s = new InputSimulator(); s.Keyboard.KeyDown(VirtualKeyCode.VK_W);

this just runs forward, to stop use s.Keyboard.KeyUp(VirtualKeyCode.VK_W); you can also use s.Keyboard.KeyPress(VirtualKeyCode.VK_W); and this will just click the "W" key.

Jason A
  • 111
  • 1
  • 9
  • Works as a charm, including mouse events. There is some pretty insane stuff that you can do if you combine both. This is far more advanced than putting a brick on a space bar or building a Lego machine to do some primitive key presses / clicks. – Konstantin Konstantinov Nov 15 '20 at 23:57