Posted April 21, 20169 yr Hello, when i try to use keybinds that are bound to buttons on my mouse forge doesnt always execute the code. The strange thing is that it works sometimes but i really have to "spam" the button to make it work. Also vanilla keybinds work with my mouse buttons, eg my sprint button is bound to "Button 5" and it works perfectly fine. NOTE: This is not about setting the standard key of a keybinding to a mouse button, in my case its "Button 4" with id "-97". It also doesnt work with "Button 3" (which is middle mouse click). Id appreciate it if you tell me if thats a common problem or somethings wrong with my code. My main file: public class Mymod { public static KeyBinding toggleTest = new KeyBinding("Toggle Test", -97, "Mymod"); public void preInit(FMLPreInitializationEvent event){ } @EventHandler public void init(FMLInitializationEvent event){ ClientRegistry.registerKeyBinding(toggleTest); FMLCommonHandler.instance().bus().register(new KeyHandler()); } public void postInit(FMLPostInitializationEvent event){ } } KeyHandler: public class KeyHandler { static boolean tTest; @SubscribeEvent public void onKeyInputEvent(KeyInputEvent event){ if(Antipathy.toggleTest.isKeyDown()){ if(tAutohit){ tAutohit = false; Chat.printMsg("Disabled Test"); //this solely outputs a chatmessage, nothing fancy } else{ tAutohit = true; Chat.printMsg("Enabled Test"); } } } }
April 22, 20169 yr From my experience, KeyInputEvent does not fire at all for mouse buttons, so you have to also listen to MouseEvent (in which the real key code is equal to event.button - 100) and, if that key code is one of your key bindings, do your thing. An alternative is to not use either of those events but instead subscribe to the ClientTick event and iterate through all of your key bindings checking if any are pressed / released and handling them from there. This is the option most often advocated. http://i.imgur.com/NdrFdld.png[/img]
April 22, 20169 yr Author From my experience, KeyInputEvent does not fire at all for mouse buttons, so you have to also listen to MouseEvent (in which the real key code is equal to event.button - 100) and, if that key code is one of your key bindings, do your thing. An alternative is to not use either of those events but instead subscribe to the ClientTick event and iterate through all of your key bindings checking if any are pressed / released and handling them from there. This is the option most often advocated. thanks for your answer, the weird thing is that sometimes it triggered even though it shouldnt (according to you). I will check out the solutions you provided, but is there a reason (eg performance) why clienttick should be preferred? Is this how youre supposed to do it? I assume i only need my keybinds now, but not the keyhandler right? @SubscribeEvent public void onClientTickEvent(ClientTickEvent event){ boolean mousepressed = Mouse.isButtonDown(-97); boolean keyboardpressed = Keyboard.isKeyDown(Keyboard.KEY_P); }
April 22, 20169 yr Author hmmm i still cant get it to work completely. This code works when the keybind for toggleTest is set to a key but not when set to a mouse button: public class Clienttick { public static boolean tTest; @SubscribeEvent public void onClientTickEvent(ClientTickEvent event){ if(Mymod.toggleTest.getKeyCode() < -85){ //check if keybind is set to a mouse button if(Mouse.isButtonDown(Mymod.toggleTest.getKeyCode())){ if(tTest){ Chat.printMsg("disabled"); tTest = false; } else{ Chat.printMsg("enabled"); tTest = true; } } } else{ if(Keyboard.isKeyDown(Mymod.toggleTest.getKeyCode())){ if(tTest){ Chat.printMsg("disabled"); tTest = false; } else{ Chat.printMsg("enabled"); tTest = true; } } } } }
April 22, 20169 yr You shouldn't be checking Mouse at all, just use your KeyBinding. The KeyBinding class doesn't care what button it is bound to, as long as the button is pressed, the KeyBinding will tell you that. if (Mymod.toggleTest.getKeyIsPressed()) { // might be #getIsKeyPressed or something, don't recall exactly // key binding was pressed, whether assigned to Keyboard, Mouse, GamePad, whatever } http://i.imgur.com/NdrFdld.png[/img]
April 23, 20169 yr Author Thanks, that worked! I guess the tutorial i watched wasnt very good. Thank you for the help.
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.