Jump to content

Simulating keyboard and mouse input


Recommended Posts

I am trying to simulate keyboard and mouse input for a neural network but I can't figure out how to actually get the keys to be pressed. I put the following code into a separate class but when I call the jump method nothing happens.

public static void jump() {
  Minecraft mc = Minecraft.getInstance();
  KeyBinding.setKeyBindState(mc.gameSettings.keyBindJump.getKey(), true);

The method is indeed being called but the jump keybind isn't changing whatsoever.

How do I go about simulating these inputs?

Link to comment
Share on other sites

Looking at both KeyboardListener#setupCallbacks and MouseHelper#registerCallbacks, I can see that I need to use the following:

// for the keyboard
this.onKeyEvent(windowPointer, key, scanCode, action, modifiers);

// for the mouse
this.mouseButtonCallback(handle1, button, action, modifiers);

However I am unsure as to how to get the windowPointer and the handle1. From what I understand, InputMappings#setKeyCallbacks gives the necessary parameters, but calling this again would mean that I would need my own onKeyEvent and mouseButtonCallback methods to handle the input.

Do I need to call InputMappings#setKeyCallbacks again or is there a nice way to get the windowPointer and the other parameters?

Edited by somewhatOfACoder
Link to comment
Share on other sites

Ok so I changed the method I made to be the following:

public static void jump() {
		Minecraft mc = Minecraft.getInstance();
		int jumpKeyCode = mc.gameSettings.keyBindJump.getKey().getKeyCode();
		int scanCode = jumpKeyCode;
		int action = 1;
		int modifiers = 0;
		mc.keyboardListener.onKeyEvent(mc.getMainWindow().getHandle(), jumpKeyCode, scanCode, action, modifiers);

However it still doesn't work. The keyCode is fine, but I am not sure about how to get the scanCode. I had a look at KeyboardListener#onKeyEvent and it seems that if the action is 1 then the key will be pressed. I am also not sure about the modifiers because I can't find where it is used.

Link to comment
Share on other sites

Ok I am now stumped. I read the input guide and made a callback method for the keyboard which allowed me to get the keycode, scancode, action and modifier of whatever key I typed. However after using these values as parameters for KeyboardListener#onKeyEvent, nothing happened. For example, the space bar has a keycode of 32, a scancode of 57, an action of 1 when pressed and no modifiers. When I put these values directly into KeyboardListener#onKeyEvent nothing happened. Help?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

By using this site, you agree to our Terms of Use.