-1
我的应用程序在后台运行,并不总是处于活动状态。但是,我怎样才能将预定的文本插入到用户计算机上的当前光标位置,而不会使我的应用程序处于活动状态?插入来自非活动应用程序的文本?
我的应用程序在后台运行,并不总是处于活动状态。但是,我怎样才能将预定的文本插入到用户计算机上的当前光标位置,而不会使我的应用程序处于活动状态?插入来自非活动应用程序的文本?
此代码使用Accessibility API。
CFTypeRef focusedUI;
AXUIElementCopyAttributeValue(AXUIElementCreateSystemWide(), kAXFocusedUIElementAttribute, &focusedUI);
if (focusedUI) {
CFTypeRef textValue, textRange;
// get text content and range
AXUIElementCopyAttributeValue(focusedUI, kAXValueAttribute, &textValue);
AXUIElementCopyAttributeValue(focusedUI, kAXSelectedTextRangeAttribute, &textRange);
NSRange range;
AXValueGetValue(textRange, kAXValueCFRangeType, &range);
// replace current range with new text
NSString *newTextValue = [(__bridge NSString *)textValue stringByReplacingCharactersInRange:range withString:@"hello"];
AXUIElementSetAttributeValue(focusedUI, kAXValueAttribute, (__bridge CFStringRef)newTextValue);
// set cursor to correct position
range.length = 5;
range.location += 5;
AXValueRef valueRef = AXValueCreate(kAXValueCFRangeType, (const void *)&range);
AXUIElementSetAttributeValue(focusedUI, kAXSelectedTextRangeAttribute, valueRef);
CFRelease(textValue);
CFRelease(textRange);
CFRelease(focusedUI);
}
非常感谢!然而,它似乎并不适用于Whatsapp网站或Facebook等网站的文本字段,尽管它可以在网址栏中使用。任何想法如何解决这个问题? – user2161301