Key Mappings
A key mapping, or key binding, defines a particular action that should be tied to an input: mouse click, key press, etc. Each action defined by a key mapping can be checked whenever the client can take an input. Furthermore, each key mapping can be assigned to any input through the Controls option menu.
Registering a KeyMapping
A KeyMapping
can be registered by listening to the RegisterKeyMappingsEvent
on the mod event bus only on the physical client and calling #register
.
// In some physical client only class
// Key mapping is lazily initialized so it doesn't exist until it is registered
public static final Lazy<KeyMapping> EXAMPLE_MAPPING = Lazy.of(() -> /*...*/);
// Event is on the mod event bus only on the physical client
@SubscribeEvent
public void registerBindings(RegisterKeyMappingsEvent event) {
event.register(EXAMPLE_MAPPING.get());
}
Creating a KeyMapping
A KeyMapping
can be created using it's constructor. The KeyMapping
takes in a translation key defining the name of the mapping, the default input of the mapping, and the translation key defining the category the mapping will be put within in the Controls option menu.
A KeyMapping
can be added to a custom category by providing a category translation key not provided by vanilla. Custom category translation keys should contain the mod id (e.g. key.categories.examplemod.examplecategory
).
Default Inputs
Each key mapping has a default input associated with it. This is provided through InputConstants.Key. Each input consists of an InputConstants.Type, which defines what device is providing the input, and an integer, which defines the associated identifier of the input on the device.
Vanilla provides three types of inputs: KEYSYM
, which defines a keyboard through the provided GLFW
key tokens, SCANCODE
, which defines a keyboard through the platform-specific scancode, and MOUSE
, which defines a mouse.
It is highly recommended to use KEYSYM
over SCANCODE
for keyboards as GLFW
key tokens are not tied to any particular system. You can read more on the GLFW docs.
The integer is dependent on the type provided. All input codes are defined in GLFW
: KEYSYM
tokens are prefixed with GLFW_KEY_*
while MOUSE
codes are prefixed with GLFW_MOUSE_*
.
new KeyMapping(
"key.examplemod.example1", // Will be localized using this translation key
InputConstants.Type.KEYSYM, // Default mapping is on the keyboard
GLFW.GLFW_KEY_P, // Default key is P
"key.categories.misc" // Mapping will be in the misc category
)
If the key mapping should not be mapped to a default, the input should be set to InputConstants#UNKNOWN
. The vanilla constructor will require you to extract the input code via InputConstants$Key#getValue
while the NeoForge constructor can be supplied the raw input field.