System-wide hotkey for an application

后端 未结 3 824
广开言路
广开言路 2020-12-04 22:55

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

相关标签:
3条回答
  • 2020-12-04 23:34

    PTHotKey is old and busted (generates reams of warnings) on modern SDKs. Use SGHotKeysLib instead.

    Both SGHotKeysLib and PTHotKey are reusable source code. You need only add the classes to your own project, then use them from your own classes.

    0 讨论(0)
  • 2020-12-04 23:45

    I also didn't like PTHotKey, so I ended up writing a new wrapper, available here:

    http://github.com/davedelong/DDHotKey

    edit

    The 2 files you'd need are:

    • DDHotKeyCenter.h
    • DDHotKeyCenter.m

    And you'd use it something like this:

    - (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);
    }
    
    0 讨论(0)
  • 2020-12-04 23:46

    There is a library called PTHotKey that makes this fairly easy. You can google PTHotKey or just grab it from http://code.google.com/p/shortcutrecorder/source/browse/trunk/Demo/HotKey/?r=2

    0 讨论(0)
提交回复
热议问题