Atoll/DynamicIsland/managers/SystemKeyObserver.swift
2025-09-06 21:54:19 +05:30

48 lines
No EOL
2.1 KiB
Swift
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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
}
}
}
}