如何在可可中模拟Mac Media Key [英] How to simulate mac media keys in cocoa

查看:65
本文介绍了如何在可可中模拟Mac Media Key的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要模拟Mac键盘上的媒体/功能键的按下.从亮度到音量调高/调低.

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.

如果可能的话,当您进行更改时,id之类的半透明图标仍会出现在屏幕上.

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

推荐答案

在Swift中尝试一下:

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)

(信用信息转到 @ Alex293 )

这来自我们的讨论,该内容以编程方式控制了键盘亮度: https://github.com/pirate/mac-keyboard-brightness

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

这篇关于如何在可可中模拟Mac Media Key的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆