User Tools

Site Tools


tutorial:keybinds

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:keybinds [2021/02/28 17:18] – Added a brief guide to the Client entrypoint in an effort to clear up confusion on where to place the subsequent code sailkitetutorial:keybinds [2025/10/22 16:50] (current) – revert that codeblock cassiancc
Line 9: Line 9:
 Adding a key-bind is easy. You'll need to: Adding a key-bind is easy. You'll need to:
   * open or create a Client [[documentation:entrypoint]]   * open or create a Client [[documentation:entrypoint]]
 +  * create a Keybinding.Category object
   * create a KeyBinding object   * create a KeyBinding object
   * react to the key being pressed   * react to the key being pressed
  
  
-See [[https://github.com/FabricMC/fabric/blob/1.16/fabric-key-binding-api-v1/src/testmod/java/net/fabricmc/fabric/test/client/keybinding/KeyBindingsTest.java|here]] for an updated example.+See [[https://github.com/FabricMC/fabric/blob/1.21.10/fabric-key-binding-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/client/keybinding/KeyBindingsTest.java|here]] for an updated example.
  
 ==== Preparing an Entrypoint ==== ==== Preparing an Entrypoint ====
Line 37: Line 38:
 </code> </code>
  
-So, what are we doing here? Fabric entrypoints for most use cases are designated by implementing a special interface unique to the side or sides that the code in the entrypoint should be run on. For our Client, we simply have our class implement the ''ClientModInitializer'' interface. The interface requires us to ''@Override'' a single method, ''onInitializeClient''. It is in this method (and the equivalents from the other entrypoints respectively) that we will often call methods provided by the Fabric API for easily registering and adding some of the objects and behaviors that we may wish to have in our mod. Of course, we'll also need to update our `fabric.mod.jsonto include our newly created entrypoint, so be sure to consult the [[documentation:entrypoint|entrypoints page]] if you need a refresher on that process.+So, what are we doing here? Fabric entrypoints for most use cases are designated by implementing a special interface unique to the side or sides that the code in the entrypoint should be run on. For our Client, we simply have our class implement the ''ClientModInitializer'' interface. The interface requires us to ''@Override'' a single method, ''onInitializeClient''. It is in this method (and the equivalents from the other entrypoints respectively) that we will often call methods provided by the Fabric API for easily registering and adding some of the objects and behaviors that we may wish to have in our mod. Of course, we'll also need to update our ''fabric.mod.json'' to include our newly created entrypoint, so be sure to consult the [[documentation:entrypoint|entrypoints page]] if you need a refresher on that process.
  
 ==== Creating your Keybind ==== ==== Creating your Keybind ====
  
-Declare one of these in an area of your preference:+Declare these in an area of your preference:
  
 <code java> <code java>
 private static KeyBinding keyBinding; private static KeyBinding keyBinding;
 +private static final KeyBinding.Category CATEGORY = KeyBinding.Category.create(Identifier.of("examplemod", "test"));
 </code> </code>
  
-FabricKeyBinding has a Builder for initialization. It takes in an Identifier, InputUtil.Type, key code, and binding category:+KeyBindingHelper has a Builder for initialization. It takes in an Identifier, InputUtil.Type, key code, and KeyBinding.Category:
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
Line 54: Line 56:
     InputUtil.Type.KEYSYM, // The type of the keybinding, KEYSYM for keyboard, MOUSE for mouse.     InputUtil.Type.KEYSYM, // The type of the keybinding, KEYSYM for keyboard, MOUSE for mouse.
     GLFW.GLFW_KEY_R, // The keycode of the key     GLFW.GLFW_KEY_R, // The keycode of the key
-    "category.examplemod.test" // The translation key of the keybinding's category.+    CATEGORY // The category of the key - you'll need to add a translation for this! 
 +    
 )); ));
 </code> </code>
  
-If you want a sticky key, add ''() -> true'' as last parameter.+On versions older than 1.21.9, you would substitute your Keybinding.Category for the translation key of the keybinding's category, e.g. "category.examplemod.test" 
 + 
 +Sticky keys can also be created with ''KeyBindingHelper''. In vanilla, the sneak and sprint keys act as sticky keys when they are set to 'Sneak: Toggle' and 'Sprint: Toggle' respectively. If a key binding should always act as a sticky key, then pass ''() -> true'' as the final parameter.
      
 ''GLFW.GLFW_KEY_R'' can be replaced with whatever key you want the binding to default to. The category is related to how the keybinding is grouped in the settings page. ''GLFW.GLFW_KEY_R'' can be replaced with whatever key you want the binding to default to. The category is related to how the keybinding is grouped in the settings page.
Line 64: Line 69:
 ==== Responding to your Keybind ==== ==== Responding to your Keybind ====
  
-The code here will print "Key 1 was pressed!" ingame.+The code here will print "Key 1 was pressed!" ingame. Keep note that this is entirely client-side. To have the server respond to a keybind, you'll need to send a custom packet and have the server handle it separately.
  
 +
 +For versions since 1.19:
 +<code java>
 +
 +ClientTickEvents.END_CLIENT_TICK.register(client -> {
 +    while (keyBinding.wasPressed()) {
 + client.player.sendMessage(Text.literal("Key 1 was pressed!"), false);
 +    }
 +});
 +</code>
 +
 +For versions below 1.19:
 <code java> <code java>
  
Line 75: Line 92:
 </code> </code>
      
-Keep note that this is entirely client-side. To have the server respond to a keybind, you'll need to send a custom packet and have the server handle it separately.+
tutorial/keybinds.1614532725.txt.gz · Last modified: 2021/02/28 17:18 by sailkite