我正在开发一款macOS应用程序,希望在NSViewController
中处理本地热键事件(command+上箭头键)。
以下是使用Swift实现的方法:
override func keyDown(with event: NSEvent) {
let modifierkeys = event.modifierFlags.intersection(.deviceIndependentFlagsMask);
let hasCommand = modifierkeys == .command;
switch Int(event.keyCode) {
case kVK_UpArrow where hasCommand:
print("command up");
break;
case kVK_ANSI_B where hasCommand:
print("command B");
break;
default:
break;
}
}
当我在视图中构建并按Command+上箭头时,控制台没有任何显示。但是当我按下 Command+B 时,“command B”被记录下来。
那么为什么 Command+上箭头不能正常工作?应该如何实现这一点?