gpt4 book ai didi

objective-c - 无法通过事件点击在某些应用程序上模拟退格按钮

转载 作者:行者123 更新时间:2023-12-03 17:05:11 25 4
gpt4 key购买 nike

我正在尝试模拟 Mac 操作系统上的一些按键。如果按下“h”键(例如,如果用户键入“tigh”,它将变成“ti”),此代码应该通过修改键盘事件来删除前一个字符。然而,它仅适用于某些应用程序;其他人完全拒绝我的事件。这段代码有什么问题吗?

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
CFMachPortRef eventTap;
CGEventMask eventMask;
CFRunLoopSourceRef runLoopSource;

eventMask = ((1 << kCGEventKeyDown) | (1 << kCGEventKeyUp));

eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0,
eventMask, KeyHandler, NULL);
if (!eventTap) {
fprintf(stderr, "failed to create event tap\n");
exit(1);
}

runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
CFRunLoopRun();
}

CGEventRef KeyHandler(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
{
UniCharCount actualStringLength;
UniCharCount maxStringLength = 1;
UniChar chars[3];

CGEventKeyboardGetUnicodeString(event, maxStringLength, &actualStringLength, chars);

if (chars[0] == 'h') {
chars[0] = '\b';
CGEventKeyboardSetUnicodeString(event, 1, chars);
return event;
}

return event;
}

最佳答案

某些应用程序根据事件中的按键代码 (CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode)) 查找正在键入的内容,而不是事件的 UnicodeString

也就是说,您需要将事件的 kCGKeyboardEventKeycode 值从“h”提供的 4 更新为 51 或 (>0x33)用于退格键。

关于objective-c - 无法通过事件点击在某些应用程序上模拟退格按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10742086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com