简体   繁体   English

以编程方式插入Unicode(Objective-C / OSX)

[英]Inserting Unicode programmatically (Objective-C / OSX)

I have bindings that insert text into the current application as a sequence of keypresses. 我有绑定,这些绑定将文本作为一系列按键插入到当前应用程序中。 Each keypress is executed by: 每次按键均由以下命令执行:

https://github.com/sboosali/workflow-osx/blob/master/cbits/objc_workflow.m#L115 https://github.com/sboosali/workflow-osx/blob/master/cbits/objc_workflow.m#L115

eg "A" becomes (0x00020000, 0x00) (ie (ShiftModifier, AKey) ). 例如, "A"变为(0x00020000, 0x00) (即(ShiftModifier, AKey) )。

However, this has limitations, such as filtering away Unicode characters. 但是,这有局限性,例如过滤掉Unicode字符。 Rather than "indirectly" inserting text via keypresses, how could I "directly" insert text, including arbitrary Unicode? 而不是通过按键“间接”插入文本,我如何“直接”插入文本,包括任意Unicode?

Alternatives: 备择方案:

  1. A subset of Unicode could be supported with the Option modifier, as capital letters are supported with the Shift modifier. Option修饰符可以支持Unicode的子集,因为Shift修饰符可以支持大写字母。 However, it's only a subset. 但是,这只是一个子集。

  2. Inserting text via the clipboard supports all of Unicode. 通过剪贴板插入文本支持所有Unicode。 However, this overwrites the previous clipboard contents, and needs a variable-length delay. 但是,这将覆盖先前的剪贴板内容,并且需要可变长度的延迟。

(I've been reading through the documentation (like https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSTextInputClient_Protocol/index.html#//apple_ref/occ/intfm/NSTextInputClient/insertText:replacementRange :), but I can't seem to get a hold of any of the objects I would need (like NSTextInputClient )). (我一直在阅读文档(例如https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSTextInputClient_Protocol/index.html#//apple_ref/occ/intfm/NSTextInputClient/insertText:replacementRange :),但我似乎无法掌握我需要的任何对象(例如NSTextInputClient ))。

found it, use CGEventKeyboardSetUnicodeString : 找到它,使用CGEventKeyboardSetUnicodeString

// UniChar is a UTF8 codepoint.
insertChar(UniChar c) {

 UniChar cs[1];
 cs[0] = c;

 CGEventSourceRef s = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
 CGEventRef e = CGEventCreateKeyboardEvent(s, 0, true);

 CGEventKeyboardSetUnicodeString(e, 1, cs);   

 CGEventPost(kCGHIDEventTap, e);    

 CFRelease(s);
 CFRelease(e);
}

https://developer.apple.com/library/mac/documentation/Carbon/Reference/QuartzEventServicesRef/#//apple_ref/c/func/CGEventKeyboardSetUnicodeString https://developer.apple.com/library/mac/documentation/Carbon/Reference/QuartzEventServicesRef/#//apple_ref/c/func/CGEventKeyboardSetUnicodeString

How send unicode character to active application? 如何将Unicode字符发送到活动应用程序?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM