Hi again,
I'm trying to hookup a controller to a custom CanvasSFML widget and can't get key events to register. Mouse move and click work fine.
Header:
src:
The log messages from isFocused, keyPressed, canHandleKeyPress and getSignal never get called but the messages/actions in mouseMoved and leftMouseReleased do.
The canvas hierarchy is: GameCanvas > VerticalLayout["LeftPanel"] > HorizontalLayout["HudRoot"] > RootContainer.

I've looked through the forums, the tutorial pages on signals and custom widgets, and TGUI source code, especially EditBox, but I just can't seem to figure out how to get the canvas to receive keyboard input.
If someone could point out what I'm doing wrong in the above code or provide me some basic code showing how to create a custom canvas with keyPress functionality, I'd greatly appreciate it.
Cheers
I'm trying to hookup a controller to a custom CanvasSFML widget and can't get key events to register. Mouse move and click work fine.
Header:
Code Select
class GameCanvas : public tgui::CanvasSFML{
public:
using Ptr = std::shared_ptr<GameCanvas>;
GameCanvas(flecs::world& w);
static GameCanvas::Ptr create(flecs::world& w);
bool canHandleKeyPress(const tgui::Event::KeyEvent& event) override;
void keyPressed (const tgui::Event::KeyEvent& event) override;
bool leftMousePressed(tgui::Vector2f pos) override;
void leftMouseReleased(tgui::Vector2f pos) override;
void rightMousePressed(tgui::Vector2f pos) override;
void rightMouseReleased(tgui::Vector2f pos) override;
void mouseMoved(tgui::Vector2f pos) override;
bool isFocused() const{
bool f = tgui::CanvasSFML::isFocused();
std::cout << "GameCanvas::isFocused(): " << (f ? "True" : "False") << std::endl;
return f;
}
tgui::SignalTyped<tgui::Event::KeyEvent> onKeyPress = {"onKeyPress"};
protected:
tgui::Signal& getSignal(tgui::String signalName) override;
private:
flecs::world world;
};
src:
Code Select
#include "gameCanvas.hpp"
GameCanvas::GameCanvas(flecs::world& w) : tgui::CanvasSFML(), world(w) {
onKeyPress(
[this](const tgui::Event::KeyEvent& event){
Tag::InputCtrl* ctrl = world.lookup("Tag::GameScene").get_mut<Tag::InputCtrl>();
ctrl->ctrl->onKeyEvent(event);
}
);
}
GameCanvas::Ptr GameCanvas::create(flecs::world& w){
auto canvas = std::make_shared<GameCanvas>(w);
return canvas;
}
void GameCanvas::keyPressed (const tgui::Event::KeyEvent& event){
std::cout << "GameCanvas::keyPressed()" << std::endl;
tgui::CanvasSFML::keyPressed(event);
onKeyPress.emit(this, event);
}
bool GameCanvas::leftMousePressed(tgui::Vector2f pos){
tgui::CanvasSFML::leftMousePressed(pos);
return false;
}
void GameCanvas::leftMouseReleased(tgui::Vector2f pos){
tgui::CanvasSFML::leftMouseReleased(pos);
std::cout << "GameCanvas::leftMouseReleased()" << std::endl;
}
void GameCanvas::rightMousePressed(tgui::Vector2f pos){
tgui::CanvasSFML::rightMousePressed(pos);
}
void GameCanvas::rightMouseReleased(tgui::Vector2f pos){
tgui::CanvasSFML::rightMouseReleased(pos);
}
void GameCanvas::mouseMoved(tgui::Vector2f pos){
tgui::CanvasSFML::mouseMoved(pos);
Tag::InputCtrl* ctrl = world.lookup("Tag::GameScene").get_mut<Tag::InputCtrl>();
ctrl->ctrl->onMouseMoveEvent(pos - getPosition());
}
bool GameCanvas::canHandleKeyPress(const tgui::Event::KeyEvent& event){
std::cout << "GameCanvas::canHandleKeyPress()" << std::endl;
return true;
}
tgui::Signal& GameCanvas::getSignal(tgui::String signalName) {
std::cout << "GameCanvas::getSignal()" << std::endl;
if (signalName == onKeyPress.getName())
return onKeyPress;
else
return tgui::CanvasSFML::getSignal(std::move(signalName));
}
The log messages from isFocused, keyPressed, canHandleKeyPress and getSignal never get called but the messages/actions in mouseMoved and leftMouseReleased do.
The canvas hierarchy is: GameCanvas > VerticalLayout["LeftPanel"] > HorizontalLayout["HudRoot"] > RootContainer.

I've looked through the forums, the tutorial pages on signals and custom widgets, and TGUI source code, especially EditBox, but I just can't seem to figure out how to get the canvas to receive keyboard input.
If someone could point out what I'm doing wrong in the above code or provide me some basic code showing how to create a custom canvas with keyPress functionality, I'd greatly appreciate it.
Cheers