2015-06-26 29 views

回答

0

此代码使用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); 
     } 
+0

非常感谢!然而,它似乎并不适用于Whatsapp网站或Facebook等网站的文本字段,尽管它可以在网址栏中使用。任何想法如何解决这个问题? – user2161301

相关问题