如何判断在 Dock 上放置过程中是否按下了修饰键
我的 Cocoa 应用程序支持将文件拖放到其 Dock 图标上,但我希望根据是否按住修饰键(Command、Option 等)来实现不同的行为。
我尝试检查 currentEvent
的 modifierFlags
,但无论是否按住修饰符,它们都是相同的(我正在使用 Option 键进行测试)。
代码:
// Code is inside my AppDelegate
- (void)application:(NSApplication *)theApplication openFiles:(NSArray *)files {
BOOL optDown = (([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask)
== NSAlternateKeyMask);
NSLog(@"flags: %u, down? %@", [[NSApp currentEvent] modifierFlags],
optDown ? @"YES" : @"NO");
}
输出(按下 Option 键放下文件,然后不放下):
flags: 1088, down? NO
flags: 1088, down? NO
预期
flags: <not sure>, down? YES
flags: <different>, down? NO
My Cocoa application supports dropping files onto its Dock icon, but I'd like different behavior depending on whether a modifier key is held down (Command, Option, etc.).
I tried checking the modifierFlags
for the currentEvent
, but they are the same regardless of whether a modifier is held down, or not (I was testing with the Option key).
Code:
// Code is inside my AppDelegate
- (void)application:(NSApplication *)theApplication openFiles:(NSArray *)files {
BOOL optDown = (([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask)
== NSAlternateKeyMask);
NSLog(@"flags: %u, down? %@", [[NSApp currentEvent] modifierFlags],
optDown ? @"YES" : @"NO");
}
Output (dropping a file with the Option key down, then not):
flags: 1088, down? NO
flags: 1088, down? NO
Expected
flags: <not sure>, down? YES
flags: <different>, down? NO
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,如果您的应用不是最前面的,您就不能指望
[NSApp currentEvent]
与当前用户状态有任何关系。为了获取硬件状态(无论最前面的应用程序如何,该状态都可以工作),
GetCurrentKeyModifiers()
支持回溯到 10.0(包括 64 位);如果您需要 10.6,[NSEvent modifierFlags]
是另一个选择。In general, you can't expect
[NSApp currentEvent]
to have anything to do with the current user state if your app is not frontmost.To get the hardware state, which will work regardless of the frontmost app,
GetCurrentKeyModifiers()
is supported back to 10.0 (including 64-bit);[NSEvent modifierFlags]
is another option if you can require 10.6.