How to simulate Unicode Char "keystrokes" in Mac Os X using Objective-C?

I want to simulate a Unicode character in Mac OS X to send to a Foreground application. I mean, I have a Unicode char (may contain Arabic, Chinese, etc.), Like "a", and I want to enter it. Please note that I am not trying to use virtual keys or key codes. Only a character.

Sincerely yours, Peyman Mortazavi

+5
source share
1 answer

I found a very good approach, but there is still a problem. look at the code, then I will write what the current problem is.

    CGEventRef downEvt = CGEventCreateKeyboardEvent( NULL, 0, true );
CGEventRef upEvt = CGEventCreateKeyboardEvent( NULL, 0, false );
UniChar oneChar = 'h';
CGEventKeyboardSetUnicodeString( downEvt, 1, &oneChar );
CGEventKeyboardSetUnicodeString( upEvt, 1, &oneChar );
CGEventPost( kCGAnnotatedSessionEventTap, downEvt );
CGEventPost( kCGAnnotatedSessionEventTap, upEvt );

, , , , kCGAnnotatedSessionEventTap, kCGSessionEventTap, Box. , .

,

+5

All Articles