User Tools

Site Tools


tutorial:keybinds

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
tutorial:keybinds [2019/04/06 21:02] – created draylartutorial:keybinds [2023/12/27 13:14] (current) – ↷ Links adapted because of a move operation 2601:188:cb7c:25a0:19fa:9122:4e5a:fad1
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 the ''%%"depends"%%'' block 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 FabricKeyBinding object +  * open or create a Client [[documentation:entrypoint]] 
-  * register your key+  * create a KeyBinding object
   * react to the key being pressed   * react to the key being pressed
  
  
-==== Creating your Keybind ====+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.
  
-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 an Entrypoint ====
  
-  private static FabricKeyBinding keyBinding;+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!
  
-FabricKeyBinding has Builder for initialization. It takes in an IdentifierInputUtil.Typekey code, and binding category:+In order to create Client entrypointwe'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:side]]We'll make a quick example class called ''ExampleClientEntrypoint''but usually the common practice would be to name the class "YourModName" followed by "Client", e.g. ''YoYoDeleriumClient'' or ''HappySheepHammocksClient''. Let's take a look at the code, and then we'll explain what's happening:
  
-  keyBinding FabricKeyBinding.Builder.create( +<code java[enable_line_numbers="true"]> 
-    new Identifier("wiki-keybinds", "spook"), +/* package */ 
-    InputUtil.Type.KEY_KEYBOARD, +/* imports */
-    GLFW.GLFW_KEY_R, +
-    "Wiki Keybinds" +
-  ).build(); +
-   +
-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.+
  
 +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
 +    }
 +}
 +</code>
  
-==== Registering your Keybind ====+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.
  
-To register your keybinding, use the **KeybindingRegistry**:+==== Creating your Keybind ====
  
-  KeyBindingRegistry.INSTANCE.register(keyBinding); +Declare one of these in an area of your preference:
-   +
-If you log in to your game now, you will see your key binding in the settings page.+
  
 +<code java>
 +private static KeyBinding keyBinding;
 +</code>
 +
 +FabricKeyBinding has a Builder for initialization. It takes in an Identifier, InputUtil.Type, key code, and binding category:
 +
 +<code java [enable_line_numbers="true"]>
 +keyBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding(
 +    "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.
 +    GLFW.GLFW_KEY_R, // The keycode of the key
 +    "category.examplemod.test" // The translation key of the keybinding's category.
 +));
 +</code>
 +
 +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.
  
 ==== Responding to your Keybind ==== ==== Responding to your Keybind ====
  
 +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.
  
-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: 
  
-  ClientTickCallback.EVENT.register(-> +For versions since 1.19: 
-  +<code java> 
-    if(keyBinding.wasPressed()) System.out.println("was pressed!"); + 
-  });+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> 
 + 
 +ClientTickEvents.END_CLIENT_TICK.register(client -> 
 +    while (keyBinding.wasPressed()) 
 + client.player.sendMessage(new LiteralText("Key 1 was pressed!"), false); 
 +    } 
 +}); 
 +</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.1554584566.txt.gz · Last modified: 2019/04/06 21:02 by draylar