Created
September 10, 2021 00:45
-
-
Save williamtoader/1e0a5bc2277ffc9d2943ffb64279314a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// macKeyboardBacklight.swift | |
// | |
// Created by Wiliam Toader on 10.09.2021. | |
// Solution suggested by user Alex293 on github issue https://github.com/pirate/mac-keyboard-brightness/issues/1 | |
import Foundation | |
import IOKit | |
import AppKit | |
func up() -> Void { | |
let code = NX_KEYTYPE_ILLUMINATION_UP | |
let event1 = NSEvent.otherEvent(with: .systemDefined, location: NSPoint.zero, modifierFlags: NSEvent.ModifierFlags(rawValue: 0xa00), timestamp: 0, windowNumber: 0, context: nil, subtype: 8, data1: (Int((code << 16 as Int32) | (0xa << 8 as Int32))), data2: -1) | |
event1?.cgEvent?.post(tap: .cghidEventTap) | |
let event2 = NSEvent.otherEvent(with: .systemDefined, location: NSPoint.zero, modifierFlags: NSEvent.ModifierFlags(rawValue: 0xb00), timestamp: 0, windowNumber: 0, context: nil, subtype: 8, data1: (Int((code << 16 as Int32) | (0xb << 8 as Int32))), data2: -1) | |
event2?.cgEvent?.post(tap: .cghidEventTap) | |
} | |
func down() -> Void { | |
let code = NX_KEYTYPE_ILLUMINATION_DOWN | |
let event1 = NSEvent.otherEvent(with: .systemDefined, location: NSPoint.zero, modifierFlags: NSEvent.ModifierFlags(rawValue: 0xa00), timestamp: 0, windowNumber: 0, context: nil, subtype: 8, data1: (Int((code << 16 as Int32) | (0xa << 8 as Int32))), data2: -1) | |
event1?.cgEvent?.post(tap: .cghidEventTap) | |
let event2 = NSEvent.otherEvent(with: .systemDefined, location: NSPoint.zero, modifierFlags: NSEvent.ModifierFlags(rawValue: 0xb00), timestamp: 0, windowNumber: 0, context: nil, subtype: 8, data1: (Int((code << 16 as Int32) | (0xb << 8 as Int32))), data2: -1) | |
event2?.cgEvent?.post(tap: .cghidEventTap) | |
} | |
let cmdUp = CommandLine.arguments.firstIndex(where: {$0 == "-up"}) ?? 0 | |
let cmdDown = CommandLine.arguments.firstIndex(where: {$0 == "-down"}) ?? 0 | |
let nr = cmdUp > 0 ? cmdUp + 1 : (cmdDown > 0 ? cmdDown + 1 : 0) | |
for _ in 1...(Int(CommandLine.arguments[nr]) ?? 1) { | |
if(cmdUp > 0) { up() } | |
else if(cmdDown > 0) {down()} | |
} | |
if(CommandLine.arguments.count < 2) { | |
print("A simple program to adjust keyboard backlight\n") | |
print("Arguments:\n") | |
print("-up\t<number>\t\tIncrements brightness by number of steps.\n") | |
print("-down\t<number>\t\tDecrements brightness by number of steps.\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment