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 revision Previous revision
Next revision
Previous revision
tutorial:keybinds [2019/09/30 09:43]
fudge
tutorial:keybinds [2020/08/09 20:59] (current)
emmanuelmess Updated to new API
Line 4: Line 4:
  
 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 "​depends"​ 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:
-  * create a FabricKeyBinding ​object +  * create a KeyBinding ​object
-  * register your key+
   * 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.
  
 ==== 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 25:
  
 <code java [enable_line_numbers="​true"​]>​ <code java [enable_line_numbers="​true"​]>​
-keyBinding = FabricKeyBinding.Builder.create( +keyBinding = KeyBindingHelper.registerKeyBinding(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>​
 +
 +If you want a sticky key, add "() -> true" as last 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.
  
 +==== Responding to your Keybind ====
  
-==== Registering your Keybind ==== +The code here will print "Key 1 was pressed!"​ ingame.
- +
-To register your keybinding, register using the **KeybindingRegistry**,​ **in the client mod initializer**:​+
  
 <code java> <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. 
  
- +ClientTickCallback.EVENT.register(client ​-> { 
-==== Responding to your Keybind ==== +    ​while (keyBinding.wasPressed()) 
- + client.player.sendMessage(new LiteralText("Key 1 was pressed!"​), false); 
- +    }
-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> +
-ClientTickCallback.EVENT.register(-> +
-+
-    ​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.1569836589.txt.gz · Last modified: 2019/09/30 09:43 by fudge