Jump to content

[1.15.2] Open custom GUI from keybind


Recommended Posts

public class KeyInputHandler {
    private static final Logger LOGGER = LogManager.getLogger();

    public void onKeyInput(InputEvent.KeyInputEvent event)
        PlayerEntity player = Minecraft.getInstance().player;
        if (Keybinds.test_item_gui_key.isPressed() && player.getHeldItemMainhand().getItem() instanceof ItemTestItem){
            LOGGER.info("KEY PRESSED");


How can I open my ItemTestItem's GUI when I press the C key?
Right Now I am opening it from NetworkHooks in my ItemTestItem class.

public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand handIn) {
    ItemStack stack = player.getHeldItem(handIn).getStack();
    if (!world.isRemote){
        if (player.isSneaking()){
            INamedContainerProvider containerProviderTestItem = new ContainerProviderTestItem(this, stack);
            final int NUMBER_OF_SLOTS = 8;
            NetworkHooks.openGui((ServerPlayerEntity) player, containerProviderTestItem, (packetBuffer)-> packetBuffer.writeInt(NUMBER_OF_SLOTS);});
            return ActionResult.resultSuccess(stack);

Here's the git repo if needed: https://github.com/NindyBun/TestMod/blob/master/src/main/java/com/nindybun/miningtool/KeyInputHandler.java
Apologies in advance if my code is crude.

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.