应用程序的系统范围热键 [英] System-wide hotkey for an application
问题描述
我有一个带有 3 个按钮的简单窗口,我正在尝试添加一个系统范围的热键,这样我就可以按下"这些按钮而不必切换到该应用程序,按下一个按钮,然后回到原来的状态
I have a simple window with 3 buttons and I am trying to add a system-wide hot key so i can "press" those buttons without having to switch to that app, press a button and then go back to what I was doing.
类似于 Cmd + Shift + 1 按下按钮 1,Cmd + Shift + 2 按下按钮 2 等
Something like Cmd + Shift + 1 press button 1, Cmd + Shift + 2 press button 2, etc.
有什么办法可以在 Cocoa 中实现这一点(使用 Objective-C)?谢谢,感谢代码,因为我是 Cocoa 的新手.
Is there any way to achieve this in Cocoa (with Objective-C)? Thanks, code is appreciated since I am a total newbie on Cocoa.
推荐答案
我也不喜欢 PTHotKey,所以我最终编写了一个新的包装器,可在此处获得:
I also didn't like PTHotKey, so I ended up writing a new wrapper, available here:
http://github.com/davedelong/DDHotKey
编辑
您需要的 2 个文件是:
The 2 files you'd need are:
你会像这样使用它:
- (IBAction) registerHotkey:(id)sender {
DDHotKeyCenter * c = [[DDHotKeyCenter alloc] init];
if (![c registerHotKeyWithKeyCode:kVK_ANSI_1 modifierFlags:(NSCommandKeyMask | NSShiftKeyMask) target:self action:@selector(hotkeyWithEvent:) object:nil]) {
NSLog(@"unable to register hotkey");
} else {
NSLog(@"registered hotkey");
}
[c release];
}
- (void) hotkeyWithEvent:(NSEvent *)hkEvent {
NSLog(@"Hotkey event: %@", hkEvent);
}
这篇关于应用程序的系统范围热键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!