Skip to content

Commit

Permalink
Dramatically improve reliability of clicking (and drag+drop).
Browse files Browse the repository at this point in the history
In lieu of a gnarly hack, Scoot is brought to the background *before* any
mouse-related operation (like a click) is issued. This way, if the target
window was focused before Scoot was invoked, the target window will regain
focus ahead of the mouse-related operation.

Closes #28.
  • Loading branch information
mjrusso committed Dec 10, 2022
1 parent 573ec9c commit 1e1933f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 33 deletions.
19 changes: 12 additions & 7 deletions Scoot/KeyboardInputWindow+Keyboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,16 @@ extension KeyboardInputWindow {
}

switch (character, isHoldingDownLeftMouseButton, modifiers) {
// Scoot is brought to the background before issuing any mouse-related
// operation (like clicking). If the target window was focused ahead of
// invoking Scoot, it will regain focus _before_ clicking, which
// significantly improves reliability. For more context, see
// <https://github.com/mjrusso/scoot/issues/28>.
case ("\r", false, []):
appDelegate?.bringToBackground()
mouse.click(button: .left)
return
case ("\r", false, _), ("[", false, _), ("]", false, _):
// The user is pressing the left mouse button while holding down
// one or more modifier keys, or pressing the middle or right mouse
// button (with or without modifiers). In any of these cases, a
// system-provided context menu will likely pop up. Scoot can't
// take control of the mouse cursor when a context menu is active,
// so it is a better user experience to bring Scoot to the
// background before issuing the click.
appDelegate?.bringToBackground()

// Note that if a modifier is being held, it will "pass through" to
Expand All @@ -87,15 +86,21 @@ extension KeyboardInputWindow {
}
return
case ("\r", true, _):
appDelegate?.bringToBackground()
mouse.notifyDrag(.left)
mouse.release(.left)
isHoldingDownLeftMouseButton = false
return
case ("=", false, _):
appDelegate?.bringToBackground()
mouse.pressDown(.left)
isHoldingDownLeftMouseButton = true
// Automatically bring Scoot to the foreground, so the user can
// continue their drag operation.
appDelegate?.bringToForeground()
return
case ("\\", false, _):
appDelegate?.bringToBackground()
mouse.doubleClick(button: .left)
return
default:
Expand Down
27 changes: 1 addition & 26 deletions Scoot/Mouse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,8 @@ struct Mouse {
}

func click(button: Mouse.Button) {
// It should be possible to `pressDown()` and then `release()` (after a
// short delay), however that approach doesn't work reliably: in some
// applications (for example, when trying to click on a hyperlink in a
// browser, or in an apps that render their user interface using web
// technologies), mouse clicks don't actually register.
//
// For an unknown reason, posting two `leftMouseDown` events, followed
// by a single `leftMouseUp`, *seems* to work consistently everywhere.
// How's that for a hack?
//
// [N.B.: A previous implementation issued two clicks in a row (i.e.,
// `pressDown()`, `release()`, `pressDown()`, `release()`), and that
// worked better than one could reasonably expect. However, there were
// rare cases where two clicks would actually be registered: for example,
// in Safari, clicking the "close tab" button (the one embedded in the
// tab itself) would result in two tabs being closed. Another example:
// it wasn't possible to use Scoot to bring up it's menu bar: two clicks
// would register, and it would immediately close.]
//
// [N.B.: This question [0] on Stack Overflow ("Simulating mouse clicks
// on Mac OS X does not work for some applications") seems relevant, but
// unfortunately none of the proposed strategies seem to work.]
//
// [0]: https://stackoverflow.com/q/2369806/15304124
pressDown(button)
pressDown(button)
usleep(40000)
usleep(10000)
release(button)
}

Expand Down

0 comments on commit 1e1933f

Please sign in to comment.