如何将unicode字符发送到活动应用程序?

use*_*134 5 macos sendinput

我在Windows API中需要类似SendInput的东西.

我看到这个方法,我不知道无论如何都要将unicode字符转换为虚拟Key代码.

CGEventRef CGEventCreateKeyboardEvent (
   CGEventSourceRef source,
   CGKeyCode virtualKey,
   bool keyDown
);
Run Code Online (Sandbox Code Playgroud)

use*_*134 5

CGEventRef e = CGEventCreateKeyboardEvent(NULL, NULL, true);
CGEventKeyboardSetUnicodeString(e, unicodeStringLength, unicodeString);   
CGEventPost(kCGHIDEventTap, e);      
Run Code Online (Sandbox Code Playgroud)

  • 将`NULL`作为`CGEventCreateKeyboardEvent`的第二个参数传递是不正确的,因为它不是指针.它恰好起作用,因为`NULL`被视为零,这是一个有效的密钥代码.另请注意,最后应该说'CFRelease(e)`. (4认同)