Fix Issue #78: Middle mouse click no longer sends 'C' keyboard event

The bug was caused by accessing event.key.code on a mouse event without
checking the event type first. Since SFML uses a union for events, this
read garbage data. The middle mouse button value (2) coincidentally matched
the keyboard 'C' value (2), causing the spurious keyboard event.

Fixed by adding event type check before accessing key-specific fields.
Only keyboard events (KeyPressed/KeyReleased) now trigger key callbacks.

Test added to verify middle clicks no longer generate keyboard events.

Closes #78
This commit is contained in:
John McCardle 2025-07-03 19:42:32 -04:00
commit 59e6f8d53d
2 changed files with 89 additions and 1 deletions

View file

@ -229,7 +229,8 @@ void GameEngine::processEvent(const sf::Event& event)
std::string name = currentScene()->action(actionCode);
currentScene()->doAction(name, actionType);
}
else if (currentScene()->key_callable)
else if (currentScene()->key_callable &&
(event.type == sf::Event::KeyPressed || event.type == sf::Event::KeyReleased))
{
currentScene()->key_callable->call(ActionCode::key_str(event.key.code), actionType);
}