Swiftpack.co - Swifteroid/Observatory as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by Swifteroid.
Swifteroid/Observatory 0.5.6
Cocoa and Carbon event, notification and hotkey observing framework πŸ”­ in pure Swift πŸ’―
⭐️ 9
πŸ•“ 1 week ago
macOS linux macOS iOS
.package(url: "https://github.com/Swifteroid/Observatory.git", from: "0.5.6")


Observatory is a micro-framework for easier event, notification and hotkey management in Swift.

  • Standardised approach for event, notification and hotkey observing.
  • Simple enabling and disabling of observers.
  • Rich choice of handler signatures.
  • Handle local / global / both events.
  • Chaining support.

Observe global hotkeys.

let observer: HotkeyObserver = HotkeyObserver(active: true)
let fooHotkey: KeyboardHotkey = KeyboardHotkey(key: KeyboardKey.five, modifier: [.commandKey, .shiftKey])
let barHotkey: KeyboardHotkey = KeyboardHotkey(key: KeyboardKey.six, modifier: [.commandKey, .shiftKey])

    .add(hotkey: fooHotkey) { Swift.print("Such foo…") }
    .add(hotkey: barHotkey) { Swift.print("So bar…") }

Observe notifications, chose between plain () -> () or standard (Notification) -> () signatures.

let observer: NotificationObserver = NotificationObserver(active: true)
let observee: AnyObject = NSObject()

    .add(name: Notification.Name("foo"), observee: observee) { Swift.print("Foo captain!") }
    .add(names: [Notification.Name("bar"), Notification.Name("baz")], observee: observee) { Swift.print("Yes \($0.name)!") }

Observe events, like with notifications, you can chose between plain () -> () and standard local (NSEvent) -> NSEvent? or global (NSEvent) -> () signatures.

let observer: EventObserver = EventObserver(active: true)

    .add(mask: .any, handler: { Swift.print("Any is better than none.") })
    .add(mask: [.leftMouseDown, .leftMouseUp], handler: { Swift.print("It's a \($0.type) event!") })

Checkout Observatory.playground for information and examples.


Stars: 9
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.


Release Notes

2 years ago

This is a breaking change release:

  • HotkeyCommand and HotkeyCenter was replaced with Shortcut and ShortcutCenter, see unit tests for details and examples.
  • Renamed error enums.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics