Show AppController.m syntax highlighted
#import "AppController.h"
#include <Carbon/Carbon.h>
const UInt32 kMyHotKeyIdentifier='wolf';
const UInt32 kMyHotKey = 9; //the 'v' key virtual key code
EventHotKeyRef gMyHotKeyRef;
EventHotKeyID gMyHotKeyID;
EventHandlerUPP gAppHotKeyFunction;
static BOOL AnyKeyDown() {
const UInt32 kCapsLockBit = 0x02L;
KeyMap keys;
GetKeys( keys );
return keys[0] || (keys[1] & ~kCapsLockBit) || keys[2] || keys[3];
}
pascal OSStatus MyHotKeyHandler(EventHandlerCallRef nextHandler,EventRef theEvent,void *userData) {
NSPasteboard *p = [NSPasteboard generalPasteboard];
if( [[p types] containsObject:NSStringPboardType] ) {
NSString *input = [p stringForType:NSStringPboardType];
[p declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
[p setString:input forType:NSStringPboardType];
long ignored;
while( AnyKeyDown() ) {
Delay(6, &ignored); // tenth of a second
}
CGPostKeyboardEvent( 0, 55, 1 ); // cmd-down
Delay(8, &ignored);
CGPostKeyboardEvent( 'v', 9, 1 ); // v-down
Delay(8, &ignored);
CGPostKeyboardEvent( 'v', 9, 0 ); // v-up
Delay(8, &ignored);
CGPostKeyboardEvent( 0, 55, 0 ); // cmd-up
// TODO restore previous styled clipboard
}
return noErr;
}
@implementation AppController
#define kShowedUserNoteKey @"showedUserNote"
#define kHeader (CFStringRef)@"StyleStripper"
#define kMsg (CFStringRef)@"StyleStripper is now running in the background. Use command-shift-v to paste unstyled text."
- (void)awakeFromNib {
if( ![[NSUserDefaults standardUserDefaults] boolForKey:kShowedUserNoteKey] ) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kShowedUserNoteKey];
[[NSUserDefaults standardUserDefaults] synchronize];
CFUserNotificationDisplayNotice(
0.0, // timeout
kCFUserNotificationNoteAlertLevel, // flags
NULL, // iconURL
NULL, // soundURL
NULL, // localizationURL
kHeader, // alertHeader
kMsg, // alertMessage
NULL ); // defaultButtonTitle
}
EventTypeSpec eventType;
gAppHotKeyFunction = NewEventHandlerUPP( MyHotKeyHandler );
eventType.eventClass = kEventClassKeyboard;
eventType.eventKind = kEventHotKeyPressed;
InstallApplicationEventHandler( gAppHotKeyFunction, 1, &eventType, NULL, NULL);
gMyHotKeyID.signature = kMyHotKeyIdentifier;
gMyHotKeyID.id = 1;
RegisterEventHotKey( kMyHotKey, cmdKey+shiftKey, gMyHotKeyID, GetApplicationEventTarget(), 0, &gMyHotKeyRef );
}
@end
See more files for this project here