在Cocoa中注册全局文件拖动事件 [英] Register for global file drag events in Cocoa

查看:92
本文介绍了在Cocoa中注册全局文件拖动事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当OS X用户在OS X中(不仅是在我的应用程序中)拖动任何文件时,我都试图收到通知.

I'm trying to be notified when a OS X user is dragging any file in OS X, not only in my app.

我当前的方法是在 NSEvent 上使用 addGlobalMonitorForEventsMatchingMask:handler:,如下所示:

My current approach was using addGlobalMonitorForEventsMatchingMask:handler: on NSEvent, as follows:

[NSEvent addGlobalMonitorForEventsMatchingMask:NSLeftMouseDraggedMask handler:^(NSEvent* event) {
    NSPasteboard* pb = [NSPasteboard pasteboardWithName:NSDragPboard];
    NSLog(@"%@", [pb propertyListForType:NSFilenamesPboardType]);
}];

这部分起作用-当我开始从桌面或Finder拖动文件时调用处理程序,但是当我执行包含左鼠标拖动的其他所有操作时也调用该处理程序,例如移动窗户.问题是 NSDragPboard 似乎仍包含最新的拖动文件URL,例如当我释放文件并开始移动窗口时,很难区分这些操作.

This works partially - the handler is being called when I start dragging a file from my desktop or Finder, however it also is being called when I perform every other operation that contains a left-mouse-drag, e.g. moving a window. The issue is that the NSDragPboard still seems to contain the latest dragged file URL e.g. when I let off the file and start moving a window, which makes it hard to distinguish between these operations.

TL; DR-我对系统范围内的文件拖动操作感兴趣.我不需要有关拖动文件本身的任何信息,仅需要有关文件拖动操作已启动或停止的信息.如果有任何提示可以解决该问题,我将不胜感激.

TL;DR - I am interested in file drag operations system-wide. I do not need any information about the dragged file itself, just the information that a file drag operation has been started or stopped. I would appreciate any hint to a possible solution for this question.

推荐答案

与Apple DTS交谈后,这很可能是一个错误.我已针对此问题提交 rdar://25892115 .当前似乎无法用给定的API解决我原来的问题.

After having talked to Apple DTS, this is most likely a bug. I have filed rdar://25892115 for this issue. There currently seems to be no way to solve my original question with the given API.

为了解决我的问题,我现在使用 Accessibility API 来确定光标下方的项目是否为文件(kAXFilenameAttribute 不为 NULL).

To solve my problem, I am now using the Accessibility API to figure out if the item below the cursor is a file (kAXFilenameAttribute is not NULL).

这篇关于在Cocoa中注册全局文件拖动事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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