Posted August 31, 201312 yr Hello, I am trying to make a simple Keybind toggle that toggles a HUD gui. But I cannot seem to figure out how to make the keyUp and keyDown methods toggle. All the GUI is doing is running a loop checking to see if keyPressed is true. If keyPressed is true then it doesnt show it. But I want it to toggle, so I don't want to press it and then release it and the GUI toggle on and then off. I want it to be like the F3 menu ( on how it toggles ). Could any of you experienced folk help me a little? package ws.mods.wsessentials; import java.io.File; import java.io.FileWriter; import java.io.PrintWriter; import java.util.EnumSet; import net.minecraft.client.Minecraft; import net.minecraft.client.settings.KeyBinding; import net.minecraftforge.common.Configuration; import cpw.mods.fml.client.registry.KeyBindingRegistry.KeyHandler; import cpw.mods.fml.common.TickType; public class KeyBindings extends KeyHandler { public wsEssentials ws; private EnumSet tickTypes = EnumSet.of(TickType.CLIENT); public static boolean keyPressed = false; public static boolean keyReleased = true; public KeyBindings(KeyBinding[] keyBindings, boolean[] repeatings){ super(keyBindings, repeatings); } @Override public String getLabel(){ return "F"; } @Override public void keyDown(EnumSet<TickType> types, KeyBinding kb, boolean tickEnd, boolean isRepeat){ if (keyPressed == false){ keyPressed = true; } else { keyPressed = false; } keyReleased = false; } @Override public void keyUp(EnumSet<TickType> types, KeyBinding kb, boolean tickEnd){ if (keyPressed == true){ keyReleased = true; } else { keyReleased = true; } } @Override public EnumSet<TickType> ticks(){ return tickTypes; } }
August 31, 201312 yr Don't use those booleans, and disable the repeatings for your key. Use the keyDown method only. In it, if(tickend) { if(hasOpenedGui) { //close gui hasOpenedGui=false; } else { //open gui hasOpenedGui=true; } } That is, if you have an HUD. If it is a gui (extends GuiScreen), you can use Minecraft.getMinecraft().currentScreen to check.
August 31, 201312 yr Author Don't use those booleans, and disable the repeatings for your key. Use the keyDown method only. In it, if(tickend) { if(hasOpenedGui) { //close gui hasOpenedGui=false; } else { //open gui hasOpenedGui=true; } } That is, if you have an HUD. If it is a gui (extends GuiScreen), you can use Minecraft.getMinecraft().currentScreen to check. Wow, I don't know why I didn't think of that before. Thank you very much! The GUI is just a simple GUI that extends Gui, but this seems to work brilliantly for it. Thank you.
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.