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 [2019/04/06 21:04] 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:
  
-  private static FabricKeyBinding keyBinding;+<code java> 
 +private static KeyBinding keyBinding; 
 +</code>
  
 FabricKeyBinding has a Builder for initialization. It takes in an Identifier, InputUtil.Type, key code, and binding category: FabricKeyBinding has a Builder for initialization. It takes in an Identifier, InputUtil.Type, key code, and binding category:
  
-  keyBinding = FabricKeyBinding.Builder.create+<code java [enable_line_numbers="true"]> 
-    new Identifier("wiki-keybinds", "spook")+keyBinding = new KeyBinding
-    InputUtil.Type.KEY_KEYBOARD+    "key.examplemod.spook", // The translation key of the keybinding's name 
-    GLFW.GLFW_KEY_R, +    InputUtil.Type.KEYSYM// The type of the keybinding, KEYSYM for keyboard, MOUSE for mouse. 
-    "Wiki Keybinds+    GLFW.GLFW_KEY_R, // The keycode of the key 
-  ).build();+    "category.examplemod.test// The translation key of the keybinding's category. 
 +); 
 +</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 ====
 +
 +Fabric will automatically register the keybinding's category on keybinding's registration.
  
 ==== Registering your Keybind ==== ==== Registering your Keybind ====
  
-To register your keybinding, use the **KeybindingRegistry**:+To register your keybinding, register using the **KeyBindingHelper**, **in the client mod initializer**:
  
-  KeyBindingRegistry.INSTANCE.register(keyBinding);+<code java> 
 +KeyBindingHelper.registerKeyBinding(keyBinding); 
 +</code>
      
 If you log in to your game now, you will see your key binding in the settings page. If you log in to your game now, you will see your key binding in the settings page.
Line 39: 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:
  
-  ClientTickCallback.EVENT.register(-> +<code java> 
-  +ClientTickCallback.EVENT.register(client -> { 
-    if(keyBinding.isPressed()) System.out.println("was pressed!"); +    while (keyBinding.wasPressed()) System.out.println("was pressed!"); 
-  });+}); 
 +</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