在Dock上,如何知道修改器键是否关闭 [英] How to tell if a modifier key is down during drop on Dock

查看:140
本文介绍了在Dock上,如何知道修改器键是否关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Cocoa应用程序支持将文件拖放到Dock图标上,但是我想要不同的行为取决于修饰符键是否被按下(Command,Option等)。



我尝试检查 currentEvent modifierFlags ,但无论修饰符是否为(/)>

代码


$ b $应用程序:(NSApplication *)应用程序openFiles:(NSArray *)文件{
BOOL optDown = (([[NSApp currentEvent] modifierFlags]& NSAlternateKeyMask)
== NSAlternateKeyMask);
NSLog(@flags:%u,down?%@,[[NSApp currentEvent] modifierFlags],
optDown?@YES:@NO);
}

输出(使用Option键删除文件下来,然后不):

  flags:1088,down? NO 
标志:1088,下来? NO

预期

  flags:< not sure>,down? YES 
标志:<不同>下来? NO


解决方案

一般来说,你不能指望 [NSApp currentEvent] 与当前用户状态有任何关系,如果您的应用程序不是最前端的。



要获取硬件状态,无论最前端的应用程序如何,都将工作, GetCurrentKeyModifiers()被支持回到10.0(包括64位); [NSEvent modifierFlags] 是另一个选项,如果你可以要求10.6。


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

解决方案

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.

这篇关于在Dock上,如何知道修改器键是否关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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