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

First, how do you detect a player holding down a keybind, and constantly perform an action?

 

 

Second, my keybind is running all of its code twice.  I'm using KeyInputEvent, and its getPhase() returns an EventPriority (?!) so...how do I fix that?

 

 

Keybind

public class GatherManaKeybind extends KeyBinding
{
   public GatherManaKeybind()
   {
      super("key.gatherMana", Keyboard.KEY_G, "key.categories." + AAReference.MODID);
   }


   @SubscribeEvent(priority = EventPriority.HIGHEST)
   public void onKeyInput(InputEvent.KeyInputEvent event)
   {
      if(this.isPressed())
      {
         ~do it to it, but not twice~
      }
   }
}

 

 

ClientProxy

   @Override
   public void preInit(FMLPreInitializationEvent event)
   {
      super.preInit(event);
      GatherManaKeybind gatherKBD = new GatherManaKeybind();
      FMLCommonHandler.instance().bus().register(gatherKBD);
      ClientRegistry.registerKeyBinding(gatherKBD);
   }

  • Author

For holding, no, I want the player to be able to hold the key down and have the code constantly run.  So moving to a ClientTickEvent might fix that?  With KeyInputEvent, holding the key only made the code run once. (er, twice, just, it wasn't continuous)

 

(What is WITH this forum's formatting going wonky randomly? <__<)

 

Edit: I changed it to a ClientTickEvent, but it's still not detecting the key is being held down, is there a different method I have to check for in KeyBinding?

 

public class GatherManaKeybind extends KeyBinding
{
   public GatherManaKeybind()
   {
      super("key.gatherMana", Keyboard.KEY_G, "key.categories." + AAReference.MODID);
   }


   @SubscribeEvent()
   public void onKeyInput(TickEvent.ClientTickEvent event)
   {
      if(event.phase == TickEvent.Phase.END && this.isPressed())
      {
         ~snip~
      }
   }
}

  • Author

That does not appear to be in 1.7.10's KeyBinding...

 

 

Edit: KeyBinding#getIsKeyPressed works.

For holding, no, I want the player to be able to hold the key down and have the code constantly run.  So moving to a ClientTickEvent might fix that?  With KeyInputEvent, holding the key only made the code run once. (er, twice, just, it wasn't continuous)

 

(What is WITH this forum's formatting going wonky randomly? <__<)

 

Edit: I changed it to a ClientTickEvent, but it's still not detecting the key is being held down, is there a different method I have to check for in KeyBinding?

 

Here is just a bit of java info.

 

Java never "constantly" runs. Never. You can't program that (well you probably could but it's not how it works). WHat happens is you set the Boolean pressed to true, so it runs a class or etc. and then you check the Boolean throughout the code to make sure it is true, if it isn't, program the code to disregard the events that took place and to start over at Boolean pressed true when the keybind is pressed.

public class GatherManaKeybind extends KeyBinding
{
   public GatherManaKeybind()
   {
      super("key.gatherMana", Keyboard.KEY_G, "key.categories." + AAReference.MODID);
   }


   @SubscribeEvent()
   public void onKeyInput(TickEvent.ClientTickEvent event)
   {
      if(event.phase == TickEvent.Phase.END && this.isPressed())
      {
         ~snip~
      }
   }
}

For holding, no, I want the player to be able to hold the key down and have the code constantly run.  So moving to a ClientTickEvent might fix that?  With KeyInputEvent, holding the key only made the code run once. (er, twice, just, it wasn't continuous)

 

(What is WITH this forum's formatting going wonky randomly? <__<)

 

Edit: I changed it to a ClientTickEvent, but it's still not detecting the key is being held down, is there a different method I have to check for in KeyBinding?

 

public class GatherManaKeybind extends KeyBinding
{
   public GatherManaKeybind()
   {
      super("key.gatherMana", Keyboard.KEY_G, "key.categories." + AAReference.MODID);
   }


   @SubscribeEvent()
   public void onKeyInput(TickEvent.ClientTickEvent event)
   {
      if(event.phase == TickEvent.Phase.END && this.isPressed())
      {
         ~snip~
      }
   }
}

 

Here is just a bit of java info.

 

Java never "constantly" runs. Never. You can't program that (well you probably could but it's not how it works). What happens is you set the Boolean pressed to true, so it runs a class or etc. and then you check the Boolean throughout the code to make sure it is true, if it isn't, program the code to disregard the events that took place and to start over at Boolean pressed true when the keybind is pressed again.

 

NOTE: When I say disregard, I don't mean erase what happened due to the result of pressing the keybind, I mean to literally pretend it didn't happen (even though it did happen) and start over.

 

All of this is a bit hard to explain, but the point stand (if you can understand my mumbo-jumbo).

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.