Can't block capslock with CGEventTap
Posted
by Thor Frølich
on Stack Overflow
See other posts from Stack Overflow
or by Thor Frølich
Published on 2010-05-29T17:16:06Z
Indexed on
2010/05/29
17:22 UTC
Read the original article
Hit count: 337
I'm using Quartz CGEventTap in an attempt to globally intercept capslock presses and block them (to have them do something useful instead). I succesfully detect capslock presses but have so far been unable to block them. My code (originating from this stackoverflow answer) is something like this:
eventTap = CGEventTapCreate(kCGHIDEventTap,
kCGTailAppendEventTap,
kCGEventTapOptionDefault,
eventMask,
myCGEventCallback,
&oldFlags);
runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef theEvent, void *refcon)
{
CGEventFlags *oldFlags = (CGEventFlags *)refcon;
switch (type)
{
case kCGEventFlagsChanged:
{
CGEventFlags newFlags = CGEventGetFlags(theEvent);
CGEventFlags changedFlags = *oldFlags ^ newFlags;
*oldFlags = newFlags;
if (changedFlags == 65536)
{
NSLog(@"Capslock pressed. Let's not return the event");
return NULL;
}
break;
}
default:
break;
}
NSLog(@"Different modifier than capslock. Returning the event");
return theEvent;
}
If I understand correctly returning NULL should effectively block the keypress from propagating. Indeed it also does for "normal" keyup and -down events. However capslock toggles regardless. Any ideas why that is? Am I making incorrect assumptions? And/or how can I do things differently to achieve my goal?
Thanks,
Thor
© Stack Overflow or respective owner