0

I am currently working on an accessibility app whose functionality is triggered upon dragging a window with two mouse buttons pressed. My app needs to open a new borderless window and steal focus from the window that is being dragged (and preventing the window from being dragged further). My goal is to eventually resize the dragged window with the help of the focus stealing window. How do I do this?

I am using the Cocoa framework on Swift. Here's what I've tried:

NSApp.activate(ignoringOtherApps: true)
myAccessibilityWindow.makeKeyAndOrderFront(nil)

The accessibility window is ordered front as expected, but it doesn't steal focus (no mouse events are triggered within the accessibility window) until I stop dragging the triggering window. This is not desired, as I to steal the mouse focus ASAP and prevent the window from being dragged further.

Willeke
  • 14,578
  • 4
  • 19
  • 47

1 Answers1

0

In addition to making the accessibility window the key window, you must simulate a mouse up event in order to stop dragging.

let mouseLocation = NSEvent.mouseLocation
let source = CGEventSource.init(stateID: .hidSystemState)
let mouseUp = CGEvent(mouseEventSource: source, mouseType: .leftMouseUp, mouseCursorPosition: mouseLocation, mouseButton: .left)
mouseUp?.post(tap: .cghidEventTap)

Big thanks to Tarun Lalwani.