-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add option to listen to mouse clicks
- Loading branch information
Showing
4 changed files
with
220 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
func bridge<T : AnyObject>(obj : T) -> UnsafeRawPointer { | ||
return UnsafeRawPointer(Unmanaged.passUnretained(obj).toOpaque()) | ||
} | ||
|
||
func bridge<T : AnyObject>(ptr : UnsafeRawPointer) -> T { | ||
return Unmanaged<T>.fromOpaque(ptr).takeUnretainedValue() | ||
} | ||
|
||
func bridgeRetained<T : AnyObject>(obj : T) -> UnsafeRawPointer { | ||
return UnsafeRawPointer(Unmanaged.passRetained(obj).toOpaque()) | ||
} | ||
|
||
func bridgeTransfer<T : AnyObject>(ptr : UnsafeRawPointer) -> T { | ||
return Unmanaged<T>.fromOpaque(ptr).takeRetainedValue() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import Foundation | ||
|
||
enum MouseEventType { | ||
case click | ||
case drag | ||
} | ||
|
||
enum MouseButton: String, CustomStringConvertible { | ||
case left | ||
case right | ||
case center | ||
case other | ||
|
||
var description: String { | ||
get { | ||
return self.rawValue | ||
} | ||
} | ||
} | ||
|
||
struct RawMouseEvent { | ||
let eventType: CGEventType | ||
let button: MouseButton | ||
let point: CGPoint | ||
|
||
init(eventType: CGEventType, button: MouseButton, point: CGPoint) { | ||
self.eventType = eventType | ||
self.button = button | ||
self.point = point | ||
} | ||
} | ||
|
||
struct MouseEvent { | ||
let eventType: MouseEventType | ||
let button: MouseButton | ||
let startPoint: CGPoint | ||
let endPoint: CGPoint | ||
|
||
init(eventType: MouseEventType, button: MouseButton, startPoint: CGPoint, endPoint: CGPoint) { | ||
self.eventType = eventType | ||
self.button = button | ||
self.startPoint = startPoint | ||
self.endPoint = endPoint | ||
} | ||
} | ||
|
||
class MouseEventProcessor { | ||
var events: [RawMouseEvent] = [] | ||
|
||
func consumeEvent(type: CGEventType, event: CGEvent) -> MouseEvent? { | ||
let button = getMouseButton(type: type, event: event) | ||
let rawEvent = RawMouseEvent(eventType: type, button: button, point: event.location) | ||
|
||
switch type { | ||
case .leftMouseUp, .rightMouseUp, .otherMouseUp: | ||
switch events.last?.eventType { | ||
case .leftMouseDown, .rightMouseDown, .otherMouseDown: | ||
return MouseEvent(eventType: .click, button: button, startPoint: events.first!.point, endPoint: event.location) | ||
case .leftMouseDragged, .rightMouseDragged, .otherMouseDragged: | ||
return MouseEvent(eventType: .drag, button: button, startPoint: events.first!.point, endPoint: event.location) | ||
default: | ||
events.append(rawEvent) | ||
} | ||
events = [] | ||
default: | ||
events.append(rawEvent) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
private func getMouseButton(type: CGEventType, event: CGEvent) -> MouseButton { | ||
var button: MouseButton = .other | ||
|
||
switch type { | ||
case .leftMouseDown, .leftMouseUp, .leftMouseDragged: | ||
button = .left | ||
case .rightMouseDown, .rightMouseUp, .rightMouseDragged: | ||
button = .right | ||
case .otherMouseDown, .otherMouseUp, .otherMouseDragged: | ||
let buttonNumber = event.getIntegerValueField(.mouseEventButtonNumber) | ||
switch UInt32(buttonNumber) { | ||
case CGMouseButton.left.rawValue: | ||
button = .left | ||
case CGMouseButton.right.rawValue: | ||
button = .right | ||
case CGMouseButton.center.rawValue: | ||
button = .center | ||
default: | ||
button = .other | ||
} | ||
default: | ||
button = .other | ||
} | ||
|
||
return button | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters