Sorehead Posted April 19 Share Posted April 19 Does anyone know of a workflow we can use to adjust keyboard brightness manually? Looking to use hotkeys to adjust instead of clicking my way through Apple menus. Link to comment
vitor Posted April 19 Share Posted April 19 You can do it by following these instructions, except you’ll use a Hotkey Trigger instead of a Remote Trigger. And instead of setting the text to fast-forward or play-pause, use keyboard-brightness-up and keyboard-brightness-down. zeitlings 1 Link to comment
Sorehead Posted April 19 Author Share Posted April 19 (edited) Thanks for the quick response, vitor. I think I did what your instructions describe, but the keyboard brightness is not increasing. In fact, I don't think anything happens. Before trying the Workflow, I set my keyboard brightness to no brightness, then tried to increase brightness using my Hotkey. I entered the Command-1 Hotkey several times. I also tried holding Command-1 down for a long time. Neither works. Could you look at the attached pictures and the code I'm using to see what might be wrong? If important, I'm using Ventura 13.3.1 on MBP 16-in 2021. And here's the code I'm running: var query = CommandLine.arguments[1] print(query, terminator: "")import Quartz let supportedKeys: [String: UInt32] = [ "screen-brightness-up": 2, "screen-brightness-down": 3, "keyboard-brightness-up": 21, "keyboard-brightness-down": 22, "play-pause": 16, "fast-forward": 17, "rewind": 18, "volume-mute-toggle": 7, "volume-up": 0, "volume-down": 1, ] func pressKey(_ key: UInt32) { func keyDown(_ down: Bool) { let flags = NSEvent.ModifierFlags(rawValue: (down ? 0xa00 : 0xb00)) let data1 = Int((key << 16) | (down ? 0xa00 : 0xb00)) let ev = NSEvent.otherEvent( with: NSEvent.EventType.systemDefined, location: NSPoint(x: 0, y: 0), modifierFlags: flags, timestamp: 0, windowNumber: 0, context: nil, subtype: 8, data1: data1, data2: -1 ) let cev = ev?.cgEvent cev?.post(tap: CGEventTapLocation.cgSessionEventTap) } keyDown(true) keyDown(false) } pressKey(supportedKeys[CommandLine.arguments[1]]!) Edited April 19 by Sorehead Link to comment
vitor Posted April 19 Share Posted April 19 1 hour ago, Sorehead said: var query = CommandLine.arguments[1] print(query, terminator: "") Remove that part. That’s the placeholder that’s there by default, you need to replace all of it with the given code. Link to comment
Sorehead Posted April 19 Author Share Posted April 19 Wow, you're fast, vitor. I appreciate your time and effort in helping me, sincerely. I removed the two lines of code you directed me to erase. The workflow still isn't working. I ran Alfred's Debugger and received the following...any thoughts? [19:30:16.056] Logging Started... [19:30:16.724] Keyboard Brightness[Hotkey] Processing complete [19:30:16.735] Keyboard Brightness[Hotkey] Passing output 'keyboard-brightness-up' to Run Script [19:30:16.866] ERROR: Keyboard Brightness[Run Script] /Users/Sorehead/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/E06D20DF-E0EE-4C54-83BB-6E0A4C5614DB:15:17: error: cannot find 'NSEvent' in scope let flags = NSEvent.ModifierFlags(rawValue: (down ? 0xa00 : 0xb00)) ^~~~~~~ /Users/Sorehead/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/E06D20DF-E0EE-4C54-83BB-6E0A4C5614DB:17:14: error: cannot find 'NSEvent' in scope let ev = NSEvent.otherEvent( ^~~~~~~ /Users/Sorehead/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/E06D20DF-E0EE-4C54-83BB-6E0A4C5614DB:18:13: error: cannot find 'NSEvent' in scope with: NSEvent.EventType.systemDefined, ^~~~~~~ /Users/Sorehead/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/E06D20DF-E0EE-4C54-83BB-6E0A4C5614DB:19:17: error: cannot find 'NSPoint' in scope location: NSPoint(x: 0, y: 0), ^~~~~~~ /Users/Sorehead/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/E06D20DF-E0EE-4C54-83BB-6E0A4C5614DB:23:16: error: 'nil' requires a contextual type context: nil, ^ /Users/Sorehead/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/E06D20DF-E0EE-4C54-83BB-6E0A4C5614DB:28:19: error: type of expression is ambiguous without more context let cev = ev?.cgEvent ~~~~^~~~~~~ /Users/Sorehead/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/E06D20DF-E0EE-4C54-83BB-6E0A4C5614DB:29:20: error: cannot find 'CGEventTapLocation' in scope cev?.post(tap: CGEventTapLocation.cgSessionEventTap) ^~~~~~~~~~~~~~~~~~ [19:30:20.181] Logging Stopped. Link to comment
vitor Posted April 20 Share Posted April 20 You’ve deleted too much, now (import Quartz). Like so. Sorehead 1 Link to comment
Sorehead Posted April 20 Author Share Posted April 20 vitor, you solved my problem! Thank you so much Link to comment
zeitlings Posted April 20 Share Posted April 20 Why isn't this a proper workflow yet? This is great 😄. I've been looking for one that controls keyboard brightness myself a while ago. I'll whip one up real quick. Link to comment
vitor Posted April 20 Share Posted April 20 (edited) 40 minutes ago, zeitlings said: Why isn't this a proper workflow yet? Because I have thorough plans for the code but there’s a catch: while it works as a script, once you compile it runs but doesn’t perform the action. Why? That’s what I’ve been trying to ascertain with Apple. I want the solution to be fast, robust, and have no dependencies. Which theoretically should be possible. Edited April 20 by vitor Link to comment
zeitlings Posted April 20 Share Posted April 20 21 minutes ago, vitor said: Because I have thorough plans for the code but there’s a catch: while it works as a script, once you compile it runs but doesn’t perform the action. Why? That’s what I’ve been trying to ascertain with Apple. I want the solution to be fast, robust, and have no dependencies. Which theoretically should be possible. Have you checked if it is a sandbox thing that has to be (and can be) set to expose events to the system? Perhaps there's also a difference when trying to build it via SPM vs. Xcode. Why that is, I don't know, but I had different behaviours before. The script version works fast and stable enough for me at the moment 🤷♂️. I'll make it available as a workflow for now, at least until your replacement is ready. Link to comment
vitor Posted April 20 Share Posted April 20 Sometimes all it takes is going back after a while and looking at something with fresh eyes (and the accumulated knowledge you’ve collected since, I guess). Turns out it may be a case of it running too fast. I’m going to run some tests and may have something for when 5.1 is out as stable. Link to comment
vitor Posted April 26 Share Posted April 26 Alfred 5.1 is out, and indeed there is something new to show. In the Extras section of Automation Tasks there is now a group to simulate keyboard inputs with support for all the keys in the code above. Link to comment
zeitlings Posted April 26 Share Posted April 26 Nice! FYI, I had to click on "Reinstall Tasks" before they showed up in the Extras section. Before that, the Modal said "Automation Tasks are up to date". Is this the intended behavior, or should there be a hint that new stuff is available? Link to comment
vitor Posted April 26 Share Posted April 26 Are you on Alfred 5.1 already? If so the whole process is now easier, and Automation Tasks can even be kept up-to-date automatically if you check the first checkbox. I recommend activating both! Link to comment
zeitlings Posted April 26 Share Posted April 26 3 hours ago, vitor said: Are you on Alfred 5.1 already? I mean this window here, where only the button "Reinstall Tasks" was visible instead of "Update Tasks". What I wanted to point out is that it didn't show me that an update was available. I only mention it in case this could be an indicator of a problem with the propagation of the updates. Maybe it is nothing and they would have appeared sooner or later 😄 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now