不带编辑菜单的对话框中的Cocoa键盘快捷键 [英] Cocoa Keyboard Shortcuts in Dialog without an Edit Menu
问题描述
我有一个 LSUIElement
应用程序,显示菜单状态项。应用程序可以显示包含文本字段的对话框窗口。
I have an LSUIElement
application that displays a menubar status item. The application can display a dialog window that contains a text field.
如果用户右键单击/控制单击文本字段,将出现一个允许剪切,复制,粘贴等。但是,标准的Command-X,Command-C和Command-V键盘快捷键在该字段中不起作用。我假设这是因为我的应用程序不提供一个编辑菜单,其中定义了这些快捷方式。
If the user right-clicks/control-clicks the text field, a menu appears that allows cut, copy, paste, etc. However, the standard Command-X, Command-C, and Command-V keyboard shortcuts do not work in the field. I assume this is because my application does not provide an Edit menu with those shortcuts defined.
我已经尝试添加一个编辑菜单项到我的应用程序的菜单, 发送一些代码博客,但是没有工作。可以使用编辑菜单中的菜单项,但是键盘快捷键仍然不起作用。
I've tried adding an Edit menu item to my application's menu, as suggested in the Ship Some Code blog, but that did not work. The menu items in the Edit menu can be used, but keyboard shortcuts still don't work.
我可以想象几种方法来破解键盘处理,但是
I can imagine a few ways to hack the keyboard handling, but is there a "recommended" way to make this work?
(有关应用程序的详细信息,请参阅 Menubar倒计时。)
(For details about the app, see Menubar Countdown.)
Related question: Copy/Paste Not Working in Modal Window
推荐答案
在模态窗口中复制/粘贴不工作
What worked for me was using The View Solution presented in Copy and Paste Keyboard Shortcuts at CocoaRocket.
基本上,这意味着子类化NSTextField并覆盖 performKeyEquivalent:
。
Basically, this means subclassing NSTextField and overriding performKeyEquivalent:
.
更新: CocoaRocket网站似乎已不复存在。以下是互联网存档链接: http: //web.archive.org/web/20100126000339/http://www.cocoarocket.com/articles/copypaste.html
Update: The CocoaRocket site is apparently gone. Here's the Internet Archive link: http://web.archive.org/web/20100126000339/http://www.cocoarocket.com/articles/copypaste.html
编辑:Swift代码如下所示
Edit: The Swift code looks like this
class Editing: NSTextField {
private let commandKey = NSEventModifierFlags.CommandKeyMask.rawValue
private let commandShiftKey = NSEventModifierFlags.CommandKeyMask.rawValue | NSEventModifierFlags.ShiftKeyMask.rawValue
override func performKeyEquivalent(event: NSEvent) -> Bool {
if event.type == NSEventType.KeyDown {
if (event.modifierFlags.rawValue & NSEventModifierFlags.DeviceIndependentModifierFlagsMask.rawValue) == commandKey {
switch event.charactersIgnoringModifiers! {
case "x":
if NSApp.sendAction(Selector("cut:"), to:nil, from:self) { return true }
case "c":
if NSApp.sendAction(Selector("copy:"), to:nil, from:self) { return true }
case "v":
if NSApp.sendAction(Selector("paste:"), to:nil, from:self) { return true }
case "z":
if NSApp.sendAction(Selector("undo:"), to:nil, from:self) { return true }
case "a":
if NSApp.sendAction(Selector("selectAll:"), to:nil, from:self) { return true }
default:
break
}
}
else if (event.modifierFlags.rawValue & NSEventModifierFlags.DeviceIndependentModifierFlagsMask.rawValue) == commandShiftKey {
if event.charactersIgnoringModifiers == "Z" {
if NSApp.sendAction(Selector("redo:"), to:nil, from:self) { return true }
}
}
}
return super.performKeyEquivalent(event)
}
}
这篇关于不带编辑菜单的对话框中的Cocoa键盘快捷键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!