Created
April 1, 2025 08:16
-
-
Save thewhodidthis/62365a2ab95946af2da27d854a142d78 to your computer and use it in GitHub Desktop.
Helps disable Mac keyboards by eating up key presses
This file contains hidden or 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
| import Cocoa | |
| @main | |
| class AppDelegate: NSObject, NSApplicationDelegate { | |
| var eventTap: CFMachPort? | |
| func applicationDidFinishLaunching(_ aNotification: Notification) { | |
| let eventMask = (1 << CGEventType.keyDown.rawValue) | (1 << CGEventType.keyUp.rawValue) | |
| guard let tap = CGEvent.tapCreate( | |
| tap: .cgSessionEventTap, | |
| place: .headInsertEventTap, | |
| options: .defaultTap, | |
| eventsOfInterest: CGEventMask(eventMask), | |
| callback: { _, _, _, _ in | |
| return nil | |
| }, | |
| userInfo: nil | |
| ) else { | |
| print("Failed to create event tap.") | |
| NSApp.terminate(nil) | |
| return | |
| } | |
| eventTap = tap | |
| let runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, tap, 0) | |
| CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, .commonModes) | |
| CGEvent.tapEnable(tap: tap, enable: true) | |
| } | |
| func applicationWillTerminate(_ aNotification: Notification) { | |
| if let tap = eventTap { | |
| CGEvent.tapEnable(tap: tap, enable: false) | |
| } | |
| } | |
| func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool { | |
| return true | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment