Skip to content

About

macOS menu bar app that blocks keyboard and trackpad input and dims the screen, for cleaning your Mac.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Input Lock

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.

Requirements

  • macOS 13.0 or later.
  • Xcode Command Line Tools (xcode-select --install) for swiftc and codesign.
  • 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.

Build

git clone https://github.com/direksethi/mac-input-lock.git
cd mac-input-lock
./build.sh

This compiles main.swift with swiftc, assembles build/InputLock.app, and signs it ad hoc. Nothing outside the repository folder is touched.

Install

./install.sh

This does three things:

  1. Copies build/InputLock.app to /Applications/InputLock.app.
  2. Copies com.direksethi.inputlock.plist to ~/Library/LaunchAgents/, so the app starts at login.
  3. 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.app

To remove everything install.sh added:

./uninstall.sh

Usage

Input 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.

Panic exit and safety nets

Blocking all input is risky if the way out fails, so there are three:

  1. Cmd+Shift+L is checked before any blocking and always toggles the lock.
  2. Esc 10 times. Each press must come within 3 seconds of the previous one. The tenth press releases the lock.
  3. 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.

How it works

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.

Limitations

  • 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.

Files

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

License

MIT. See LICENSE.

About

macOS menu bar app that blocks keyboard and trackpad input and dims the screen, for cleaning your Mac.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages