Jump to content

Recommended Posts

Posted

Hello,

 

I have implemented keybindings for my mod like this:

public void clientSetup(final FMLClientSetupEvent event) {
	int keyCode = 297;
	String description = "description";
	keybinding = new KeyBinding(description, KeyConflictContext.UNIVERSAL, KeyModifier.NONE, InputMappings.Type.KEYSYM, keyCode, "category");
	ClientRegistry.registerKeyBinding(keybinding);
}

@SubscribeEvent
public void keyPressed(InputEvent.KeyInputEvent event) {
	if (keybinding.isPressed()) {
    	// do something
	}
}

 

I first register the keybinding in the FMLClientSetupEvent and then I use the isPressed method inside of a KeyInputEvent to find out whether the keybinding has been pressed.

This works great in most cases.

 

The only problem is, that it does not work in ChestScreens.

Does anybody know why that could be the case?

 

Henne

Posted

IIRC key bindings are not fired when inside a Screen.

You have to subscribe to GuiScreenEvent.KeyboardKeyEvent or its subclasses to handle key presses inside a Screen.

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Posted

I tried all the GuiScreenEvent.Keyboard* classes.

Nothing makes the keybinding.isPressed method return true.

They are definitely being called, but the keybinding is not being triggered.

The inventory screen for example worked right away, but as I said, the ChestScreen is giving me some trouble.

Posted (edited)
25 minutes ago, henne90gen said:

I tried all the GuiScreenEvent.Keyboard* classes.

Nothing makes the keybinding.isPressed method return true.

They are definitely being called, but the keybinding is not being triggered.

The inventory screen for example worked right away, but as I said, the ChestScreen is giving me some trouble.

That is expected, as key binds are not triggered inside screens.

4 hours ago, DavidM said:

IIRC key bindings are not fired when inside a Screen.

 

You will need to check KeyboardKeyEvent#getKeyCode manually in the event.

Edited by DavidM

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

  • 2 months later...
Posted

Disclaimer: I am not an expierenced forge dev, this is just what i found after digging through a bunch of code (aka Minecraft internals).

Okay, this is just for future reference, if someone else finds this thread and tries implementing it themselves. For singular events (like an inventory sort) i think subscribing to a `GuiScreenEvent.KeyboardKeyEvent` or even just as `KeyInputEvent` and then checking for keycode and modifiers might be best, but for toggles (like holding a key to show a different UI screen) i found using InputMappings.isKeyDown better.

 

private boolean isKeyBindDown(KeyBinding keyBinding) {
    if (keyBinding.isKeyDown()) return true;
    InputMappings.Input key = keyBinding.getKey();
    int keyCode = key.getKeyCode();
    // GL window handle
    long handle = Minecraft.getInstance().getMainWindow().getHandle();
    return InputMappings.isKeyDown(handle, keyCode) && keyBinding.getKeyModifier().isActive(keyBinding.getKeyConflictContext());
}

 

`InputMappings` is internally used by `KeyModifier#isActive` as well, so im fairly confident that this is at least a somewhat intended path. This should only work with Keyboard Keybinds and not Mousebindings, but i haven't found a way for that yet, and honestly even finding this took way to long.

 

Short addendum after i already wrote most of this: `KeyModifier#isActive` doesn't actually check the passed `IKeyConflictContext` so one should maybe check that it is active, but i think, since this mostly concerns GUIs that should actually be already checked.

 

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.

Guest
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.

Announcements



×
×
  • Create New...

Important Information

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