Loading...
Searching...
No Matches
ml::Keyable Class Reference

Trait that adds keyboard-input callbacks to any Core object. More...

#include <Malena/Traits/Interaction/Keyable.h>

Inheritance diagram for ml::Keyable:
[legend]

Public Member Functions

void onKeypress (std::function< void()> callback, bool overwrite=true)
 Register a no-argument callback invoked when a key is pressed while this component has focus.
void onKeypress (std::function< void(const std::optional< sf::Event > &)> callback, bool overwrite=true)
 Register a callback invoked when a key is pressed while this component has focus, receiving the raw SFML event.
void onKeyRelease (std::function< void()> callback, bool overwrite=true)
 Register a no-argument callback invoked when a key is released while this component has focus.
void onKeyRelease (std::function< void(const std::optional< sf::Event > &)> callback, bool overwrite=true)
 Register a callback invoked when a key is released while this component has focus, receiving the raw SFML event.
void onTextEntered (std::function< void()> callback, bool overwrite=true)
 Register a no-argument callback invoked when a unicode character is entered while this component has focus.
void onTextEntered (std::function< void(const std::optional< sf::Event > &)> callback, bool overwrite=true)
 Register a callback invoked when a unicode character is entered while this component has focus, receiving the raw SFML event.
virtual void process (const std::string &key, const std::optional< sf::Event > &event)
 Invoke all callbacks registered for key.
template<typename ENUM_TYPE>
void process (ENUM_TYPE eventName, const std::optional< sf::Event > &event)
 Invoke all callbacks registered for eventName.

Detailed Description

Trait that adds keyboard-input callbacks to any Core object.

Keyable is automatically inherited by every ml::Core object. Keyboard callbacks only fire on the component that currently has focus (see Focusable). A component receives no keyboard events unless it has been clicked or focus has been explicitly transferred to it.

Usage

// React to any key press
myInput.onKeypress([](const std::optional<sf::Event>& e){
if (auto* kp = e->getIf<sf::Event::KeyPressed>())
if (kp->code == sf::Keyboard::Key::Enter)
submit();
});
// React to typed characters (unicode-aware)
myInput.onTextEntered([](const std::optional<sf::Event>& e){
if (auto* te = e->getIf<sf::Event::TextEntered>())
buffer += static_cast<char>(te->unicode);
});
// React to key release
myInput.onKeyRelease([]{ /* key released *\/ });
*

To unsubscribe:

myInput.unsubscribe(ml::Event::KEYPRESS);
myInput.unsubscribe(ml::Event::KEY_RELEASE);
myInput.unsubscribe(ml::Event::TEXT_ENTERED);
@ TEXT_ENTERED
Unicode character entered while component has focus.
Definition Event.h:57
@ KEYPRESS
Key pressed while component has focus.
Definition Event.h:55
@ KEY_RELEASE
Key released while component has focus.
Definition Event.h:56
See also
Focusable, ml::Event::KEYPRESS, ml::Event::KEY_RELEASE, ml::Event::TEXT_ENTERED, Unsubscribable

Definition at line 54 of file Keyable.h.

Member Function Documentation

◆ onKeypress() [1/2]

void ml::Keyable::onKeypress ( std::function< void()> callback,
bool overwrite = true )

Register a no-argument callback invoked when a key is pressed while this component has focus.

Parameters
callbackFunction invoked with no arguments on key press.
overwriteWhen true, replaces any existing callback; when false, appends an additional one.

◆ onKeypress() [2/2]

void ml::Keyable::onKeypress ( std::function< void(const std::optional< sf::Event > &)> callback,
bool overwrite = true )

Register a callback invoked when a key is pressed while this component has focus, receiving the raw SFML event.

Parameters
callbackFunction invoked with the SFML event on key press.
overwriteWhen true, replaces any existing callback; when false, appends an additional one.

◆ onKeyRelease() [1/2]

void ml::Keyable::onKeyRelease ( std::function< void()> callback,
bool overwrite = true )

Register a no-argument callback invoked when a key is released while this component has focus.

Parameters
callbackFunction invoked with no arguments on key release.
overwriteWhen true, replaces any existing callback; when false, appends an additional one.

◆ onKeyRelease() [2/2]

void ml::Keyable::onKeyRelease ( std::function< void(const std::optional< sf::Event > &)> callback,
bool overwrite = true )

Register a callback invoked when a key is released while this component has focus, receiving the raw SFML event.

Parameters
callbackFunction invoked with the SFML event on key release.
overwriteWhen true, replaces any existing callback; when false, appends an additional one.

◆ onTextEntered() [1/2]

void ml::Keyable::onTextEntered ( std::function< void()> callback,
bool overwrite = true )

Register a no-argument callback invoked when a unicode character is entered while this component has focus.

Prefer onTextEntered over onKeypress for text input — it handles modifier keys, input method editors (IME), and non-ASCII characters correctly.

Parameters
callbackFunction invoked with no arguments when text is entered.
overwriteWhen true, replaces any existing callback; when false, appends an additional one.

◆ onTextEntered() [2/2]

void ml::Keyable::onTextEntered ( std::function< void(const std::optional< sf::Event > &)> callback,
bool overwrite = true )

Register a callback invoked when a unicode character is entered while this component has focus, receiving the raw SFML event.

Parameters
callbackFunction invoked with the SFML event when text is entered.
overwriteWhen true, replaces any existing callback; when false, appends an additional one.

◆ process() [1/2]

virtual void ml::EventReceiver::process ( const std::string & key,
const std::optional< sf::Event > & event )
virtualinherited

Invoke all callbacks registered for key.

Called by framework dispatchers. key is the string produced by EnumKey::get(eventEnum).

Parameters
keyString key identifying the event.
eventSFML event forwarded to each callback.

◆ process() [2/2]

template<typename ENUM_TYPE>
void ml::EventReceiver::process ( ENUM_TYPE eventName,
const std::optional< sf::Event > & event )
inherited

Invoke all callbacks registered for eventName.

Template overload that accepts any enum value directly, converting to a string key via EnumKey::get() internally.

Template Parameters
ENUM_TYPEAny enum type.
Parameters
eventNameThe enum value identifying the event.
eventSFML event forwarded to each callback.

The documentation for this class was generated from the following file: