objective-c – 使用事件点击消耗OSX鼠标触控板事件

objective-c – 使用事件点击消耗OSX鼠标触控板事件,第1张

概述我正在尝试添加一个事件陷阱来启用/禁用我的魔术触控板上的事件.我认为这是直截了当的,即注册一个事件陷阱,并在需要时通过返回NULL来丢弃该事件.我的想法是使用pad进行一些特定的,耗时的数据输入,输入数据的应用程序是第三方的,所以我不能只是添加代码来实现我想要的.所以我想我会监视系统事件,然后通过一堆CGEventCreateKeyboardEvents发送所需的输入. 问题是返回null似乎没有 我正在尝试添加一个事件陷阱来启用/禁用我的魔术触控板上的事件.我认为这是直截了当的,即注册一个事件陷阱,并在需要时通过返回NulL来丢弃该事件.我的想法是使用pad进行一些特定的,耗时的数据输入,输入数据的应用程序是第三方的,所以我不能只是添加代码来实现我想要的.所以我想我会监视系统事件,然后通过一堆CGEventCreateKeyboardEvents发送所需的输入.

问题是返回null似乎没有丢弃事件,更多的调查表明这不仅限于来自触控板的那些,而且也是我的默认USB鼠标.

我的代码如下.以下是我希望不能移动鼠标,如果我改变(A)使用kCGEventScrollWheel或kCGEventleftMouseDragged然后消耗事件,即滚动或左btn拖动不会发生.这是否意味着并非所有事件都可以丢弃?希望我在这里遗漏一些明显的东西

#define case_print(a) case a: printf("%s - %d\n",#a,a); break;  CGEventRef eventOccurred(CGEventTapProxy proxy,CGEventType type,CGEventRef event,voID* refcon) {    int subType =  CGEventGetIntegerValueFIEld(event,kCGMouseEventSubtype);    if (type == NSEventTypeGesture || subType == NX_SUBTYPE_MOUSE_touch) {        printf("touchpad\n");        switch(type) {                case_print(kCGEventNull)                case_print(kCGEventleftMouseDown)                case_print(kCGEventleftMouseUp)                case_print(kCGEventRightmouseDown)                case_print(kCGEventRightmouseUp)                case_print(kCGEventMouseMoved)                case_print(kCGEventleftMouseDragged)                case_print(kCGEventRightmouseDragged)                case_print(kCGEventScrollWheel)                case_print(kCGEventOtherMouseDown)                case_print(kCGEventOtherMouseUp)                case_print(kCGEventOtherMouseDragged)                case_print(kCGEventTapDisabledByTimeout)                case_print(kCGEventTapDisabledByUserinput)                case_print(NSEventTypeGesture)                case_print(NSEventTypeMagnify)                case_print(NSEventTypeSwipe)                case_print(NSEventTypeRotate)                case_print(NSEventTypeBeginGesture)                case_print(NSEventTypeEndGesture)            default:                printf("default: %d\n",type);                break;            }        event = NulL;    }  else {        if (type == kCGEventMouseMoved) {  // (A)            printf("discarding mouse event");            event = NulL;        }    }    return event;}CFMachPortRef createEventTap() {      CGEventMask eventMask = NSAnyEventMask;    if (!AXAPIEnabled() && !AXIsProcesstrusted()) {         printf("axAPI not enabled");    }     return CGEventTapCreate(kCGHIDEventTap,kCGheadInsertEventTap,kCGEventTapOptionDefault,eventMask,eventOccurred,NulL); }int main (int argc,const char * argv[]) {    CFMachPortRef tap = createEventTap();    if (tap) {        CFRunLoopSourceRef rl = CFMachPortCreateRunLoopSource(kcfAllocatorDefault,tap,0);        CFRunLoopAddSource(CFRunLoopGetMain(),rl,kcfRunLoopCommonModes);        CGEventTapEnable(tap,true);        CFRunLoopRun();        printf("Tap created.\n");        sleep(-1);    } else {        printf("Failed!\n");    }    return 0;}

注意,虽然我认为辅助功能选项不会影响除键盘事件以外的任何内容,但不会输出“未启用axAPI”.

顺便说一下,我已经看过一些关于如何从触摸板上获取事件的类似帖子,没有什么适用于丢弃它们(除了返回null应该工作).

解决方法 如果您的tap是被动的,则返回NulL将使事件流不受影响.从 CGEventTapCallBack参考文档:

“If the event tap is an passive
Listener,your callback function may
return the event that is passed in,or
NulL. In either case,the event stream
is not affected.”

但是,看起来您的点按是活动的.因此,返回的NulL应该删除该事件.您是否考虑过修改事件以使 *** 作无效?

另请注意,调用CGEventTapCreate需要root用户权限才能拦截所有事件.您的进程是否以root身份运行?

总结

以上是内存溢出为你收集整理的objective-c – 使用事件点击消耗OSX鼠标/触控板事件全部内容,希望文章能够帮你解决objective-c – 使用事件点击消耗OSX鼠标/触控板事件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1034050.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存