tutorial:keybinds
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorial:keybinds [2020/07/25 19:04] – Update deprecated ClientTickCallback to use ClientTickEvents kot | tutorial:keybinds [2025/10/22 16:50] (current) – revert that codeblock cassiancc | ||
|---|---|---|---|
| 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 '' | ||
| 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: | ||
| + | * create a Keybinding.Category object | ||
| * 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:// | ||
| + | |||
| + | ==== Preparing an Entrypoint ==== | ||
| + | |||
| + | If you already have a Client entrypoint created and you are familiar with how it works, you can safely proceed to the [[#Creating your Keybind|next section]]. Otherwise, stick around for a quick overview! | ||
| + | |||
| + | In order to create a Client entrypoint, we'll need to do a couple of different things to let Fabric know that we intend to specify code that only needs to be executed by the physical client [[tutorial: | ||
| + | |||
| + | <code java[enable_line_numbers=" | ||
| + | /* package */ | ||
| + | /* imports */ | ||
| + | |||
| + | public class ExampleClientEntrypoint implements ClientModInitializer { | ||
| + | | ||
| + | // The KeyBinding declaration and registration are commonly executed here statically | ||
| + | | ||
| + | @Override | ||
| + | public void onInitializeClient() { | ||
| + | | ||
| + | // Event registration will be executed inside this method | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | 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 KeyBinding keyBinding; | private static KeyBinding keyBinding; | ||
| + | private static final KeyBinding.Category CATEGORY = KeyBinding.Category.create(Identifier.of(" | ||
| </ | </ | ||
| - | FabricKeyBinding | + | KeyBindingHelper |
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| - | keyBinding = new KeyBinding( | + | keyBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding( |
| " | " | ||
| InputUtil.Type.KEYSYM, | InputUtil.Type.KEYSYM, | ||
| GLFW.GLFW_KEY_R, | GLFW.GLFW_KEY_R, | ||
| - | | + | |
| - | ); | + | |
| + | )); | ||
| </ | </ | ||
| - | | ||
| - | 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 | + | On versions older than 1.21.9, you would substitute |
| - | Fabric will automatically register | + | Sticky keys can also be created with '' |
| + | |||
| + | '' | ||
| - | ==== Registering | + | ==== Responding to your Keybind ==== |
| - | To register your keybinding, register using the **KeyBindingHelper**, **in the client mod initializer**: | + | The code here will print "Key 1 was pressed!" |
| + | |||
| + | For versions since 1.19: | ||
| <code java> | <code java> | ||
| - | KeyBindingHelper.registerKeyBinding(keyBinding); | + | |
| + | ClientTickEvents.END_CLIENT_TICK.register(client -> { | ||
| + | while (keyBinding.wasPressed()) { | ||
| + | client.player.sendMessage(Text.literal(" | ||
| + | } | ||
| + | }); | ||
| </ | </ | ||
| - | | ||
| - | If you log in to your game now, you will see your key binding in the settings page. | ||
| - | |||
| - | |||
| - | ==== Responding to your Keybind ==== | ||
| - | |||
| - | Unfortunately, | ||
| + | For versions below 1.19: | ||
| <code java> | <code java> | ||
| - | ClientTickEvents.START_CLIENT_TICK.register(client -> { | + | |
| - | while (keyBinding.wasPressed()) | + | ClientTickEvents.END_CLIENT_TICK.register(client -> { |
| + | while (keyBinding.wasPressed()) | ||
| + | client.player.sendMessage(new LiteralText("Key 1 was pressed!" | ||
| + | } | ||
| }); | }); | ||
| </ | </ | ||
| | | ||
| - | 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.1595703888.txt.gz · Last modified: 2020/07/25 19:04 by kot