Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Send key inputs to Minecraft to move the player

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.

like image 682
Jason A Avatar asked Dec 17 '22 18:12

Jason A


2 Answers

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)

like image 75
Steve Avatar answered Jan 01 '23 23:01

Steve


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.

like image 44
Jason A Avatar answered Jan 01 '23 21:01

Jason A