Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PowerGuard

A macOS menu-bar switch that stops the Touch ID / power button from locking your Mac.

Flip it on when you don't want a stray press to interrupt you, flip it off when you're done. No administrator password, no background daemon, no kernel extension.

Українською


Why

On a MacBook, a short press of the Touch ID (power) button locks the screen instantly. If someone keeps bumping it — a kid, a cat, a partner reaching across the desk — you lose your session over and over. PowerGuard turns that behaviour off from a switch next to the clock.

Holding the button for ~10 seconds still forces a shutdown. That is implemented in hardware by the SMC and is never affected by this app, so you cannot lock yourself out.

How it works

The interesting part is that the short press does not put the Mac to sleep. pmset -g log records no sleep event at all when the button is pressed. The lock is performed by loginwindow:

loginwindow[412] [com.apple.loginwindow.logging] -[LWDefaultScreenLockUI closeAuthAndReset:]
WindowManager[611] <ScaleScreenLockPhasedTransitionInternalModifier direction=unlock ...>

That behaviour is governed by exactly one preference:

Domain Key Type
com.apple.loginwindow DisableScreenLockImmediate Boolean

loginwindow runs as the logged-in user, so the user domain is the correct one and no administrator privileges are required. PowerGuard reads and writes it through CFPreferences.

The equivalent by hand:

# Button no longer locks the Mac
defaults write com.apple.loginwindow DisableScreenLockImmediate -bool true

# Back to the default behaviour
defaults delete com.apple.loginwindow DisableScreenLockImmediate

PowerGuard is just a tidy switch around that, with the current state read back from the system rather than remembered by the app.

What does not work

Two plausible approaches were implemented and tested end to end on Apple Silicon (M1, macOS 26.5.2). Both had no effect. They are documented here so nobody has to repeat them.

1. IOKit Sleep On Power Button

kIOPMSettingSleepOnPowerButtonKey (IOPM.h) is shown by pmset -g as Sleep On Power Button. pmset has no argument to write it; it is set through IOPMSetPMPreferences, which is exported by IOKit but declared only in the private IOPMLibPrivate.h, and requires root.

The value does get written — verified as 0 for every power source — and nothing changes. It governs sleep, and there is no sleep involved in this scenario.

2. hidutil key remapping

The button is present in the HID system, but not as an IOHIDDevice — it is an IOHIDEventService named AppleM68Buttons sitting on top of an IOPlatformDevice, which is why it never shows up when you enumerate HID devices:

"ButtonUsagePairs" = (51539607600)   →  0xC00000030 = Consumer page 0x0C, usage 0x30 (Power)
"DeviceUsagePairs" = ({"DeviceUsagePage"=12,"DeviceUsage"=1})

hidutil list shows it, and it accepts a remap:

hidutil property --matching '{"PrimaryUsagePage":12,"PrimaryUsage":1,"Built-In":1}' \
  --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0xC00000030,
                             "HIDKeyboardModifierMappingDst":0x700000000}]}'

The UserKeyMapping property lands in the IORegistry and reads back correctly — but the parsed form, HIDKeyboardModifierMappingPairs, stays empty. The driver stores the property and ignores it. The button keeps working exactly as before.

Requirements

  • macOS 13 or later
  • Command Line Tools for Xcode (only to build — a full Xcode install is not needed)

Install

Download PowerGuard.zip from Releases, unzip it, and move PowerGuard.app to your Applications folder.

The app is ad-hoc signed, not notarized, so Gatekeeper will refuse the first launch. Either right-click the app and choose Open, or clear the quarantine flag:

xattr -dr com.apple.quarantine /Applications/PowerGuard.app

Build from source

git clone https://github.com/voltergared03/PowerGuard.git
cd PowerGuard
./build.sh

The result is build/PowerGuard.app. Pass --desktop to also drop a ready-to-use copy on your Desktop with the quarantine flag already cleared:

./build.sh --desktop

There is no Xcode project and no package manifest — build.sh invokes swiftc directly, writes Info.plist, renders the icon from an SF Symbol, and ad-hoc signs the bundle.

Usage

The app lives only in the menu bar (LSUIElement), with no Dock icon and no window.

  • Left click the icon to open the menu and use the switch.
  • A filled green lock means the button is disabled; a dimmed open lock means it behaves normally.
  • Launch at login registers the app through SMAppService.
  • Toggle with a single click makes a left click flip the switch directly; the menu then opens with a right click.

The menu always shows the value read back from the system, so it stays honest if something else changes the setting.

Project layout

Sources/App/
  main.swift                  NSApplication entry point, accessory activation policy
  StatusMenuController.swift  status item, menu, icon state
  ScreenLockGuard.swift       the actual mechanism (CFPreferences)
  ToggleSwitch.swift          custom green switch (NSSwitch follows the system accent colour)
  SwitchRowView.swift         the menu row hosting the switch
  Preferences.swift           app settings and launch-at-login
  LegacyCleanup.swift         removes leftovers from the two approaches that did not work
Tools/makeicon.swift          renders the app icon
build.sh                      builds and packages the .app

Source comments are in Ukrainian.

License

MIT

About

macOS menu-bar switch that stops the Touch ID / power button from locking your Mac — no admin password, no daemon

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages