我正在尝试重新映射CAPS-LOCK。
我可以使用CGEventTap
成功检测到CAPS-LOCK键按下/按下事件。
但是,我无法消费它们。 (OTOH我 am 能够使用普通的按键按下/按下事件)。
下面的代码摘录说明了这一点:
@implementation Intercept
- (BOOL) tapEvents
{
_eventTap = CGEventTapCreate( kCGHIDEventTap, // kCGSessionEventTap,
kCGHeadInsertEventTap,
kCGEventTapOptionDefault,
CGEventMaskBit( NSEventTypeKeyDown )
| CGEventMaskBit( NSEventTypeFlagsChanged )
| CGEventMaskBit( NSEventTypeSystemDefined ),
(CGEventTapCallBack) tapCallback,
(__bridge void *)(self));
CGEventTapEnable( _eventTap, YES );
}
CGEventRef tapCallback(
CGEventTapProxy proxy,
CGEventType type,
CGEventRef event,
Intercept* listener
)
{
return [listener interceptEvent:event];
}
- (CGEventRef)interceptEvent:(CGEventRef)cgEvent
{
NSEvent* event = [NSEvent eventWithCGEvent:cgEvent];
/*
CAPSLOCK sends:
(1) NSEventTypeSystemDefined
(2) NSEventTypeFlagsChanged
*/
switch( event.type ) {
// if I return NULL here, it DOESN'T eat CAPS-LOCK
case NSEventTypeFlagsChanged:
case NSEventTypeSystemDefined:
return NULL;
// if I return NULL here, it DOES eat keystrokes
case NSEventTypeKeyDown:
return NULL;
}
CGEventRef ev = [event CGEvent];
CFRetain(ev);
return ev;
}
这把钥匙怎么逃脱约束其余钥匙的规则?
我必须下拉到IOHID层吗?
REF:Simulate/Toggle CAPS LOCK programatically in OS X
或者,注入解毒剂CAPS-LOCK事件会取消真实事件的影响,这是否有意义?