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
Next revisionBoth sides next revision
tutorial:keybinds [2020/02/02 02:28] draylartutorial:keybinds [2020/06/13 13:22] shedaniel
Line 6: Line 6:
  
 Adding a key-bind is easy. You'll need to: Adding a key-bind is easy. You'll need to:
-  * create a FabricKeyBinding object+  * create a KeyBinding object
   * register your key   * register your key
   * react to the key being pressed   * react to the key being pressed
Line 13: Line 13:
 ==== Creating your Keybind ==== ==== Creating your Keybind ====
  
-Fabric API has a **FabricKeyBinding** object, which makes it easier to create your own **KeyBinding**. Declare one of these in an area of your preference:+Declare one of these in an area of your preference:
  
 <code java> <code java>
-private static FabricKeyBinding keyBinding;+private static KeyBinding keyBinding;
 </code> </code>
  
Line 22: Line 22:
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-keyBinding = FabricKeyBinding.Builder.create+keyBinding = new KeyBinding
-    new Identifier("tutorial", "spook")+    "key.examplemod.spook", // The translation key of the keybinding's name 
-    InputUtil.Type.KEYSYM, +    InputUtil.Type.KEYSYM, // The type of the keybinding, KEYSYM for keyboard, MOUSE for mouse. 
-    GLFW.GLFW_KEY_R, +    GLFW.GLFW_KEY_R, // The keycode of the key 
-    "Wiki Keybinds" +    "category.examplemod.test// The translation key of the keybinding's category. 
-).build();+);
 </code> </code>
      
 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.
- 
  
 ==== Configuring your Keybind's Category ==== ==== Configuring your Keybind's Category ====
-If you opt to use a custom keybind category, you'll need to add it.  + 
-<code java> +Fabric will automatically register the keybinding's category on keybinding's registration.
-KeyBindingRegistry.INSTANCE.addCategory(String categoryName); +
-</code> +
-'categoryName' should match the 4th argument provided while calling 'Builder.create'. If you don't do this, a warning will appear in console.+
  
 ==== Registering your Keybind ==== ==== Registering your Keybind ====
  
-To register your keybinding, register using the **KeybindingRegistry**, **in the client mod initializer**:+To register your keybinding, register using the **KeyBindingHelper**, **in the client mod initializer**:
  
 <code java> <code java>
-KeyBindingRegistry.INSTANCE.register(keyBinding);+KeyBindingHelper.registerKeyBinding(keyBinding);
 </code> </code>
      
Line 52: Line 48:
  
 ==== Responding to your Keybind ==== ==== Responding to your Keybind ====
- 
  
 Unfortunately, there's no clear-cut way to respond to a keybinding. Most would agree the best way is to hook into the client tick event: Unfortunately, there's no clear-cut way to respond to a keybinding. Most would agree the best way is to hook into the client tick event:
  
 <code java> <code java>
-ClientTickCallback.EVENT.register(-> +ClientTickCallback.EVENT.register(client -> { 
-+    while (keyBinding.wasPressed()) System.out.println("was pressed!");
-    if(keyBinding.isPressed()) System.out.println("was pressed!");+
 }); });
 </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. 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.txt · Last modified: 2023/12/27 13:14 by 2601:188:cb7c:25a0:19fa:9122:4e5a:fad1