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 [2019/06/17 19:03] – modid refactoring draylar
Line 15: Line 15:
 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: 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:
  
-  private static FabricKeyBinding keyBinding;+<code java> 
 +private static FabricKeyBinding 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 = FabricKeyBinding.Builder.create( 
-    InputUtil.Type.KEY_KEYBOARD,+    new Identifier("tutorial", "spook"), 
 +    InputUtil.Type.KEYSYM,
     GLFW.GLFW_KEY_R,     GLFW.GLFW_KEY_R,
     "Wiki Keybinds"     "Wiki Keybinds"
-  ).build();+).build(); 
 +</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.
Line 33: Line 37:
 To register your keybinding, use the **KeybindingRegistry**: To register your keybinding, use the **KeybindingRegistry**:
  
-  KeyBindingRegistry.INSTANCE.register(keyBinding);+<code java> 
 +KeyBindingRegistry.INSTANCE.register(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 43: Line 49:
 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(e -> +<code java> 
-  {+ClientTickCallback.EVENT.register(e -> 
 +{
     if(keyBinding.isPressed()) System.out.println("was pressed!");     if(keyBinding.isPressed()) 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