加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

可可 – 如何在OS X应用程序中使用Swift监听全局热键?

发布时间:2020-12-14 06:02:11 所属栏目:百科 来源:网络整理
导读:我试图有一个处理程序在我的Mac OS X应用程序中用Swift编写的全局(系统范围)热键组合,但我只是无法找到适当的文档。我读过,我不得不在一些遗留的Carbon API为它,是没有更好的方法?你能给我一些概念证明Swift代码吗?提前致谢! 自从Swift 2.0以来,你现
我试图有一个处理程序在我的Mac OS X应用程序中用Swift编写的全局(系统范围)热键组合,但我只是无法找到适当的文档。我读过,我不得不在一些遗留的Carbon API为它,是没有更好的方法?你能给我一些概念证明Swift代码吗?提前致谢!
自从Swift 2.0以来,你现在可以传递一个函数指针到C API。
var gMyHotKeyID = EventHotKeyID()
    gMyHotKeyID.signature = OSType("swat".fourCharCodeValue)
    gMyHotKeyID.id = UInt32(keyCode)

    var eventType = EventTypeSpec()
    eventType.eventClass = OSType(kEventClassKeyboard)
    eventType.eventKind = OSType(kEventHotKeyPressed)

    // Install handler.
    InstallEventHandler(GetApplicationEventTarget(),{(nextHanlder,theEvent,userData) -> OSStatus in
            var hkCom = EventHotKeyID()
            GetEventParameter(theEvent,EventParamName(kEventParamDirectObject),EventParamType(typeEventHotKeyID),nil,sizeof(EventHotKeyID),&hkCom)

            /// Check that hkCom in indeed your hotkey ID and handle it.
        },1,&eventType,nil)

    // Register hotkey.
    let status = RegisterEventHotKey(UInt32(keyCode),UInt32(modifierKeys),gMyHotKeyID,GetApplicationEventTarget(),&hotKeyRef)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读