objective c - Programmatically Disable Mouse & keyboard -


i programmatically disable mouse & keyboard input temporarily on mac (using objective c/c/unix) & reenable them.

i have made small open source application allows selectively disable keyboards cgeventtap function os x. inside carbon framework, based on corefoundation, works on lion. example can try open sourceapp multilayout, available here on github.

basically need if want is:

to use it, need add carbon framework:

#import <carbon/carbon.h> 

then create event tap this:

void tap_keyboard(void) {     cfrunloopsourceref runloopsource;      cgeventmask mask = kcgeventmaskforallevents;     //cgeventmask mask = cgeventmaskbit(kcgeventkeyup) | cgeventmaskbit(kcgeventkeydown);      cfmachportref eventtap = cgeventtapcreate(kcghideventtap, kcgheadinserteventtap, kcgeventtapoptiondefault, mask, mycgeventcallback, null);      if (!eventtap) {          nslog(@"couldn't create event tap!");         exit(1);     }      runloopsource = cfmachportcreaterunloopsource(kcfallocatordefault, eventtap, 0);      cfrunloopaddsource(cfrunloopgetcurrent(), runloopsource, kcfrunloopcommonmodes);      cgeventtapenable(eventtap, true);      cfrelease(eventtap);     cfrelease(runloopsource);  } 

to interrupt events when necessary, use snippet:

bool dontforwardtap = false;  cgeventref mycgeventcallback(cgeventtapproxy proxy, cgeventtype type, cgeventref event, void *refcon) {       //nslog(@"event tap: %d", (int) cgeventgetintegervaluefield(event, kcgkeyboardeventkeycode));      if (dontforwardtap)         return nil;     else         return event; } 

just set boolean dontforwardtap true, , events stopped.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -