tutorial:keybinds
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous 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 sailkite | tutorial:keybinds [2025/12/12 16:57] (current) – fix half-finished mojmapping cassiancc | ||
|---|---|---|---|
| Line 9: | Line 9: | ||
| 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: | * open or create a Client [[documentation: | ||
| - | * create a KeyBinding | + | * create a KeyMapping.Category object |
| + | * create a KeyMapping | ||
| * react to the key being pressed | * react to the key being pressed | ||
| - | See [[https:// | + | See [[https:// |
| ==== Preparing an Entrypoint ==== | ==== Preparing an Entrypoint ==== | ||
| Line 37: | Line 38: | ||
| </ | </ | ||
| - | 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 '' | + | 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 '' |
| ==== Creating your Keybind ==== | ==== Creating your Keybind ==== | ||
| - | Declare | + | Declare these in an area of your preference: |
| <code java> | <code java> | ||
| - | private static | + | private static |
| + | private static final KeyMapping.Category CATEGORY = KeyMapping.Category.register(ResourceLocation.fromNamespaceAndPath(" | ||
| </ | </ | ||
| - | FabricKeyBinding | + | KeyBindingHelper |
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| - | keyBinding = KeyBindingHelper.registerKeyBinding(new | + | keyBinding = KeyBindingHelper.registerKeyBinding(new |
| - | " | + | |
| - | | + | InputConstants.Type.KEYSYM, |
| - | GLFW.GLFW_KEY_R, | + | |
| - | " | + | CATEGORY |
| )); | )); | ||
| </ | </ | ||
| - | If you want a sticky key, add '' | + | On versions older than 1.21.9, |
| + | |||
| + | Sticky keys can also be created with '' | ||
| | | ||
| '' | '' | ||
| Line 64: | Line 68: | ||
| ==== Responding to your Keybind ==== | ==== Responding to your Keybind ==== | ||
| - | The code here will print "Key 1 was pressed!" | + | The code here will print "Key 1 was pressed!" |
| + | |||
| + | For versions since 1.19: | ||
| <code java> | <code java> | ||
| ClientTickEvents.END_CLIENT_TICK.register(client -> { | ClientTickEvents.END_CLIENT_TICK.register(client -> { | ||
| - | | + | while (keyBinding.consumeClick()) { |
| - | client.player.sendMessage(new LiteralText("Key 1 was pressed!" | + | |
| - | } | + | } |
| + | }); | ||
| + | </ | ||
| + | |||
| + | For versions below 1.19: | ||
| + | <code java> | ||
| + | |||
| + | ClientTickEvents.END_CLIENT_TICK.register(client -> { | ||
| + | while (keyBinding.consumeClick()) { | ||
| + | | ||
| + | | ||
| }); | }); | ||
| </ | </ | ||
| | | ||
| - | 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.1614532725.txt.gz · Last modified: 2021/02/28 17:18 by sailkite