Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System-wide hotkey for an application

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.

Something like Cmd + Shift + 1 press button 1, Cmd + Shift + 2 press button 2, etc.

Is there any way to achieve this in Cocoa (with Objective-C)? Thanks, code is appreciated since I am a total newbie on Cocoa.

like image 595
Jessica Avatar asked Sep 21 '10 12:09

Jessica


People also ask

What is the shortcut key of application?

The shortcut key is the key or the combination of keys that allows you to execute commands without opening the start menu on the screen. Defining a key or keys as a shortcut key to an application program allows you to start the application without using the start menu.

How do you switch between application using the keyboard?

Press and hold the [Alt] key > Click the [Tab] key once. A box with screen shots representing all of the open applications will appear. Keep the [Alt] key pressed down and press the [Tab] key or arrows to switch between open applications.


1 Answers

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);
}
like image 182
Dave DeLong Avatar answered Oct 18 '22 22:10

Dave DeLong