| Index | Submit class | Submit snippet | Submission feed | List |

BSHotKey

Language: Objective-C, Author: Karsten
License: Public domain

Plugin that adds global hotkey support to any application that loads it.

Usage: compile, copy the resulting bundle into your application's plugin folder and tell the app to load the bundle. Register a hotkey with name, target and action to the manager and you are ready to go.

Add the managers prefs view to your application's prefs window to change the hotkey (by default you don't have any hotkey set).
Hotkeys are stored/loaded to the user defaults automatically.

See the project for an example Application that makes use of the plugin.

BSHotKey source preview

//
//  BSHotKeyApp.m
//  BSHotKey
//
//  Created by Karsten Kusche on 09.04.07.
//  Copyright 2007 briksoftware.com. All rights reserved.
//

#import "BSHotKeyApp.h"
#import "BSHotKeyManager.h"

#define Log(ARGS...) [NSApp log:ARGS...]

@implementation BSHotKeyApp

- (id) init {
  self = [super init];
  if (self != nil) {
    if ([[NSBundle bundleWithPath:[[[NSBundle mainBundle] builtInPlugInsPath] stringByAppendingPathComponent:@"BSHotKey.bundle"]] load])
    {
      NSLog(@"bundle loaded");
    }    
  }
  return self;
}

- (void)log:(NSString*)format, ...
{
  va_list ap;
  va_start(ap, format);
  NSString* string = [[NSString alloc] initWithFormat:format arguments:ap];
  va_end(ap);

  NSLog(string);
  [logView replaceCharactersInRange:NSMakeRange([[logView string] length], 0) withString:[NSString stringWithFormat:@"%@\n",string]];
  [logView scrollRangeToVisible:NSMakeRange([[logView string] length], 0)];
  [string release];
  
}

- (void)finishLaunching
{
  [[BSHotKeyManager defaultManager] hotKeyNamed:@"MyHotKey" withAction:@selector(myHotKeyPressed:) target:self];
  [[BSHotKeyManager defaultManager] setReleaseAction:@selector(myHotKeyReleased:)];
  [super finishLaunching]; 
  [self log: @"finished launching, and hotkey is registered"];
}

- (void)awakeFromNib
{
  NSView* view = [[BSHotKeyManager defaultManager] prefsView];
//  NSLog(@"view = %@",prefsView);
  [prefsView addSubview:view];
  [prefsView setNeedsDisplay:YES];
  [[BSHotKeyManager defaultManager] setPrefsDelegate:self];
}

- (void)myHotKeyPressed:(id)name
{
  [self log:@"hotkey %@ was pressed :-D",name];
}

- (void)myHotKeyReleased:(id)name
{
  [self log:@"hotkey %@ was released :-D",name];
}

- (void)hotKeyChangedTo:(BSHotKey*)hotKey
{
  if (hotKey)
  {
    NSArray* array = [hotKey asArray];
    int modifiers = [[array objectAtIndex:0] unsignedShortValue];
    int keyCode = [[array objectAtIndex:1] unsignedCharValue];
    int charCode = [[array objectAtIndex:2] unsignedCharValue];
    
    [self log:@"hotkey changed to: %@. Use: \n[BSHotKey hotKeyWithKey:%p charCode: %p modifiers: %p]\n to use it as a default in your application.",
      hotKey, keyCode, charCode, modifiers];
  }
  else
  {
    [self log:@"hotKey was removed"];
  }
}

@end

BSHotKey header preview

        Mac OS X                  2   ç                                            ATTR ÑÚÏ      Ø  A                   Ø      com.apple.TextEncoding      ç 2  .com.apple.metadata:kMDItemAttributeChangeDate    UTF-8;134217984bplist003A©§°¡0º                                 
Download Archive

Compatible with:


Comments

Name

Website

Do you hate spammers? (Answer "Yes")