Skip to content

Commit

Permalink
fix GetKeyState while spamming Send
Browse files Browse the repository at this point in the history
while Send-like operations take place, the list of pressed down keys was falsely not updated, which leads to wrong results when a lot is happening in loops

ref #26
  • Loading branch information
phil294 committed Dec 19, 2022
1 parent 6da4328 commit 93562b7
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/cmd/x11/keyboard/input.cr
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ class Cmd::X11::Keyboard::Input < Cmd::Base
end

buf = ""
listener = thread.runner.display.register_key_listener do |key_event, keysym, char| # TODO: inconsistency: KeyEvent/char vs. KeyCombination.key_name
next if key_event.type != ::X11::KeyRelease
listener = thread.runner.display.register_key_listener do |key_event, keysym, char, is_paused| # TODO: inconsistency: KeyEvent/char vs. KeyCombination.key_name
next if is_paused
next if key_event.type != ::X11::KeyPress
end_key = end_keys.find { |k| k.keysym == keysym }
if end_key
next channel.send("EndKey:#{end_key.key_name}")
Expand Down
7 changes: 3 additions & 4 deletions src/run/display.cr
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,14 @@ module Run

# TODO: put keysym and char into key_event in callers?
private def handle_event(key_event, keysym, char)
return if @is_paused
@key_listeners.each do |sub|
spawn same_thread: true do
sub.call(key_event, keysym, char)
sub.call(key_event, keysym, char, @is_paused)
end
end
end
@key_listeners = [] of Proc(::X11::KeyEvent, UInt64, Char?, Nil)
def register_key_listener(&block : ::X11::KeyEvent, UInt64, Char? -> _)
@key_listeners = [] of Proc(::X11::KeyEvent, UInt64, Char?, Bool, Nil)
def register_key_listener(&block : ::X11::KeyEvent, UInt64, Char?, Bool -> _)
@key_listeners << block
block
end
Expand Down
7 changes: 4 additions & 3 deletions src/run/display/hotkeys.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module Run
end

def run
@runner.display.register_key_listener do |key_event, keysym, char|
handle_event(key_event, keysym, char)
@runner.display.register_key_listener do |key_event, keysym, char, is_paused|
handle_event(key_event, keysym, char, is_paused)
end
@runner.display.on_pause { pause }
@runner.display.on_suspend { resume }
Expand Down Expand Up @@ -62,7 +62,8 @@ module Run
hotkey
end

def handle_event(key_event, keysym, char)
def handle_event(key_event, keysym, char, is_paused)
return if is_paused
up = key_event.type == ::X11::KeyRelease || key_event.type == ::X11::ButtonRelease # TODO: externalize somehow because this is duplicate in various places

hotkey = @hotkeys.find do |hotkey|
Expand Down
9 changes: 5 additions & 4 deletions src/run/display/hotstrings.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ module Run
end

def run
@runner.display.register_key_listener do |key_event, keysym, char|
handle_event(key_event, keysym, char)
@runner.display.register_key_listener do |key_event, keysym, char, is_paused|
handle_event(key_event, keysym, char, is_paused)
end
end

Expand All @@ -31,8 +31,9 @@ module Run
@candidate : Hotstring? = nil
@modifier_keysyms : StaticArray(Int32, 13) = StaticArray[::X11::XK_Shift_L, ::X11::XK_Shift_R, ::X11::XK_Control_L, ::X11::XK_Control_R, ::X11::XK_Caps_Lock, ::X11::XK_Shift_Lock, ::X11::XK_Meta_L, ::X11::XK_Meta_R, ::X11::XK_Alt_L, ::X11::XK_Alt_R, ::X11::XK_Super_L, ::X11::XK_Super_R, ::X11::XK_Num_Lock]

def handle_event(key_event, keysym, char)
up = key_event.type == ::X11::KeyRelease || key_event.type == ::X11::ButtonRelease
def handle_event(key_event, keysym, char, is_paused)
return if is_paused
up = key_event.type == ::X11::KeyPress || key_event.type == ::X11::ButtonPress
return if ! up
prev_candidate = @candidate
@candidate = nil
Expand Down
2 changes: 1 addition & 1 deletion src/run/display/pressed-keys.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Run
@pressed_down_keysyms : StaticArray(UInt64, 8) = StaticArray[0_u64,0_u64,0_u64,0_u64,0_u64,0_u64,0_u64,0_u64]

def run
@runner.display.register_key_listener do |key_event, keysym, char|
@runner.display.register_key_listener do |key_event, keysym, char, is_paused|
up = key_event.type == ::X11::KeyRelease || key_event.type == ::X11::ButtonRelease

if ! up
Expand Down

0 comments on commit 93562b7

Please sign in to comment.