Key Mappings

From Forge Community Wiki
Revision as of 01:26, 4 January 2021 by ChampionAsh5357 (talk | contribs) (Key Bindings docs, should be applied within some subcategory)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

An input is required to produce some sort of action to the player within the game. These inputs can be boiled down to bindings associated with a certain key or mouse click. To allow these inputs to be remappable, a KeyBinding can be declared and registered.

A KeyBinding can be declared with the following parameters:

Parameter Description
description A translation key used to describe what this binding will do (e.g. key.modid.keyName).
keyConflictContext Determines when the key binding should conflict with another defined key binding. By default, there are three values within KeyConflictContext: UNIVERSAL which are used in every context, GUI which are used whenever a screen is open, and IN_GAME whenever a screen is not open. Custom contexts can be created by implementing IKeyConflictContext.
inputType Determines the type of input this binding will declare by default. There are three possible values: KEYSYM which represents a mapped key, SCANCODE which represents the value emitted by the keyboard itself, and MOUSE which represents a mouse click.
keyCode The associated key codes based on the specified input type as mapped by GLFW.
category A translation key representing the category this key is located in (e.g. key.modid.categories.categoryName).
If you would like there to be no binding by default, use a constructor that contains InputMappings$Input instead and supply InputMappings#INPUT_INVALID as the argument.

The KeyBinding can then be registered using ClientRegistry::registerKeyBinding within FMLClientSetupEvent.

Using Registered Bindings

There are two contexts in which a key binding can be used normally: in or not in a screen. As such, there are two ways to handle these bindings. When not in a screen, ClientTickEvent should be used to determine whether the key is down using KeyBinding#isKeyDown. If within a screen, the following logic can be applied using KeyBinding#isActiveAndMatches within IGuiEventListener#keyPressed and IGuiEventListener#mouseClicked for mouse input. Note that the necessary InputMappings$Input can be constructed using InputMappings::getInputByCode or MOUSE::getOrMakeInput respectively.