在Swift中使用CGEventTapCreate麻烦和参数 [英] Using CGEventTapCreate Trouble with parameters in Swift

查看:130
本文介绍了在Swift中使用CGEventTapCreate麻烦和参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Swift中使用CGEventTapCreate等来捕获按键.我想这样做,以便当我的孩子使用Mac程序时,我可以限制哪些键可以执行某些操作.我非常感谢我需要启用辅助技术才能使CGEventTap正常工作

I am trying to trap keypresses using a CGEventTapCreate etc in Swift. I want to do this so that when my toddler is using a program the Mac, I can limit which keys do something. I appreciate that I need to enable assistive technology to get CGEventTap to work

我无法理解在Swift中需要做些什么才能在调用中获取最终参数.从本质上讲,我不了解石英事件

I am having trouble understanding what I need to do in Swift to get the final parameter in the call. Essentially, I do not understand part of the documentation at quartz event documentation

特别是

refcon:指向用户定义数据的指针.该指针被传递到 在callback参数中指定的回调函数.

refcon: A pointer to user-defined data. This pointer is passed into the callback function specified in the callback parameter.

这是我的代码缺少通话中的最后一个参数

Here is my code missing the final parameter from the call

@IBAction func interceptKeyboard(sender: NSButton) {


    CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, CGEventMask((kCGEventKeyDown) | (kCGEventKeyUp)), MyCallBack: kCGHIDEventTap, )

}

func MyCallBack(myLocation:CGEventTapLocation) -> Void{
    //do Nowt
}

推荐答案

前一阵子,但是我认为当您查看CGEventTapCallback的类型时,您会看到它是一个CFunctionPointer:

Bit of a while ago, but I think when you look at the type of CGEventTapCallback, you see that it is a CFunctionPointer:

typealias CGEventTapCallBack = CFunctionPointer<((CGEventTapProxy, 
CGEventType, CGEvent!, UnsafeMutablePointer<Void>) -> Unmanaged<CGEvent>!)>

目前您无法在Swift中创建这些函数,因此您必须使用Objective-C,在其中创建一个函数,然后可以在Swift中调用等效函数,请参见此处:

At the moment you can't create these in Swift, so you have to dip into objective-c, create a function there, which can then call an equivalent function in Swift, see here:

用于CFunctionPointer到Swift闭包的Objective-C包装器

我最终用Objective-C编写了所有这些东西.

I ended up writing the whole of this stuff in Objective-C...

这篇关于在Swift中使用CGEventTapCreate麻烦和参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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