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
Last revisionBoth sides next revision
tutorial:keybinds [2021/02/28 17:18] – Added a brief guide to the Client entrypoint in an effort to clear up confusion on where to place the subsequent code sailkitetutorial:keybinds [2023/10/01 03:26] – Clarify the function of sticky keys haykam
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|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_old|fabric.mod.json]] file.
  
 Adding a key-bind is easy. You'll need to: Adding a key-bind is easy. You'll need to:
Line 37: Line 37:
 </code> </code>
  
-So, what are we doing here? Fabric entrypoints for most use cases are designated by implementing a special interface unique to the side or sides that the code in the entrypoint should be run on. For our Client, we simply have our class implement the ''ClientModInitializer'' interface. The interface requires us to ''@Override'' a single method, ''onInitializeClient''. It is in this method (and the equivalents from the other entrypoints respectively) that we will often call methods provided by the Fabric API for easily registering and adding some of the objects and behaviors that we may wish to have in our mod. Of course, we'll also need to update our `fabric.mod.jsonto include our newly created entrypoint, so be sure to consult the [[documentation:entrypoint|entrypoints page]] if you need a refresher on that process.+So, what are we doing here? Fabric entrypoints for most use cases are designated by implementing a special interface unique to the side or sides that the code in the entrypoint should be run on. For our Client, we simply have our class implement the ''ClientModInitializer'' interface. The interface requires us to ''@Override'' a single method, ''onInitializeClient''. It is in this method (and the equivalents from the other entrypoints respectively) that we will often call methods provided by the Fabric API for easily registering and adding some of the objects and behaviors that we may wish to have in our mod. Of course, we'll also need to update our ''fabric.mod.json'' to include our newly created entrypoint, so be sure to consult the [[documentation:entrypoint|entrypoints page]] if you need a refresher on that process.
  
 ==== Creating your Keybind ==== ==== Creating your Keybind ====
Line 58: Line 58:
 </code> </code>
  
-If you want a sticky key, add ''() -> true'' as last parameter.+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 64: Line 64:
 ==== Responding to your Keybind ==== ==== Responding to your Keybind ====
  
-The code here will print "Key 1 was pressed!" ingame.+The code here will print "Key 1 was pressed!" ingame. 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.
  
 +
 +For versions since 1.19:
 +<code java>
 +
 +ClientTickEvents.END_CLIENT_TICK.register(client -> {
 +    while (keyBinding.wasPressed()) {
 + client.player.sendMessage(Text.literal("Key 1 was pressed!"), false);
 +    }
 +});
 +</code>
 +
 +For versions below 1.19:
 <code java> <code java>
  
Line 75: Line 87:
 </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.+
tutorial/keybinds.txt · Last modified: 2023/12/27 13:14 by 2601:188:cb7c:25a0:19fa:9122:4e5a:fad1