Skip to content

Commit

Permalink
unwarp
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev1an committed Jan 21, 2018
1 parent 688888f commit ff2e390
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Magic Drumpad/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.1</string>
<string>1.1.1</string>
<key>CFBundleVersion</key>
<string>5</string>
<string>7</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.music</string>
<key>LSMinimumSystemVersion</key>
Expand Down
12 changes: 10 additions & 2 deletions Magic Drumpad/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,26 @@ class ViewController: NSViewController {
}
}

var mousePosition = CGPoint.zero

func lockMouse() {
CGDisplayHideCursor(.init(0))
CGAssociateMouseAndMouseCursorPosition(0)
mousePosition = view.window?.convertToGlobal( NSEvent.mouseLocation() ) ?? .zero
CGWarpMouseCursorPosition(CGPoint(
x: NSEvent.mouseLocation().x,
y: view.window?.convertToGlobal(view.convert(lockButton.frame.origin, to: nil))?.y ?? 100
y: view.window?.convertToGlobal(
view.window?.convertToScreen(
view.convert(lockButton.frame, to: nil)
).origin ?? .zero
)?.y ?? 100
))
lockButton.keyEquivalent = escape
lockButton.title = "Press escape to unlock mouse"
}

func unlockMouse() {
CGWarpMouseCursorPosition(mousePosition)
CGDisplayShowCursor(.init(0))
CGAssociateMouseAndMouseCursorPosition(1)
lockButton.title = "Lock mouse"
Expand All @@ -153,7 +161,7 @@ extension NSWindow {
if let screen = screen {
return CGPoint(
x: point.x,
y: screen.frame.maxY - convertToScreen(NSRect(origin: point, size: .zero)).origin.y
y: screen.frame.maxY - point.y
)
} else {
return nil
Expand Down

0 comments on commit ff2e390

Please sign in to comment.