Jump to content

Keyboard Brightness


Recommended Posts

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. 

 

Screenshot2023-04-19at16_58_03.png.1021d3c3f7d1bb330f051942c8a1e9ff.png

 

 

Screenshot2023-04-19at17_00_06.thumb.png.73f02a3a2d125c475a1a91d6dc84e8dd.png

 

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 by Sorehead
Link to comment

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
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 by vitor
Link to comment
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

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
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 😄

 Automation Tasks options dialog

Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...