5

I need something like SendInput in Windows API.

I see this method, I don't know there is anyway to convert unicode character to virtual Key code.

CGEventRef CGEventCreateKeyboardEvent (
   CGEventSourceRef source,
   CGKeyCode virtualKey,
   bool keyDown
);
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
user486134
  • 405
  • 1
  • 6
  • 13
  • You can use SendInput to send unicode characters, but you are limited to UTF-8 since you are limited to a short for wScan. You need to set wVK to 0x00 and set the use unicode flag(0x0004) in dwFlags. – CaulynDarr Jan 08 '13 at 18:48

1 Answers1

5
CGEventRef e = CGEventCreateKeyboardEvent(NULL, NULL, true);
CGEventKeyboardSetUnicodeString(e, unicodeStringLength, unicodeString);   
CGEventPost(kCGHIDEventTap, e);      
user486134
  • 405
  • 1
  • 6
  • 13
  • 4
    It doesn't look right to pass `NULL` as the second parameter of `CGEventCreateKeyboardEvent`, because it's not a pointer. It happens to work, because the `NULL` is treated as zero, which is a valid key code. Also note that you should say `CFRelease(e)` at the end. – JWWalker Sep 21 '12 at 18:24