Skip to content

Commit

Permalink
add support for mouse buttons in Send such as Send, {LButton}, also…
Browse files Browse the repository at this point in the history
… works with `up`/`down` and count number
  • Loading branch information
phil294 committed Feb 27, 2023
1 parent 70adc99 commit 9c145a6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/cmd/x11/keyboard/control-send.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ class Cmd::X11::Keyboard::ControlSend < Cmd::Base
Cmd::X11::Window::Util.match(thread, match_conditions, empty_is_last_found: true, a_is_active: true) do |win|
thread.runner.display.pause do
win.clear_active_modifiers thread.runner.display.x_do.active_modifiers
thread.parse_key_combinations_to_charcodemap(keys) do |key_map, pressed|
win.keys_raw key_map, pressed: pressed, delay: 0
thread.parse_key_combinations_to_charcodemap(keys) do |key_map, pressed, mouse_button|
if ! mouse_button
win.keys_raw key_map, pressed: pressed, delay: 0
end
end
end
end
Expand Down
12 changes: 10 additions & 2 deletions src/cmd/x11/keyboard/send.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ class Cmd::X11::Keyboard::Send < Cmd::Base
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
thread.parse_key_combinations_to_charcodemap(args[0]) do |key_map, pressed|
thread.runner.display.x_do.keys_raw key_map, pressed: pressed, delay: 0
thread.parse_key_combinations_to_charcodemap(args[0]) do |key_map, pressed, mouse_button|
if mouse_button
if pressed
thread.runner.display.x_do.mouse_down mouse_button
else
thread.runner.display.x_do.mouse_up mouse_button
end
else
thread.runner.display.x_do.keys_raw key_map, pressed: pressed, delay: 0
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/run/thread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ module Run
def parse_key_combinations(str, *, implicit_braces = false)
Util::AhkString.parse_key_combinations(str, @runner.settings.escape_char, implicit_braces: implicit_braces)
end
def parse_key_combinations_to_charcodemap(str, &block : Array(XDo::LibXDo::Charcodemap), Bool -> _)
def parse_key_combinations_to_charcodemap(str, &block : Array(XDo::LibXDo::Charcodemap), Bool, XDo::Button? -> _)
Util::AhkString.parse_key_combinations_to_charcodemap(str, @runner.settings.escape_char, @runner.display.adapter.as(Run::X11), &block) # TODO: type cast etc
end

Expand Down
23 changes: 19 additions & 4 deletions src/util/ahk-string.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,29 @@ class Util::AhkString
def self.parse_key_combinations_to_charcodemap(str, escape_char : Char, x11 : Run::X11, implicit_braces = false)
self.parse_key_combinations(str, escape_char, implicit_braces: implicit_braces) do |combo|
key_map = XDo::LibXDo::Charcodemap.new
key_map.code = x11.keysym_to_keycode(combo.keysym)
key_map.modmask = combo.modifiers
mouse_button : XDo::Button? = nil
if combo.keysym < 10
mouse_button = case combo.keysym
when 1 then XDo::Button::Left
when 2 then XDo::Button::Middle
when 3 then XDo::Button::Right
when 4 then XDo::Button::ScrollUp
when 5 then XDo::Button::ScrollDown
when 6 then XDo::Button::ScrollLeft
when 7 then XDo::Button::ScrollRight
when 8 then XDo::Button::Button8
when 9 then XDo::Button::Button9
end
else
key_map.code = x11.keysym_to_keycode(combo.keysym)
key_map.modmask = combo.modifiers
end
combo.repeat.times do
if combo.down || ! combo.up
yield [key_map], true
yield [key_map], true, mouse_button
end
if combo.up || ! combo.down
yield [key_map], false
yield [key_map], false, mouse_button
end
end
end
Expand Down
10 changes: 8 additions & 2 deletions tests.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
; Right now, only commands that can be easily tested in 1-2 lines are tested.
;;;;;;;;;;;;;;;;;;;;;;

N_TESTS = 39
N_TESTS = 40

GoSub, run_tests
if tests_run != %N_TESTS%
Expand Down Expand Up @@ -549,4 +549,10 @@ hotkey, +s, off
hotkey, +S, hotkey_shift_s
runwait, xdotool key shift+s
expect = hotkey shift_s lowercase,hotkey_shift_s_success,1
gosub assert
gosub assert

Send, {LButton}
sleep 10
expect = send {lbutton},gui_button_clicked_success,1
gosub assert
gui_button_clicked_success =

0 comments on commit 9c145a6

Please sign in to comment.