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
Post a Comment