kbdlight Get it

macos-keyboard-backlight

No build step, one dependency, no network call, MIT

Your keyboard backlight, finally scriptable.

macOS gives you a slider in System Settings and two keys on the keyboard, and that is the whole interface. There is no way to reach the keyboard backlight from a shell script, a scheduled job, or a Node program. This adds the missing one.

Install

npm i -g macos-keyboard-backlight

then run kbdlight off

Star on GitHub Read the docs

Take a closer look.

This one is lit the way sun mode would have yours lit at this moment, worked out from your timezone in your browser. It types the commands into its own terminal and runs through them on its own; press one to take over, and it picks the loop back up once you are done.

An illustration, not a photograph. The backlight is hardware, so no screen recording can show it; the real thing is in the clip below.

See it run

A lit MacBook keyboard seen from above. The command kbdlight auto off && kbdlight off runs, and the white backlight drains away key by key.

The command is real and does exactly this. The MacBook is an illustration: the backlight is hardware, so no screen recording can show it.

Why this exists

Almost every part of a Mac can be driven from a script. The keyboard backlight cannot: there is no command, no flag and no supported API. If you want it off between midnight and seven, or dimmed while a recording runs, there is nothing to call.

This package resolves the private CoreBrightness interface at runtime and exposes it as a command and a library, so the backlight becomes one more thing your scripts can control.

No node-gyp. No Xcode. Nothing compiled at install time.

What you get

It follows the sun

One command puts the backlight on the sky's schedule: dark through the day, up to a level you choose after sunset, and back down at sunrise. The times move with the season on their own, so there is no clock hour to re-tune in March and again in June.

Sunrise and sunset are computed on your own machine with the NOAA solar algorithm, from coordinates your timezone already implies. No location permission, no lookup service, nothing about where you are leaving the Mac.

The ambient light sensor is a separate control and the two work together: sun mode can hand the daylight hours back to the sensor with --day auto and still hold a fixed level all evening.

An example day

sunrise
06:00
sunset
19:00
backlight now
off, it is daylight
Computed in your browser from your timezone, by the same algorithm the command uses. Nothing is sent anywhere, and nothing is stored.

The sensor and the schedule are not alternatives. They compose.

kbdlight sun on

kbdlight sun off unloads the schedule and restores the exact brightness and sensor state it found.

Install and use

Requires macOS, Apple Silicon or Intel, and Node 18 or newer.

  1. Install

    Installs a kbdlight command. Use npx instead if you only want to run it once.

    npm i -g macos-keyboard-backlight
  2. Turn it off

    Disable the ambient sensor first, otherwise it adjusts the backlight straight back.

    kbdlight auto off && kbdlight off
  3. Put it on the sun's schedule

    Dark through the day, lit after sunset, for as long as you leave it armed. Undo it with kbdlight sun off.

    kbdlight sun on
  4. Use it from Node

    The same operations as the command, returning numbers and booleans.

    node -e "require('macos-keyboard-backlight').set(0.5)"

Restore the ambient light sensor at any time with kbdlight auto on.

Made to be forked.

The whole tool is about 1,400 lines of plain JavaScript across src/ and bin/, one runtime dependency, and no build step. Nothing has to be compiled before you can read it and nothing has to be configured before you can run it.

MIT, with no contributor agreement to sign. Clone it and the same three suites that guard the published package run on your machine in under a second.

gh repo fork noluyorAbi/macos-keyboard-backlight --clone

Fork the repo Star on GitHub

Then npm install && npm test. Bugs, ideas and pull requests all go to the same place: the issue tracker is open, and so is the discussion of anything you want to build on top of it.

Questions people actually ask

The page's structured data carries these same answers, word for word, so anything quoting this page is quoting something you can read here and check.

Can the MacBook keyboard backlight be set from a script?

Not with anything Apple ships: macOS exposes a slider in System Settings and two keys on the keyboard, and no scriptable interface. kbdlight set 0.5 does it, through the private CoreBrightness framework, with no compiler involved.

Why does the keyboard brightness I set change back on its own?

Ambient auto-brightness is re-adjusting it from the light sensor, and it overrides any fixed value. Run kbdlight auto off first, then the level you set holds. Re-enable the sensor later with kbdlight auto on.

Can the keyboard backlight turn on automatically at sunset?

Yes. kbdlight sun on keeps the backlight dark through the day and lights it after sunset, then takes it back down at sunrise. The times are computed on the machine with the NOAA solar algorithm and move with the season, so there is no clock hour to re-tune.

Does it need Xcode, node-gyp or a compiler to install?

No. There is no native build step. The system calls are resolved at runtime through koffi, which ships prebuilt for both Apple Silicon and Intel, and koffi is the only runtime dependency.

Does it send location data anywhere to work out sunrise and sunset?

No. Coordinates are derived from the IANA timezone already set on the machine, which is a city name, and the times are computed locally. There is no location permission prompt, no IP lookup and no network request at any point.

Does it work with an external keyboard?

Any backlit keyboard the system reports is listed by kbdlight list, and get, set, isAuto and setAuto each take an optional keyboard ID so a single keyboard can be targeted instead of all of them.

Made by

noluyorAbi

Small tools that take one piece of friction out of a day and then stay out of the way. This one started as wanting the keyboard dark after midnight without opening System Settings.

Everything else I build, and whatever is current, lives at adatepe.dev.