Skip to content

Commit

Permalink
Fix KeysDown not being reset to 'false' when losing focus
Browse files Browse the repository at this point in the history
SFML doesn't send "KeyReleased" event as GLFW and SDL do, so we need to handle FocusLost event to allow for alt-tabbing.
SFML doesn't send the event of "Alt" begin released, so ImGui-SFML thinks that it's still pressed.

Fixes #147
  • Loading branch information
eliasdaler committed Apr 9, 2021
1 parent 64093df commit 5c3027a
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions imgui-SFML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,15 @@ void ProcessEvent(const sf::Event& event) {
}
break;
case sf::Event::KeyPressed: // fall-through
case sf::Event::KeyReleased:
io.KeysDown[event.key.code] = (event.type == sf::Event::KeyPressed);
break;
case sf::Event::KeyReleased: {
int key = event.key.code;
IM_ASSERT(key >= 0 && key < IM_ARRAYSIZE(io.KeysDown));
io.KeysDown[key] = (event.type == sf::Event::KeyPressed);
io.KeyCtrl = event.key.control;
io.KeyAlt = event.key.alt;
io.KeyShift = event.key.shift;
io.KeySuper = event.key.system;
} break;
case sf::Event::TextEntered:
// Don't handle the event for unprintable characters
if (event.text.unicode < ' ' || event.text.unicode == 127) {
Expand All @@ -329,9 +335,20 @@ void ProcessEvent(const sf::Event& event) {
}

switch (event.type) {
case sf::Event::LostFocus:
case sf::Event::LostFocus: {
// reset all input - SFML doesn't send KeyReleased
// event when window goes out of focus
ImGuiIO& io = ImGui::GetIO();
for (int i = 0; i < IM_ARRAYSIZE(io.KeysDown); ++i) {
io.KeysDown[i] = false;
}
io.KeyCtrl = false;
io.KeyAlt = false;
io.KeyShift = false;
io.KeySuper = false;

s_windowHasFocus = false;
break;
} break;
case sf::Event::GainedFocus:
s_windowHasFocus = true;
break;
Expand Down Expand Up @@ -384,12 +401,6 @@ void Update(const sf::Vector2i& mousePos, const sf::Vector2f& displaySize, sf::T
}
}

// Update Ctrl, Shift, Alt, Super state
io.KeyCtrl = io.KeysDown[sf::Keyboard::LControl] || io.KeysDown[sf::Keyboard::RControl];
io.KeyAlt = io.KeysDown[sf::Keyboard::LAlt] || io.KeysDown[sf::Keyboard::RAlt];
io.KeyShift = io.KeysDown[sf::Keyboard::LShift] || io.KeysDown[sf::Keyboard::RShift];
io.KeySuper = io.KeysDown[sf::Keyboard::LSystem] || io.KeysDown[sf::Keyboard::RSystem];

#ifdef ANDROID
#ifdef USE_JNI
if (io.WantTextInput && !s_wantTextInput) {
Expand Down

0 comments on commit 5c3027a

Please sign in to comment.