NSEvent有一个方法来获取事件的子类型
:
Getting Custom Event Information
– data1
– data2
– subtype
是否可以从 CGEvent 访问相同的子类型
,而无需先将其转换为 NSEvent?
CGEventRef eventCG = ...;
NSEvent *eventNS = [NSEvent eventWithCGEvent:eventCG];
short subtypeNS = eventNS.subtype;
short subtypeCG = ???;
CGEvent docs提到鼠标事件子类型,似乎与 IOLLEvent.h 中的鼠标子类型相对应。不过,我对查找系统定义的 CGEvents 的子类型特别感兴趣。
这些将是下面的 NX_SUBTYPE_AUX_CONTROL_BUTTONS
等或 CG 替代方案。
/* sub types for mouse and move events */
#define NX_SUBTYPE_DEFAULT 0
#define NX_SUBTYPE_TABLET_POINT 1
#define NX_SUBTYPE_TABLET_PROXIMITY 2
#define NX_SUBTYPE_MOUSE_TOUCH 3
/* sub types for system defined events */
#define NX_SUBTYPE_POWER_KEY 1
#define NX_SUBTYPE_AUX_MOUSE_BUTTONS 7
/*
* NX_SUBTYPE_AUX_CONTROL_BUTTONS usage
*
* The incoming NXEvent for other mouse button down/up has event.type
* NX_SYSDEFINED and event.data.compound.subtype NX_SUBTYPE_AUX_MOUSE_BUTTONS.
* Within the event.data.compound.misc.L[0] contains bits for all the buttons
* that have changed state, and event.data.compound.misc.L[1] contains the
* current button state as a bitmask, with 1 representing down, and 0
* representing up. Bit 0 is the left button, bit one is the right button,
* bit 2 is the center button and so forth.
*/
#define NX_SUBTYPE_AUX_CONTROL_BUTTONS 8
#define NX_SUBTYPE_EJECT_KEY 10
#define NX_SUBTYPE_SLEEP_EVENT 11
#define NX_SUBTYPE_RESTART_EVENT 12
#define NX_SUBTYPE_SHUTDOWN_EVENT 13
#define NX_SUBTYPE_STICKYKEYS_ON 100
#define NX_SUBTYPE_STICKYKEYS_OFF 101
#define NX_SUBTYPE_STICKYKEYS_SHIFT 102
#define NX_SUBTYPE_STICKYKEYS_CONTROL 103
#define NX_SUBTYPE_STICKYKEYS_ALTERNATE 104
#define NX_SUBTYPE_STICKYKEYS_COMMAND 105
#define NX_SUBTYPE_STICKYKEYS_RELEASE 106
#define NX_SUBTYPE_STICKYKEYS_TOGGLEMOUSEDRIVING 107
最佳答案
对于鼠标事件,您可以使用 CGEventGetIntegerValueField
获取事件的kCGMouseEventSubtype
属性。
系统定义的事件比较棘手。 CGEvents 似乎并没有真正暴露这一点。
最简单的部分是匹配事件。只要 CGEvent 使用与 NSEvent 和 IOLLEvent 使用相同的事件类型编号,并且只要事件掩码只是 1 << eventType
的组合。 ,您应该能够在 CGEventMaskBit(NSSystemDefined)
上使用一些变体注册并CGEventGetType(event) == NSSystemDefined
用于测试。
困难的部分是在不通过 NSEvent 的情况下判断系统定义的事件的子类型是什么。
一种可能性是对鼠标事件执行相同的操作(使用 kCGMouseEventSubtype
),但由于 NXEventData
的布局鼠标事件和“复合”(包括系统定义的)事件是不同的,我不指望它能起作用。
很可能,无法从 CGEvent 获取系统定义事件的子类型,因此您必须创建一个 NSEvent。
您是否考虑过使用 NSEvent 的事件监视器 API?如果您需要 10.6 或更高版本,您可以通过这种方式捕获事件,并且您已经将它们作为 NSEvents 获取,准备好询问它们的子类型。
关于macos - NSEvent `subtype` 相当于 CGEvent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20864608/