tutorial:keybinds
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:keybinds [2019/09/30 09:43] – fudge | 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 | ||
---|---|---|---|
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: | ||
- | * create a FabricKeyBinding object | + | * open or create a Client [[documentation: |
- | * register your key | + | * create a KeyBinding object |
* react to the key being pressed | * react to the key being pressed | ||
- | ==== Creating your Keybind ==== | + | See [[https:// |
- | 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: | + | ==== Preparing |
- | <code java> | + | 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! |
- | private static FabricKeyBinding keyBinding; | + | |
- | </ | + | |
- | FabricKeyBinding has a Builder for initialization. It takes in an Identifier, InputUtil.Type, key code, and binding category: | + | In order to create |
- | <code java [enable_line_numbers=" | + | <code java[enable_line_numbers=" |
- | keyBinding = FabricKeyBinding.Builder.create( | + | /* package */ |
- | | + | /* imports */ |
- | | + | |
- | | + | public class ExampleClientEntrypoint implements ClientModInitializer { |
- | | + | |
- | ).build(); | + | |
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | // Event registration will be executed inside this method | ||
+ | } | ||
+ | } | ||
</ | </ | ||
- | | ||
- | 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. | ||
+ | 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 '' | ||
- | ==== Registering | + | ==== Creating |
- | To register your keybinding, register using the **KeybindingRegistry**, | + | Declare one of these in an area of your preference: |
<code java> | <code java> | ||
- | KeyBindingRegistry.INSTANCE.register(keyBinding); | + | private static KeyBinding |
</ | </ | ||
- | | ||
- | If you log in to your game now, you will see your key binding in the settings page. | ||
+ | FabricKeyBinding has a Builder for initialization. It takes in an Identifier, InputUtil.Type, | ||
- | ==== Responding to your Keybind ==== | + | <code java [enable_line_numbers=" |
+ | keyBinding | ||
+ | " | ||
+ | InputUtil.Type.KEYSYM, | ||
+ | GLFW.GLFW_KEY_R, | ||
+ | " | ||
+ | )); | ||
+ | </ | ||
+ | If you want a sticky key, add '' | ||
+ | | ||
+ | '' | ||
- | Unfortunately, | + | ==== Responding |
+ | |||
+ | The code here will print "Key 1 was pressed!" | ||
<code java> | <code java> | ||
- | ClientTickCallback.EVENT.register(e -> | + | |
- | { | + | ClientTickEvents.END_CLIENT_TICK.register(client |
- | | + | |
+ | 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. | 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