我创建一个事件水龙头:
_eventTap = CGEventTapCreate( kCGHIDEventTap, // kCGSessionEventTap,
kCGHeadInsertEventTap,
kCGEventTapOptionDefault,
CGEventMaskBit( kCGEventKeyDown )
| CGEventMaskBit( kCGEventFlagsChanged )
| CGEventMaskBit( NSSystemDefined )
,
(CGEventTapCallBack)_tapCallback,
(__bridge void *)(self));
}
CGEventTapEnable( _eventTap, YES );
通过记录事件点击的输出,我可以确定以下内容:
/* CAPS-LOCK
(values in hex)
NOTE: NSEventModifierFlagCapsLock = 1 << 16 = 0x10000
(Initially OFF)
DOWN (green light comes on)
NSEventTypeSystemDefined: data1=40a00 event.modifierFlags: 10000
NSEventTypeFlagsChanged: 10000 event.modifierFlags: 10000
UP
NSEventTypeSystemDefined: data1=40b00 event.modifierFlags: 10000
DOWN (green light goes off)
NSEventTypeSystemDefined: data1=40a00 event.modifierFlags: 0
NSEventTypeFlagsChanged: 0 event.modifierFlags: 0
UP
NSEventTypeSystemDefined: data1=40b00 event.modifierFlags: 0
40a00 = b1000000101000000000
40b00 = b1000000101100000000
^ = b0000000000100000000 = 1 << 8
*/
因此,我可以编写以下代码:
{
bool capslock_down = event.type == NSEventTypeSystemDefined && event.data1 == 0x40a00;
bool capslock_up = event.type == NSEventTypeSystemDefined && event.data1 == 0x40b00;
bool capslock_now_active = event.modifierFlags & NSEventModifierFlagCapsLock;
if( capslock_down ) printf( "capslock_down, %s \n", capslock_now_active ? "ACTIVE" : "inactive" );
if( capslock_up ) printf( "capslock_up , %s \n", capslock_now_active ? "ACTIVE" : "inactive" );
}
但是,对这些0x40a00
,0x40b00
值进行硬编码是不好的做法。
event.data1
时event.type == NSEventTypeSystemDefined
到底包含什么?
在哪里可以找到相关文档?
(NSEvent.h定义了NSScrollLockFunctionKey = 0xF72F
,但是我知道这很近)。