DEV Community

Pradeep Jangid
Pradeep Jangid

Posted on

🧹 How to Temporarily Disable Your Mac Keyboard While Cleaning (Safe & Reversible)

We’ve all been there — you start wiping down your MacBook keyboard and suddenly a storm of random windows open, messages get typed, and maybe even a system dialog or two pops up. 😅

Wouldn’t it be nice if you could lock your keyboard temporarily while you clean it — and then unlock it with a single command?

Good news: you can. In this post, I’ll show you a safe, reversible, code-only solution that works on macOS without uninstalling drivers or messing with system files.

🚀 Why Not Just Smash the Power Button?

Some people shut down their Mac, but:

That interrupts your workflow.

You can’t see smudges properly with the screen off.

And, let’s be honest — we love quick hacks.

This method lets you disable only the keyboard (while keeping your Mac awake), so you can wipe away dust, crumbs, and fingerprints without worrying about accidental keypresses.

🛠️ The Trick: Block All Key Events with Python

We’ll use a tiny Python script with Apple’s Quartz Event Taps API (via PyObjC).
When running, the script intercepts all key events and simply drops them.

👉 Stop the script, and your keyboard comes back to life instantly.

📦 Step 1 — Install the Dependency

Open Terminal and install the Quartz bridge:

python3 -m pip install --user pyobjc-framework-Quartz

📄 Step 2 — Save the Script

Create a file called disable_keyboard.py:

#!/usr/bin/env python3
# Disable Mac keyboard while cleaning
# Run: python3 disable_keyboard.py
# Stop: Ctrl+C or kill the process

from Quartz import (
    CGEventTapCreate, kCGHIDEventTap, kCGHeadInsertEventTap,
    kCGEventTapOptionDefault, kCGEventKeyDown, kCGEventKeyUp,
    kCGEventFlagsChanged, CGEventTapEnable
)
from Quartz import CFRunLoopGetCurrent, CFMachPortCreateRunLoopSource, CFRunLoopAddSource, CFRunLoopRun

def tap_callback(proxy, type_, event, refcon):
    return None  # Block every key event

mask = (1 << kCGEventKeyDown) | (1 << kCGEventKeyUp) | (1 << kCGEventFlagsChanged)
tap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, mask, tap_callback, None)

if not tap:
    print("⚠️ Accessibility permission required! Enable it in System Settings > Privacy & Security > Accessibility.")
    exit(1)

source = CFMachPortCreateRunLoopSource(None, tap, 0)
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, 0)
CGEventTapEnable(tap, True)

print("🧹 Keyboard disabled. Clean away! Press Ctrl+C to restore.")
CFRunLoopRun()
Enter fullscreen mode Exit fullscreen mode

🔑 Step 3 — Grant Accessibility Permission

On first run, macOS will block the script unless you grant permissions:

Go to System Settings → Privacy & Security → Accessibility.

Add Terminal (or iTerm, or Python.app) to the list.

Toggle it on.

Now re-run the script and voilà — the keyboard is locked.

⏹️ Step 4 — Unlock the Keyboard

When you’re done cleaning:

Press Ctrl + C in Terminal, OR

In another terminal, run:

pkill -f disable_keyboard.py

Keyboard input is instantly restored. ✅

🎯 Pro Tips

Want one-click control? Wrap this script in an Automator app so you can toggle it from your dock.

If you’re a power user, check out Karabiner-Elements, which lets you define profiles to disable/enable your keyboard.

Always keep a mouse or trackpad handy — since this script blocks all key events, keyboard shortcuts won’t work until you stop it.

📝 Conclusion

Cleaning your Mac doesn’t need to feel like defusing a bomb 💣 every time you brush over a key.
With this tiny Python hack, you can disable the keyboard temporarily, clean safely, and restore everything instantly.

Top comments (0)