阻止`mouseMovedEvent`回调处理运动?或者全局隐藏光标? [英] Prevent `mouseMovedEvent` callback from processing the movement? Or globally hide cursor momentarily?

查看:189
本文介绍了阻止`mouseMovedEvent`回调处理运动?或者全局隐藏光标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个全球应用程序,防止鼠标移动到屏幕的区域。必须使用所有应用程式,而我的应用程式在后台等。



我设置了 CGEventTap 有鼠标移动的回调。



用户的鼠标移动继续通过钩子,无论我如何尝试修改/杀死移动。这反映了他人的经验: http://stackoverflow.com/a/5831573/106761



- (CGEventRef)mouseMovedEvent:(CGEventRef)newUserMouseMovement
{
//尝试修改鼠标移动
CGEventSetDoubleValueField (newUserMouseMovement,kCGMouseEventDeltaX,0);
CGEventSetDoubleValueField(newUserMouseMovement,kCGMouseEventDeltaY,0);
CGEventSetDoubleValueField(newUserMouseMovement,kCGMouseEventDeltaX,0.0);

//尝试杀死事件
return NULL;
}
//鼠标移动仍然正常工作。

我能够使用 CGDisplayMoveCursorToPoint CGWarpMouseCursorPosition 将光标重新定位到原始位置。





问题是,原始用户鼠标移动在该点显示光标视觉

 

code> CGSetLocalEventsSuppressionInterval(0.0);

以增加鼠标移动的频率。但这也使真正的光标,在我移动之前,闪光在禁区如果用户将鼠标停留在我不想要的鼠标去的区域。



我可能会使用:



任何想法?



EDIT:如何创建事件抽头:

  eventTap = CGEventTapCreate(kCGHIDEventTap,kCGHeadInsertEventTap,kCGEventTapOptionDefault,kCGEventMaskForAllEvents ,myCGEventCallback,NULL); 

...

  CGEventRef myCGEventCallback(CGEventTapProxy代理,CGEventType类型,CGEventRef事件,void * refcon)
{
if(type == kCGEventMouseMoved){
event = [refToSelf mouseMovedEvent:event ];
}

...

返回事件;
}


解决方案

事件在 kCGHIDEventTap 位置



不幸的是, CGEventTapCreate() doc说:


只有作为root用户运行的进程在HID事件进入窗口服务器的点处定位事件抽头;对于其他用户,此函数返回 NULL


与你的困难有关,虽然文档似乎不符合发生的事情(他们四岁以上)。



也许你可以旋转这个功能关闭到具有超级用户权限的单独进程,将您的应用程序的其余部分保留在正常用户模式下?我相信还有一种方法可以请求根权限,只是您的程序采取的特定操作。


I have a global app that prevents the mouse from moving into areas of the screen. Must work with all apps, while my app in the background, etc.

I have set up a CGEventTap hook that has a callback for mouse movements.

The user's mouse movement continues to pass through the hook, no matter how I attempt to modify/kill the movement. That mirrors the experiences of others: http://stackoverflow.com/a/5831573/106761

- (CGEventRef) mouseMovedEvent:(CGEventRef) newUserMouseMovement
{ 
    //Attempt to modify the mouse movement
    CGEventSetDoubleValueField(newUserMouseMovement, kCGMouseEventDeltaX, 0);
    CGEventSetDoubleValueField(newUserMouseMovement, kCGMouseEventDeltaY, 0);
    CGEventSetDoubleValueField(newUserMouseMovement, kCGMouseEventDeltaX, 0.0);

    //Attempt to kill the event
    return NULL;
}
//Mouse movement still works normally.

I am able to use CGDisplayMoveCursorToPoint or CGWarpMouseCursorPosition to reposition the cursor back to the original position.

This works fine, although I would rather just kill the event altogether.

The problem is that the original user mouse movement shows the cursor visual at that point for a split second, before I am able to move the cursor.

I'm using:

CGSetLocalEventsSuppressionInterval(0.0);

to increase the frequency at which the Mouse movement fires. But that also makes the "real" cursor, before I move it, flash in the "forbidden area" if the user holds the mouse against the area in which I don't want the mouse to go.

I will potentially use: Globally hiding cursor (from background app) but I doubt that would be Mac App Store legal.

Any ideas?

EDIT: How I create the event tap:

eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, kCGEventMaskForAllEvents, myCGEventCallback, NULL);

...

    CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
{
    if (type == kCGEventMouseMoved) {
        event = [refToSelf mouseMovedEvent:event];
    }

    ...

    return event;
}

解决方案

You're trying to modify events at the kCGHIDEventTap location, which

Specifies that an event tap is placed at the point where HID system events enter the window server.

Unfortunately, the CGEventTapCreate() doc says:

Only processes running as the root user may locate an event tap at the point where HID events enter the window server; for other users, this function returns NULL.

I suspect this is strongly related to your difficulty, although the docs don't seem to match up with exactly what's happening (they are more than four years old).

Perhaps you can spin this functionality off into a separate process that has super-user permissions, leaving the rest of your app in normal user mode? I believe there's also a way to request root permissions for just a specific action taken by your program.

这篇关于阻止`mouseMovedEvent`回调处理运动?或者全局隐藏光标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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