2

I need simulate the pressing of the media/function keys on the mac keyboard. Everything from brightness to volume up/down.

Is this possible? And if so how? Ive read a bunch about how to trigger on their press, but not this.

If possible, id like the translucent icons to still come up on the screen as you change things.

Adam Meyer
  • 1,505
  • 1
  • 19
  • 28
  • 1
    I ask this question: Why? Why trigger the keypresses instead of using events and such? Similar to this with brightness: http://stackoverflow.com/questions/3239749/programmatically-change-mac-display-brightness – ars265 Aug 27 '13 at 15:58
  • I would like, if possible to have the brightness icon come up when triggered like it does when a user presses the brightness button. I have it it in code just as it is in that answer and the icon does not come up. – Adam Meyer Aug 28 '13 at 16:52
  • I see, then may I suggest http://stackoverflow.com/questions/10459085/cocoa-simulate-macbook-upper-keys-multimedia-keys which will lead you here: http://weblog.rogueamoeba.com/2007/09/29/ . Don't know if it will help but it may put you on track. – ars265 Aug 28 '13 at 20:16

1 Answers1

2

Try this out in Swift:

// Simulate illumination up
let code = NX_KEYTYPE_ILLUMINATION_UP
let event1 = NSEvent.otherEvent(with: .systemDefined, location: NSPoint.zero, modifierFlags: NSEventModifierFlags(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: NSEventModifierFlags(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)



// Simulate illumination down
let code = NX_KEYTYPE_ILLUMINATION_DOWN
let event1 = NSEvent.otherEvent(with: .systemDefined, location: NSPoint.zero, modifierFlags: NSEventModifierFlags(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: NSEventModifierFlags(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)

(credit goes to @Alex293)

This is from our discussion on ways to programmatically control the keyboard brightness with: https://github.com/pirate/mac-keyboard-brightness

Nick Sweeting
  • 5,364
  • 5
  • 27
  • 37