Skip to content

Add configurable keyboard rebinding via controls.txt#1543

Open
Lisiowen wants to merge 4 commits intoMCLCE:mainfrom
Lisiowen:main
Open

Add configurable keyboard rebinding via controls.txt#1543
Lisiowen wants to merge 4 commits intoMCLCE:mainfrom
Lisiowen:main

Conversation

@Lisiowen
Copy link
Copy Markdown

Description

This PR adds a keyboard keybind configuration system via a controls.txt file in the game directory (similar to username.txt).

  • Accessibility for users with non-QWERTY keyboard layouts (e.g France -> AZERTY)
  • Support for assistive input devices for users with limited mobility
  • QoL for users coming from Java/Bedrock who are used to their custom keybinds.
  • Maintains default keybinds since the controls.txt file is optional, it only overrides the defaults rather than replacing the current implementation.

Changes

Previous Behavior

Keyboard inputs were defined as hardcoded constants in the KeyboardMouseInput.h file, so there was no method to change these bindings without modifying the source code and recompiling.

Root Cause

The input system used a static, read-only implementation. There was also no system in place to bridge an external config file to the KeyboardMouseInput variables.

New Behavior

On launch, the game attempts to load the controls.txt file. Users can override a keybind using standard characters or modifier keys using decimal Windows Virtual-Key codes.

  • KEY_INVENTORY=I -> Standard character for "I"
  • KEY_SPRINT=160 -> VK code for Left Shift
  • KEY_SNEAK=162 -> VK code for Left Control

If the file doesn't exist or the specific action isn't included in the file, the game defaults to the standard LCE keybinds.

Fix Implementation

  • Replaced the hardcoded const members in KeyboardMouseInput.h with mutable static variables to allow the values to be reassigned at runtime during the KeyboardMouseInput::Init() sequence without changing their global state.
  • Added an INPUT_BINDING_TABLE macro as the source of truth for all the configurable inputs to KeyboardMouseInput.cpp, this defines the default values as well as generating the lookup map for any user configured overrides.
  • Added the LoadUserControls function, this uses std::map for efficient lookups and isdigit() to differentiate between VK codes and standard character inputs. It's also used to check if the controls.txt file exists and for skipping over any empty lines in the file.
  • Added the function call at the end of KeyboardMouseInput::Init() to ensure controls are ready before running.

Cross-Platform + Wine Support

  • Tested on both Windows and Arch Linux (Proton)
  • Changes maintain the use of Windows Virtual-Key codes which translate consistently across compatibility layers
  • Added logic to handle carriage returns (\r) so controls.txt files created on unix-based systems (Linux/macOS) or Windows don't cause parsing errors

AI Use Disclosure

No AI used.

Related Issues

Fixes #128
Closes #749

Documentation + Templates

Thanks for looking over my PR, I'm happy to update the project README with a section on how to use controls.txt and I can make a template file as a starting point for users as well as listing common VK codes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Suggestion] Add Customizable Keyboard Bindings [ACCESSIBILITY] Keyboard/mouse rebinding.

1 participant