Keyboard Manager - Horizon-NTH/HorizonGUI GitHub Wiki

KeyBoardManager

Overview

The KeyBoardManager class in the hgui namespace facilitates keyboard input handling in your graphical user interface. It allows you to bind specific key with a specific actions to corresponding functions, enabling you to respond to key action. The class supports binding both individual keys and key combinations. The class is designed as a static class and can be found in the header file KeyBoardManager.h).

Types

  • KeyBoardAction: Represents a keyboard action as a pair of keys and actions.

  • KeyBoardCombinationAction: Represents a keyboard combination action as a pair of a vector of keys and actions.

Member Functions

  • void bind(const std::variant<KeyBoardAction, KeyBoardCombinationAction>& action, const std::function<void()>& function): Binds a keyboard action or combination to a specific function.

  • bool is_bind(const std::variant<KeyBoardAction, KeyBoardCombinationAction>& action): Checks if a keyboard action or combination is already bound.

  • const std::function<void()>& get_bind(const std::variant<KeyBoardAction, KeyBoardCombinationAction>& action): Retrieves the function bound to a keyboard action or combination.

  • void unbind(const std::variant<KeyBoardAction, KeyBoardCombinationAction>& action): Unbinds a previously bound keyboard action or combination.

  • void bind_key_callback(const std::variant<std::function<void()>, std::function<void(keys, actions)>& function): Binds a key callback function that can handle either a single key or key-action pair or receive key and action information.

Example Usage

#include <hgui/header/KeyBoardManager.h>

// Create a function to be called when the 'W' key is pressed
void OnWKeyPressed() {
    // Your custom action when 'W' key is pressed
    std::cout << "The 'W' key was pressed!" << std::endl;
}

// Create a function to be called when the space key is pressed and released
void OnSpaceKeyAction(keys key, actions action) {
    if (key == hgui::keys::SPACE && action == hgui::actions::PRESS) {
        // Your custom action when the space key is pressed
        std::cout << "The space key was pressed!" << std::endl;
    }
    else if (key == hgui::keys::SPACE && action == hgui::actions::RELEASE) {
        // Your custom action when the space key is released
        std::cout << "The space key was released!" << std::endl;
    }
}

int main() {
    // Bind 'W' key press to the OnWKeyPressed function
    hgui::KeyBoardManager::bind(hgui::KeyBoardAction(hgui::keys::W, hgui::actions::PRESS), OnWKeyPressed);

    // Bind space key press and release to the OnSpaceKeyAction function
    hgui::KeyBoardManager::bind_keycallback(OnSpaceKeyAction);

    // Unbind keys if necessary
    hgui::KeyBoardManager::unbind(hgui::KeyBoardAction(hgui::keys::W, hgui::actions::PRESS));

    // You can also bind a key combination
    hgui::KeyBoardManager::bind(hgui::KeyBoardCombinationAction({ hgui::keys::LEFT_CONTROL, hgui::keys::S }, hgui::actions::PRESS), /* function */);

    // Process your application

    return 0;
}

Note: The KeyBoardManager class allows you to bind key actions and combinations to specific functions and provides flexibility for handling keyboard input. Be sure to handle unbinding and processing of key events appropriately for your application's requirements.

⚠️ **GitHub.com Fallback** ⚠️