Skip to content

Commit

Permalink
Send/SendRaw: Restore modifiers after sending
Browse files Browse the repository at this point in the history
  • Loading branch information
phil294 committed Jul 11, 2023
1 parent a66d77a commit 134303c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/cmd/x11/keyboard/send-raw.cr
Expand Up @@ -5,14 +5,16 @@ class Cmd::X11::Keyboard::SendRaw < Cmd::Base
def run(thread, args)
txt = args[0]
thread.runner.display.pause do # to prevent hotkey from triggering other hotkey or itself
thread.runner.display.x_do.clear_active_modifiers thread.runner.display.x_do.active_modifiers
active_modifiers = thread.runner.display.x_do.active_modifiers
thread.runner.display.x_do.clear_active_modifiers active_modifiers
if (hotkey = thread.hotkey) && hotkey.key_name.size == 1 && txt.includes?(hotkey.key_name)
# TODO: duplicate code as in send.cr
key_map_hotkey_up = XDo::LibXDo::Charcodemap.new
key_map_hotkey_up.code = hotkey.keycode
thread.runner.display.x_do.keys_raw [key_map_hotkey_up], pressed: false, delay: 0
end
thread.runner.display.x_do.type txt
thread.runner.display.x_do.set_active_modifiers active_modifiers
end
end
end
15 changes: 11 additions & 4 deletions src/cmd/x11/keyboard/send.cr
Expand Up @@ -4,7 +4,8 @@ class Cmd::X11::Keyboard::Send < Cmd::Base
def self.max_args; 1 end
def run(thread, args)
thread.runner.display.pause do # to prevent hotkey from triggering other hotkey or itself
thread.runner.display.x_do.clear_active_modifiers thread.runner.display.x_do.active_modifiers
active_modifiers = thread.runner.display.x_do.active_modifiers
thread.runner.display.x_do.clear_active_modifiers active_modifiers
thread.parse_key_combinations_to_charcodemap(args[0]) do |key_map, pressed, mouse_button, combo|
if mouse_button
if pressed
Expand All @@ -17,13 +18,19 @@ class Cmd::X11::Keyboard::Send < Cmd::Base
# https://github.com/jordansissel/xdotool/issues/210 (see also hotkeys.cr)
# Not a super great solution because for every key up/down combo of the hotkey, this will
# *always* send a second key up event now, but oh well it works
key_map_hotkey_up = XDo::LibXDo::Charcodemap.new
key_map_hotkey_up.code = hotkey.keycode
thread.runner.display.x_do.keys_raw [key_map_hotkey_up], pressed: false, delay: 0
hotkey_key_up = XDo::LibXDo::Charcodemap.new
hotkey_key_up.code = hotkey.keycode
thread.runner.display.x_do.keys_raw [hotkey_key_up], pressed: false, delay: 0
end
thread.runner.display.x_do.keys_raw key_map, pressed: pressed, delay: 0
end
end
# We can't use `x_do.set_active_modifiers active_modifiers` here because while it would be
# the preferred method, it also does some `xdo_mouse_down()` internally, based on current input state.
# And when we've sent an `{LButton}` down+up event in the keys, the x11 server might still report for the button
# to be pressed down when the up event hasn't been processed yet by it, resulting in wrong input state and
# effectively a wrong button pressed again by libxdo.
thread.runner.display.x_do.keys_raw active_modifiers, pressed: false, delay: 0
end
end
end

0 comments on commit 134303c

Please sign in to comment.