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 [2023/06/10 12:04] – ↷ Links adapted because of a move operation 83.246.176.125tutorial:keybinds [2025/12/12 16:57] (current) – fix half-finished mojmapping cassiancc
Line 5: Line 5:
 Minecraft handles user input from peripherals such as the keyboard & mouse using key-binds. When you press W your character moves forward, and when you press E your inventory opens. Every keybind can also be configured with the settings menu, so you can make your player move with arrow keys instead of WASD if you so desire. Minecraft handles user input from peripherals such as the keyboard & mouse using key-binds. When you press W your character moves forward, and when you press E your inventory opens. Every keybind can also be configured with the settings menu, so you can make your player move with arrow keys instead of WASD if you so desire.
  
-This tutorial assumes you have the key bindings API, if not add ''%%"fabric-key-binding-api-v1": "*"%%'' to the ''%%"depends"%%'' block in your [[documentation:fabric_mod_json_spec_old|fabric.mod.json]] file.+This tutorial assumes you have the key bindings API, if not add ''%%"fabric-key-binding-api-v1": "*"%%'' to the ''%%"depends"%%'' block in your [[documentation:fabric_mod_json_spec|fabric.mod.json]] file.
  
 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 object+  * create a KeyMapping.Category object 
 +  * create a KeyMapping 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.11/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 41: Line 42:
 ==== 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 KeyMapping keyBinding
 +private static final KeyMapping.Category CATEGORY = KeyMapping.Category.register(ResourceLocation.fromNamespaceAndPath("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 KeyMapping.Category:
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-keyBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding+keyBinding = KeyBindingHelper.registerKeyBinding(new KeyMapping
-    "key.examplemod.spook", // The translation key of the keybinding's name +       "key.examplemod.spook", // The translation key of the keybinding's name 
-    InputUtil.Type.KEYSYM, // The type of the keybinding, KEYSYM for keyboard, MOUSE for mouse. +       InputConstants.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 KeyMapping.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 71: Line 75:
  
 ClientTickEvents.END_CLIENT_TICK.register(client -> { ClientTickEvents.END_CLIENT_TICK.register(client -> {
-    while (keyBinding.wasPressed()) { +   while (keyBinding.consumeClick()) { 
- client.player.sendMessage(Text.literal("Key 1 was pressed!"), false); +       client.player.displayClientMessage(Component.literal("Key 1 was pressed!"), false); 
-    }+   }
 }); });
 </code> </code>
Line 81: Line 85:
  
 ClientTickEvents.END_CLIENT_TICK.register(client -> { ClientTickEvents.END_CLIENT_TICK.register(client -> {
-    while (keyBinding.wasPressed()) { +   while (keyBinding.consumeClick()) { 
- client.player.sendMessage(new LiteralText("Key 1 was pressed!"), false); +       client.player.displayClientMessage(new TextComponent("Key 1 was pressed!"), false); 
-    }+   }
 }); });
 </code> </code>
      
  
tutorial/keybinds.1686398689.txt.gz · Last modified: 2023/06/10 12:04 by 83.246.176.125