Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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");
		}
	}

}
}

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.

  • 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);
}

 

 

  • 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;
			}
		}
	}
}
}

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
}

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

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.