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 [2020/07/25 19:10]
kot Change START_CLIENT_TICK to END_CLIENT_TICK (it is the preferred way)
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 KeyBinding 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 ====
Line 22: Line 25:
  
 <code java [enable_line_numbers="​true"​]>​ <code java [enable_line_numbers="​true"​]>​
-keyBinding = new KeyBinding(+keyBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding(
     "​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.     InputUtil.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.examplemod.test"​ // The translation key of the keybinding'​s category.
-);+));
 </​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.
  
-==== Configuring ​your Keybind's Category ​====+==== Responding to your Keybind ====
  
-Fabric ​will automatically register the keybinding'​s category on keybinding'​s registration. +The code here will print "Key 1 was pressed!"​ ingame.
- +
-==== Registering your Keybind ==== +
- +
-To register your keybinding, register using the **KeyBindingHelper**,​ **in the client mod initializer**:​+
  
 <code java> <code java>
-KeyBindingHelper.registerKeyBinding(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> +
-ClientTickEvents.END_CLIENT_TICK.register(client -> { +
-    while (keyBinding.wasPressed()) ​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.1595704249.txt.gz · Last modified: 2020/07/25 19:10 by kot