Skip to content

Commit

Permalink
Fix: Previous key state for repeat count in X11
Browse files Browse the repository at this point in the history
Currently, as we've spoken in aseprite#69
@dcap has correctly pointed out that my patch has some problems with
shift key modifier - and he was correct, I've skipped an important thing
looking into this problem.

This patch adds a std::set to hold all currently pressed keys - if a key
got pressed second time, std::set will hold a key symbol of that key, if
an event for that key comes for a second time (and there were no
KeyRelease event coming for the event in-between) - then it means the key is
being pressed down, so we set repeat count to 1. If KeyRelease event
came for the key symbol that was previously pressed, then we basically
erase it from std::set.
  • Loading branch information
tetektoza authored and dacap committed Nov 3, 2023
1 parent d5a724c commit a3553d4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
21 changes: 14 additions & 7 deletions os/x11/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include <array>
#include <map>
#include <set>

#define KEY_TRACE(...)
#define EVENT_TRACE(...)
Expand Down Expand Up @@ -115,6 +116,10 @@ std::map<::Window, WindowX11*> g_activeWindows;
// for the XInput devices.
Time g_lastXInputEventTime = 0;

// Set of all currently pressed keys - used during processing KeyPress or KeyRelease
// event
std::set<KeySym> g_pressedKeys;

bool is_mouse_wheel_button(int button)
{
return (button == Button4 || button == Button5 ||
Expand Down Expand Up @@ -1042,17 +1047,19 @@ void WindowX11::processX11Event(XEvent& event)
KEY_TRACE("Xutf8LookupString %s\n", &buf[0]);
}

// Check if the key has been pressed,
// and if yes - check if it's the same one key
// as in the previous event. If it's the same one
// - it means key is being held, so set repeat to 1.

// Check if the key has been pressed, and if yes - check what's
// the previous state of the key symbol - if it was previously
// pressed set repeat count of 1, if it wasn't pressed previously
// it will set repeat count to 0
if (event.type == KeyPress) {
if (keysym == m_pressedKeySym)
if (g_pressedKeys.find(keysym) != g_pressedKeys.end())
ev.setRepeat(1);
m_pressedKeySym = keysym;
else
g_pressedKeys.insert(keysym);
}
else
m_pressedKeySym = 0;
g_pressedKeys.erase(keysym);

// Key event used by the input method (e.g. when the users
// presses a dead key).
Expand Down
2 changes: 0 additions & 2 deletions os/x11/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ class WindowX11 : public Window {
bool m_resizable = false;
bool m_transparent = false;

KeySym m_pressedKeySym = 0;

// Double-click info
Event::MouseButton m_doubleClickButton;
base::tick_t m_doubleClickTick;
Expand Down

0 comments on commit a3553d4

Please sign in to comment.