应用程序的系统级热键 [英] 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 kbd> 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个文件是:
- DDHotKeyCenter.h
- DDHotKeyCenter.m
使用它像这样:
- (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屋!