Skip to content

Commit

Permalink
Add SetMouseDelay and its built-in var A_MouseDelay
Browse files Browse the repository at this point in the history
(see other commit at `SetKeyDelay` on how this affects current scripts)
  • Loading branch information
phil294 committed Jul 20, 2023
1 parent 775320b commit ed57b0a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 12 deletions.
10 changes: 5 additions & 5 deletions docs/index.html
Expand Up @@ -509,7 +509,7 @@ <h2>Table of contents </h2>
<a class="tbd" href="#SetDefaultMouseSpeed.htm">SetDefaultMouseSpeed</a>
</li>
<li>
<a class="tbd" href="#SetMouseDelay.htm">SetMouseDelay</a>
<a href="#SetMouseDelay.htm">SetMouseDelay</a>
</li>
</ul>
</li>
Expand Down Expand Up @@ -1724,7 +1724,7 @@ <h4 class="calibre25"><a id="Variables.htm__settings" href="#Variables.htm__sett
</tr>
<tr class="calibre3">
<td class="calibre4">A_MouseDelay</td>
<td class="calibre4 tbd"><a id="Variables.htm__MouseDelay" href="#Variables.htm__MouseDelay" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">#</a> The current delay set by <a href="#SetMouseDelay.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">SetMouseDelay</a> (always decimal, not hex).</td>
<td class="calibre4"><a id="Variables.htm__MouseDelay" href="#Variables.htm__MouseDelay" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">#</a> The current delay set by <a href="#SetMouseDelay.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">SetMouseDelay</a> (always decimal, not hex).</td>
</tr>
<tr class="calibre3">
<td class="calibre4">A_DefaultMouseSpeed</td>
Expand Down Expand Up @@ -2546,7 +2546,7 @@ <h2 class="calibre9"><span class="calibre23">The "Last Found" Window </span></h2
<td height="16" class="calibre4">Sets the delay that will occur after each keystroke sent by <a href="#Send.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">Send</a> or <a href="#ControlSend.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">ControlSend</a>.</td>
</tr>
<tr class="calibre3">
<td height="16" class="tbd calibre4"><a href="#SetMouseDelay.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">SetMouseDelay</a></td>
<td height="16" class="calibre4"><a href="#SetMouseDelay.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">SetMouseDelay</a></td>
<td height="16" class="calibre4">Sets the delay that will occur after each mouse movement or click.</td>
</tr>
<tr class="calibre3">
Expand Down Expand Up @@ -10809,7 +10809,7 @@ <h2 id="Examples">Examples</h2>
<p class="calibre8">SetDefaultMouseSpeed, 0 ; Move the mouse instantly like AutoIt2.</p>
</div>
</div>
<div class="calibreMain tbd">
<div class="calibreMain">
<div class="calibreEbookContent">
<a id="SetMouseDelay.htm" href="#SetMouseDelay.htm">#</a> <h2 class="calibre17">SetMouseDelay</h2>
<hr size="2" class="calibre24" />
Expand All @@ -10834,7 +10834,7 @@ <h2 id="Examples">Examples</h2>
<p class="calibre8"> </p>
<p class="calibre8"><strong class="calibre14">Remarks</strong></p>
<p class="calibre8">A short delay (sleep) is automatically and invisibly done after every mouse movement or click generated by <a href="#MouseMove.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">MouseMove</a>, <a href="#MouseClick.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">MouseClick</a>, and <a href="#MouseClickDrag.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">MouseClickDrag</a>. This is done to improve the reliability of scripts because a window sometimes can't keep up with a rapid flood of mouse events.</p>
<p class="calibre8">Due to the granularity of the OS's time-keeping system, delays might be rounded up to the nearest multiple of 10. For example, a delay between 1 and 10 (inclusive) is equivalent to 10 on Windows XP (and probably NT &amp; 2k).</p>
<p class="calibre8 rm">Due to the granularity of the OS's time-keeping system, delays might be rounded up to the nearest multiple of 10. For example, a delay between 1 and 10 (inclusive) is equivalent to 10 on Windows XP (and probably NT &amp; 2k).</p>
<p class="calibre8">A delay of 0 internally executes a Sleep(0), which yields the remainder of the script's timeslice to any other process that may need it. If there is none, Sleep(0) will not sleep at all. By contrast, a delay of -1 will never sleep.</p>
<p class="calibre8">If unset, the default delay is 10.</p>
<p class="calibre8">The built-in variable <strong class="calibre14">A_MouseDelay</strong> contains the current setting.</p>
Expand Down
15 changes: 8 additions & 7 deletions src/cmd/x11/mouse/mouse-click.cr
Expand Up @@ -24,9 +24,6 @@ class Cmd::X11::Mouse::MouseClick < Cmd::Base
if thread.settings.coord_mode_mouse == ::Run::CoordMode::RELATIVE
x, y = Cmd::X11::Window::Util.coord_relative_to_screen(thread, x, y)
end
else
x = current_x
y = current_y
end
count = args[3]?.try &.to_i? || 1
up = down = false
Expand All @@ -36,14 +33,18 @@ class Cmd::X11::Mouse::MouseClick < Cmd::Base
end
relative = args[6]?.try &.downcase == "r"
thread.runner.display.pause do
if relative
thread.runner.display.x_do.move_mouse x, y
else
thread.runner.display.x_do.move_mouse x, y, screen
if x && y
if relative
thread.runner.display.x_do.move_mouse x, y
else
thread.runner.display.x_do.move_mouse x, y, screen
end
sleep thread.settings.mouse_delay.milliseconds if thread.settings.mouse_delay > -1
end
count.times do
thread.runner.display.x_do.mouse_down button if ! up
thread.runner.display.x_do.mouse_up button if ! down
sleep thread.settings.mouse_delay.milliseconds if thread.settings.mouse_delay > -1
end
end
end
Expand Down
1 change: 1 addition & 0 deletions src/cmd/x11/mouse/mouse-move.cr
Expand Up @@ -17,5 +17,6 @@ class Cmd::X11::Mouse::MouseMove < Cmd::Base
thread.runner.display.x_do.move_mouse x, y, screen
end
end
sleep thread.settings.mouse_delay.milliseconds if thread.settings.mouse_delay > -1
end
end
8 changes: 8 additions & 0 deletions src/cmd/x11/mouse/set-mouse-delay.cr
@@ -0,0 +1,8 @@
# SetMouseDelay, Delay
class Cmd::X11::Mouse::SetMouseDelay < Cmd::Base
def self.min_args; 1 end
def self.max_args; 1 end
def run(thread, args)
thread.settings.mouse_delay = args[0].to_i if args[0].to_i?
end
end
3 changes: 3 additions & 0 deletions src/run/thread.cr
Expand Up @@ -21,6 +21,7 @@ module Run
property detect_hidden_windows = false
property key_delay = 10
property key_press_duration = -1
property mouse_delay = 10
property ahk_x11_track_performance = false
end

Expand Down Expand Up @@ -230,6 +231,8 @@ module Run
@settings.detect_hidden_windows ? "On" : "Off"
when "a_keydelay"
@settings.key_delay.to_s
when "a_mousedelay"
@settings.mouse_delay.to_s
when "a_linenumber"
(@stack.last.line_no + 1).to_s
when "a_thislabel"
Expand Down
1 change: 1 addition & 0 deletions tests.ahk
Expand Up @@ -6,6 +6,7 @@
N_TESTS = 70

SetKeyDelay, 0
SetMouseDelay, 0

GoSub, run_tests
if tests_run != %N_TESTS%
Expand Down

0 comments on commit ed57b0a

Please sign in to comment.