KeyMap - jimdroberts/FishMMO GitHub Wiki

Description

The KeyMap struct represents a mapping between a custom virtual key name and its corresponding Unity KeyCode. It provides a lightweight way to associate a user-friendly name with a specific physical key and includes convenience methods to check the current state of the mapped key using Unity's Input system. This struct is used by the InputManager for flexible and efficient key rebinding.


API Access

Properties

  • public string VirtualKey { get; private set; }

    The custom string name for this virtual key (e.g., "Jump", "Interact", "Hotkey 1").

  • public KeyCode Key { get; set; }

    The Unity KeyCode that is mapped to this virtual key. Allows runtime key rebinding.

Methods

  • public KeyMap(string virtualKey, KeyCode key)

    Initializes a new instance of the KeyMap struct with the specified virtual key name and physical key code.

  • public bool GetKey()

    Checks if the mapped physical key is currently being held down. Wraps Input.GetKey(KeyCode).

  • public bool GetKeyDown()

    Checks if the mapped physical key was pressed down in the current frame. Wraps Input.GetKeyDown(KeyCode).

  • public bool GetKeyUp()

    Checks if the mapped physical key was released in the current frame. Wraps Input.GetKeyUp(KeyCode).


Basic Usage

Setup

  1. Create a KeyMap instance with a virtual key name and a Unity KeyCode.
  2. Use the provided methods to check the key's state in your input handling code.
  3. Use with InputManager for flexible key rebinding and input abstraction.

Example

var jumpKey = new KeyMap("Jump", KeyCode.Space);
if (jumpKey.GetKeyDown())
{
    // Handle jump action
}

Best Practices

  • Use virtual key names throughout your code for flexibility and easy rebinding.
  • Use the provided methods (GetKey, GetKeyDown, GetKeyUp) for efficient input checks.
  • Integrate KeyMap with your input management system (e.g., InputManager) for centralized control.
  • Prefer structs for small, frequently copied data to avoid heap allocations.
⚠️ **GitHub.com Fallback** ⚠️