// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
#pragma once
#include "InputDevice.h"
///
/// Represents a single hardware keyboard device. Used by the Input to report raw keyboard input events.
///
API_CLASS(NoSpawn) class FLAXENGINE_API Keyboard : public InputDevice
{
DECLARE_SCRIPTING_TYPE_NO_SPAWN(Keyboard);
protected:
struct State
{
uint16 InputTextLength;
Char InputText[32];
bool Keys[(int32)KeyboardKeys::MAX];
void Clear();
};
State _state;
State _prevState;
explicit Keyboard();
public:
///
/// Gets the text entered during the current frame (Unicode format).
///
API_PROPERTY() StringView GetInputText() const
{
return StringView(_state.InputText, _state.InputTextLength);
}
///
/// Gets keyboard key state.
///
/// Key ID to check.
/// True if user holds down the key identified by id, otherwise false.
API_FUNCTION() FORCE_INLINE bool GetKey(KeyboardKeys key) const
{
return _state.Keys[static_cast(key)];
}
///
/// Gets keyboard key down state.
///
/// Key ID to check
/// True if user starts pressing down the key, otherwise false.
API_FUNCTION() FORCE_INLINE bool GetKeyDown(KeyboardKeys key) const
{
return _state.Keys[static_cast(key)] && !_prevState.Keys[static_cast(key)];
}
///
/// Gets keyboard key up state.
///
/// Key ID to check
/// True if user releases the key, otherwise false.
API_FUNCTION() FORCE_INLINE bool GetKeyUp(KeyboardKeys key) const
{
return !_state.Keys[static_cast(key)] && _prevState.Keys[static_cast(key)];
}
///
/// Checks if any keyboard key is currently pressed.
///
API_PROPERTY() bool IsAnyKeyDown() const;
public:
///
/// Called when keyboard enters input character.
///
/// The Unicode character entered by the user.
/// The target window to receive this event, otherwise input system will pick the window automatically.
void OnCharInput(Char c, Window* target = nullptr);
///
/// Called when key goes up.
///
/// The keyboard key.
/// The target window to receive this event, otherwise input system will pick the window automatically.
void OnKeyUp(KeyboardKeys key, Window* target = nullptr);
///
/// Called when key goes down.
///
/// The keyboard key.
/// The target window to receive this event, otherwise input system will pick the window automatically.
void OnKeyDown(KeyboardKeys key, Window* target = nullptr);
public:
// [InputDevice]
void ResetState() override;;
bool Update(EventQueue& queue) final override;
};