Skip to content

Instantly share code, notes, and snippets.

@thewhodidthis
Created April 1, 2025 08:16
Show Gist options
  • Select an option

  • Save thewhodidthis/62365a2ab95946af2da27d854a142d78 to your computer and use it in GitHub Desktop.

Select an option

Save thewhodidthis/62365a2ab95946af2da27d854a142d78 to your computer and use it in GitHub Desktop.
Helps disable Mac keyboards by eating up key presses
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