A small macOS menu bar app that temporarily blocks the built-in keyboard and trackpad (and any other hardware keyboard or mouse) and dims the display to zero. One shortcut turns the lock on, the same shortcut turns it off.
It is useful when you want to wipe down the keyboard and screen without typing into whatever is open, or when you want the machine to keep running with the lid open while nothing can be pressed by accident.
The whole app is one Swift file with no dependencies.
- macOS 13.0 or later.
- Xcode Command Line Tools (
xcode-select --install) forswiftcandcodesign. - Accessibility and Input Monitoring permission for the app, granted in System Settings > Privacy & Security. The app cannot block input without them.
- Dimming has been used on the built-in display of Apple Silicon MacBooks. See Limitations for external displays.
git clone https://github.com/direksethi/mac-input-lock.git
cd mac-input-lock
./build.shThis compiles main.swift with swiftc, assembles build/InputLock.app, and
signs it ad hoc. Nothing outside the repository folder is touched.
./install.shThis does three things:
- Copies
build/InputLock.appto/Applications/InputLock.app. - Copies
com.direksethi.inputlock.plistto~/Library/LaunchAgents/, so the app starts at login. - Loads the LaunchAgent and starts the app.
On first launch macOS asks for Accessibility access. Enable Input Lock under both Accessibility and Input Monitoring in System Settings > Privacy & Security. The app retries every 3 seconds until the permission is in place, so no restart is needed.
If you prefer not to install a login item, skip install.sh and open the app
directly:
open build/InputLock.appTo remove everything install.sh added:
./uninstall.shInput Lock lives in the menu bar as a padlock icon: open when the lock is off, closed when it is on.
| Action | How |
|---|---|
| Turn the lock on or off | Cmd+Shift+L, or Activate Lock in the menu |
| Panic exit | Press Esc 10 times quickly |
| Quit | Quit Input Lock in the menu (only reachable while unlocked) |
While the lock is on:
- Key presses, modifier changes, mouse and trackpad clicks, movement, drags, scrolling, tablet events, and the media, volume and brightness keys are dropped before any app sees them.
- The brightness of every active display the app can control is saved and set to zero.
When the lock turns off, the saved brightness is restored.
Blocking all input is risky if the way out fails, so there are three:
- Cmd+Shift+L is checked before any blocking and always toggles the lock.
- Esc 10 times. Each press must come within 3 seconds of the previous one. The tenth press releases the lock.
- Timeout. The lock releases itself 30 minutes after it was turned on.
If macOS disables the event tap (it does this when a tap is slow to respond), the app re-enables it on the next notification.
Event tap. The app creates a CGEventTap at the HID level
(.cghidEventTap, inserted at the head). The callback returns nil for an
event to drop it, or returns the event to let it continue. The tap listens for
key down, key up, flags changed, all mouse button, move and drag events,
scroll wheel, tablet events, and system-defined events (type 14, which carry
the media, volume and brightness keys).
What passes through. Only events that come from hardware are blocked. The
callback reads the event's source state ID: hardware events report 1 (the
HID system state). Events posted by software, such as automation or remote
control tools, report a different value and are passed through even while the
lock is on. When the lock is off every event passes, except Cmd+Shift+L, which
the app consumes as its hotkey.
Dimming through a private framework. On Apple Silicon the public IOKit
display brightness keys do not drive the built-in panel. The app instead loads
/System/Library/PrivateFrameworks/DisplayServices.framework with dlopen
and calls DisplayServicesGetBrightness and DisplayServicesSetBrightness,
looked up with dlsym.
This is a private, undocumented API. Apple can change or remove it in any macOS release, and an app that uses it is not eligible for the Mac App Store. The symbols are resolved at runtime rather than linked, so if the framework or a symbol is missing the app still launches and still blocks input; it just does not dim the display.
Permissions. At launch the app calls AXIsProcessTrustedWithOptions with
the prompt option and IOHIDRequestAccess for listen access. If the tap cannot
be created, the app retries on a timer, and choosing Activate Lock shows a
dialog with a button that opens the Privacy settings.
- Cmd+Shift+L is taken system-wide while the app is running. Apps that use the same shortcut will not receive it.
- The shortcut, the Esc count, the Esc window and the 30 minute timeout are
constants in
main.swift. There is no settings window. - The brightness keys are blocked along with everything else while the lock is on. The display stays at zero until the lock is released.
- Dimming depends on the private DisplayServices calls succeeding for a given display. A display for which they fail (typically an external monitor) is left at its current brightness.
- Only events delivered through the event tap are blocked. Input that does not arrive as one of the listed event types, such as the power button, is not affected.
- Software-generated events are deliberately not blocked, so this is not a security lock. It does not lock the session or require a password to exit.
- The build is signed ad hoc. macOS ties the Accessibility and Input Monitoring grants to the signature, so after rebuilding you may need to remove and re-add Input Lock in Privacy & Security.
- If brightness is changed by other software while the lock is on, the value saved at activation is still what gets restored.
| File | Purpose |
|---|---|
main.swift |
The whole app |
Info.plist |
Bundle metadata; LSUIElement keeps it out of the Dock |
com.direksethi.inputlock.plist |
LaunchAgent that starts the app at login |
build.sh |
Compile and sign into build/InputLock.app |
install.sh |
Copy to /Applications and install the LaunchAgent |
uninstall.sh |
Remove the app and the LaunchAgent |
MIT. See LICENSE.