将鼠标限制在Mac上的一个显示器(潜在地使用可可) [英] Limiting mouse to one display on Mac (Potentially using Cocoa)

查看:195
本文介绍了将鼠标限制在Mac上的一个显示器(潜在地使用可可)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在热切地搜索一种方法,通过它在Mac上的多重显示设置中将用户的鼠标限制为一个显示。

I've been feverishly searching for a method by which to limit the user's mouse to one display in a multi-display setup on a Mac.

我偶然发现了这个问题:可可:将鼠标限制到屏幕,我保证我没有重复这个问题。

I've stumbled upon this question: Cocoa: Limit mouse to screen, I promise I have not duplicated this question.

然而,这个问题在我心目中引发了一个想法,可能使用Cocoa编写一个简单的应用程序来将鼠标限制到一个屏幕,运行此应用程序在后台,仍然使用我在AS3 / Adob​​e AIR / Flash中开发的游戏。

The question did, however, spark an idea in my mind that it might be possible to write a simple application using Cocoa to restrict the mouse to one screen, run this application in the background, and still use my game which has been developed in AS3/Adobe AIR/Flash.

游戏是一个全屏游戏,相同的分辨率在同一显示器上。另一个监视器也将始终具有相同的分辨率,但是仅仅是非交互式的信息显示。我需要用户能够与游戏进行交互,而不是意外地将鼠标移动到游戏的屏幕上。

The game is a full-screen game, and will always be at the same resolution on the same monitor. The other monitor will also always have the same resolution, but is to be just a non-interactive display of information. I need the user to be able to interact with the game, but not accidentally move the mouse off of the game's screen.

问题摘要
我可以使用Cocoa / Objective C创建一个基本的Mac OS X将鼠标移动到可以在背景中运行的一个显示器,并防止用户将鼠标移动到将在其上运行全屏游戏的显示器之外?

Question Summary: Can I create a basic application for Mac OS X (Lion) using Cocoa/Objective C that will restrict the mouse to one monitor that can be run IN THE BACKGROUND and prevent users from moving the mouse outside of the monitor that will have a full-screen game running on it?


我找到了为Quartz事件过滤器运行循环所需的基本代码,感谢这个答案:修改NSEvent以发送与按下

我将使用该代码进行测试,我已对其进行了修改,以检测鼠标事件,如下所示:

I am going to use that code for testing purposes, and I have modified it a bit to detect mouse events like so:

CGEventRef mouse_filter(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {

    NSPoint point = CGEventGetLocation(event);
    NSPoint target = NSMakePoint(100,100);
    if (point.x >= 500){
        CGEventSetLocation(event,target);

    }
    return event;
}

int main(int argc, char *argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    CFRunLoopSourceRef runLoopSource;

    CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, kCGEventMouseMoved, mouse_filter, NULL);

    if (!eventTap) {
        NSLog(@"Couldn't create event tap!");
        exit(1);
    }

    runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);

    CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);

    CGEventTapEnable(eventTap, true);

    CFRunLoopRun();

    CFRelease(eventTap);
    CFRelease(runLoopSource);
    [pool release];

    exit(0);
}

然而,这似乎并不正常。它当然正确地检测鼠标事件,它也适当地移动事件。例如,如果我从屏幕左侧拖动一个窗口超过500像素,则窗口拖动事件将移动到100,100。然而,鼠标本身不会移动到新的位置,当它只是在屏幕上移动,不执行任何其他操作。

This, however, doesn't seem to work quite right. It is certainly detecting mouse events properly, and it moves the events properly as well. For instance, if I drag a window past 500 pixels from the left of the screen, the window drag event gets moved to 100,100. However, the mouse itself doesn't get moved to the new location when it is simply being moved around the screen, performing no other actions. IE: You can still move the mouse all around the screen, instead of just on the left 500 pixel column.

任何想法?

推荐答案

使用Quartz事件点击可过滤鼠标移动和鼠标拖动的事件。在轻敲回调中,使用 CGEventSetLocation 修改鼠标事件的位置,否则会从主屏幕移开。

Use a Quartz Event Tap to filter mouse moved and mouse dragged events. In the tap callback, use CGEventSetLocation to modify the location of mouse events that would otherwise move off the main screen.

您可能需要以root用户身份运行程序,或者在系统偏好设置中启用辅助设备访问。

You may need to run the program as root, or have assistive device access enabled in System Preferences.

Quartz Event Services参考

这篇关于将鼠标限制在Mac上的一个显示器(潜在地使用可可)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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