我试图模仿按住音量增大键几秒钟。我试图进行简单的测试,但我认为有些东西我不见了。任何帮助将不胜感激。我目前的代码基于:https://stackoverflow.com/a/10520629/2763703。
这是它的样子:
static void HIDPostAuxKey(const UInt8 auxKeyCode) {
__block NXEventData event;
__block kern_return_t kr;
__block IOGPoint loc = { 0, 0 };
// Key press event
__block UInt32 evtInfo = auxKeyCode << 16 | NX_KEYDOWN << 8;
bzero(&event, sizeof(NXEventData));
event.key.origCharSet = event.key.charSet = NX_ASCIISET;
event.compound.subType = NX_SUBTYPE_AUX_CONTROL_BUTTONS;
event.compound.misc.L[0] = evtInfo;
// event.key.repeat = true;
kr = IOHIDPostEvent( get_event_driver(), NX_SYSDEFINED, loc, &event, kNXEventDataVersion, 0, FALSE );
assert( KERN_SUCCESS == kr );
// Key release event
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 2);
dispatch_after(delay, dispatch_get_main_queue(), ^(void){
evtInfo = auxKeyCode << 16 | NX_KEYUP << 8;
bzero(&event, sizeof(NXEventData));
event.key.origCharSet = event.key.charSet = NX_ASCIISET;
event.compound.subType = NX_SUBTYPE_AUX_CONTROL_BUTTONS;
event.compound.misc.L[0] = evtInfo;
kr = IOHIDPostEvent( get_event_driver(), NX_SYSDEFINED, loc, &event, kNXEventDataVersion, 0, FALSE );
assert( KERN_SUCCESS == kr );
});
}
如果我将此称为HIDPostAuxKey(NX_KEYTYPE_SOUND_UP)
,则表现得很奇怪。它将增加一个音量,但继续使音量增加几秒钟。我想要的就像你按住键盘上的音量调高键一样。我认为event.key.repeat = true;
行可以解决问题,但这似乎只是显示“你确定要立即关闭计算机”对话框。谢谢你的帮助。