mirror of
https://github.com/FBukovina/Atoll.git
synced 2026-10-27 15:14:33 +00:00
48 lines
No EOL
2.1 KiB
Swift
48 lines
No EOL
2.1 KiB
Swift
//
|
||
// SystemKeyObserver.swift
|
||
// DynamicIsland
|
||
//
|
||
// Adapted from TheBoringWorker-HUD KeyPressObserver.swift
|
||
// Created by GitHub Copilot on 06/09/25.
|
||
//
|
||
// ⚠️ DEPRECATED: This class is non-functional as it extends NSApplication
|
||
// but is never used as the main app class. Event monitoring has been moved
|
||
// to SystemChangesObserver using NSEvent.addGlobalMonitorForEvents.
|
||
//
|
||
|
||
import Cocoa
|
||
|
||
class SystemKeyObserver: NSApplication {
|
||
static let volumeChanged = Notification.Name("DynamicIsland.volumeChanged")
|
||
static let brightnessChanged = Notification.Name("DynamicIsland.brightnessChanged")
|
||
static let keyboardIlluminationChanged = Notification.Name("DynamicIsland.keyboardIlluminationChanged")
|
||
|
||
override func sendEvent(_ event: NSEvent) {
|
||
if event.type == .systemDefined && event.subtype.rawValue == 8 {
|
||
let keyCode = ((event.data1 & 0xFFFF0000) >> 16)
|
||
let keyFlags = (event.data1 & 0x0000FFFF)
|
||
// Get the key state. 0xA is KeyDown, OxB is KeyUp
|
||
let keyState = ((keyFlags & 0xFF00) >> 8) == 0xA
|
||
let keyRepeat = keyFlags & 0x1
|
||
mediaKeyEvent(key: Int32(keyCode), state: keyState, keyRepeat: Bool(truncating: keyRepeat as NSNumber))
|
||
}
|
||
|
||
super.sendEvent(event)
|
||
}
|
||
|
||
func mediaKeyEvent(key: Int32, state: Bool, keyRepeat: Bool) {
|
||
// Only send events on KeyDown. Without this check, these events will happen twice
|
||
if state {
|
||
switch key {
|
||
case NX_KEYTYPE_SOUND_DOWN, NX_KEYTYPE_SOUND_UP, NX_KEYTYPE_MUTE:
|
||
NotificationCenter.default.post(name: SystemKeyObserver.volumeChanged, object: self)
|
||
case NX_KEYTYPE_BRIGHTNESS_UP, NX_KEYTYPE_BRIGHTNESS_DOWN:
|
||
NotificationCenter.default.post(name: SystemKeyObserver.brightnessChanged, object: self)
|
||
case NX_KEYTYPE_ILLUMINATION_DOWN, NX_KEYTYPE_ILLUMINATION_UP:
|
||
NotificationCenter.default.post(name: SystemKeyObserver.keyboardIlluminationChanged, object: self)
|
||
default:
|
||
break
|
||
}
|
||
}
|
||
}
|
||
} |