Created
April 4, 2013 15:06
-
-
Save alesleoignis/5311170 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ALIAppDelegate.m | |
// 123 | |
// | |
// Created by Admin on 4/4/13. | |
// Copyright (c) 2013 Admin. All rights reserved. | |
// | |
#import "ALIAppDelegate.h" | |
#include <ApplicationServices/ApplicationServices.h> | |
@implementation ALIAppDelegate | |
CGEventRef | |
myCGEventCallback(CGEventTapProxy proxy, CGEventType type, | |
CGEventRef event, void *refcon) | |
{ | |
CGEventFlags eventFlags = CGEventGetFlags(event); | |
if (type == kCGEventFlagsChanged) { | |
printf("%lld\n", eventFlags); | |
} | |
// Paranoid sanity check. | |
if (type != kCGEventKeyDown) | |
return event; | |
// The incoming keycode. | |
CGKeyCode keycode = (CGKeyCode)CGEventGetIntegerValueField( | |
event, kCGKeyboardEventKeycode); | |
printf("%d\n", keycode); | |
//Keypress code goes here. | |
// We must return the event for it to be useful. | |
return event; | |
} | |
- (void)myThreadMainMethod | |
{ | |
CFMachPortRef eventTap; | |
CGEventMask eventMask; | |
CFRunLoopSourceRef runLoopSource; | |
// Create an event tap. We are interested in key presses. | |
eventMask = CGEventMaskBit(kCGEventKeyDown) | CGEventMaskBit(kCGEventKeyUp) | CGEventMaskBit(kCGEventFlagsChanged); | |
eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, eventMask, myCGEventCallback, NULL); | |
if (!eventTap) { | |
fprintf(stderr, "failed to create event tap\n"); | |
exit(1); | |
} | |
// Create a run loop source. | |
runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0); | |
// Add to the current run loop. | |
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, | |
kCFRunLoopCommonModes); | |
// Enable the event tap. | |
CGEventTapEnable(eventTap, true); | |
// Set it all running. | |
CFRunLoopRun(); | |
} | |
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification | |
{ | |
NSThread* myThread = [[NSThread alloc] initWithTarget:self | |
selector:@selector(myThreadMainMethod) | |
object:nil]; | |
[myThread start]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment