sway-farm
sway-farm copied to clipboard
Fix CMD Key Issue with useKeyboardControls in Player Component
While exploring the useKeyboardControls
hook from react-three/drei
for Player component, I discovered an issue exclusive to macOS users. The hook fails to ignore the CMD key, leading to a scenario where the character begins to move non-stop in one direction after a player uses a combination of the CMD key and a movement key (e.g., CMD+S for fullscreen). This behavior disrupts the gameplay experience and conflicts with common macOS shortcuts, causing unexpected and confusing behavior for the players.
Steps to Reproduce
- Press and hold CMD (on macOS) and then press one of the movement keys (e.g., arrow keys or WASD).
- Release both keys.
- Observe that the character continues to move in the direction of the movement key pressed, without stopping.
Solution and Implementation
To address this issue, I added a feature to the custom hook that intercepts and processes keyboard events, explicitly ignoring any inputs involving the CMD key. Additionally, the hook resets the controls state whenever the CMD key is pressed, immediately halting any ongoing movement commands. This ensures the character does not move indefinitely, allowing macOS users to use system shortcuts without triggering continuous movement in the game.