Friday 15 June 2012

objective c - Substitute keystroke -



objective c - Substitute keystroke -

i trying intercept keystroke, , substitute different character. have been able intercept key beingness pressed, perform operations. need hold key beingness pressed if matches 1 of ones watching, , insert different character. here code have right now:

#import "appdelegate.h" #import <appkit/appkit.h> #import <coregraphics/coregraphics.h> #include <applicationservices/applicationservices.h> @interface appdelegate () @property (weak) iboutlet nswindow *window; @end @implementation appdelegate - (void)applicationdidfinishlaunching:(nsnotification *)anotification { //keys beingness watched switched out nsarray *keystowatch = [[nsarray alloc] initwithobjects:@"c",@".", nil]; // register keys throughout device... [nsevent addglobalmonitorforeventsmatchingmask:nskeydownmask handler:^(nsevent *event){ //get characters nsstring *chars = [[event characters] lowercasestring]; //get actual character beingness pressed unichar character = [chars characteratindex:0]; //transform string nsstring *astring = [nsstring stringwithcharacters:&character length:1]; //if in list, start looking if keynote active if ([keystowatch containsobject:[nsstring stringwithstring:astring]]) { //debug: print message nslog(@"key beingness watched has been pressed"); //get list of running apps (nsrunningapplication *currapp in [[nsworkspace sharedworkspace] runningapplications]) { //get current active app if ([currapp isactive]) { //check if keynote, if yes perform remap if ([[currapp localizedname] isequaltostring:@"keynote"]){ //debug: print little message nslog(@"current app keynote"); if (character=='.') { nslog(@"pressed dot"); //i want post different character here postkeywithmodifiers((cgkeycode)11, false); } else if ([astring isequaltostring:@"c"]) { nslog(@"pressed c"); } } else if ([[currapp localizedname] isequaltostring:@"microsoft powerpoint"]){ } } } } } ]; } - (void)applicationwillterminate:(nsnotification *)anotification { // insert code here tear downwards application } - (bool)acceptsfirstresponder { homecoming yes; } void postkeywithmodifiers(cgkeycode key, cgeventflags modifiers) { cgeventsourceref source = cgeventsourcecreate(kcgeventsourcestatecombinedsessionstate); cgeventref keydown = cgeventcreatekeyboardevent(source, key, true); cgeventsetflags(keydown, modifiers); cgeventref keyup = cgeventcreatekeyboardevent(source, key, false); cgeventpost(kcgannotatedsessioneventtap, keydown); cgeventpost(kcgannotatedsessioneventtap, keyup); cfrelease(keyup); cfrelease(keydown); cfrelease(source); } @end

my problem not able stop original keystroke. please maintain in mind new @ obj c, allow me know if there can better. thanks!

from docs +[nsevent addglobalmonitorforeventsmatchingmask:handler:]:

events delivered asynchronously app , can observe event; cannot modify or otherwise prevent event beingness delivered original target application.

you have utilize quartz event tap that.

objective-c objective-c-blocks keyboard-events

No comments:

Post a Comment