How to simulate a Unicode Char “key press” in Mac Os X using Objective-C?

假如想象 提交于 2019-12-20 14:17:01

问题


I want to simulate a unicode character in Mac OS X to send to the Foreground application. I mean I have a unicode char(can contain Arabic, Chinese, etc.) like 'a' and I want to input it. Please note that I am not trying to use Virtual Keys or Key Codes. Only a character.

Sincerely yours, Peyman Mortazavi


回答1:


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

    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 );

The problem is that this event is limited to some sessions like the search box and this is because kCGAnnotatedSessionEventTap if we change it to kCGSessionEventTap then it will only work on Search Box. Not can you help me to combine them to make it apply this event to every sessions.

Regards, Peyman Mortazavi



来源:https://stackoverflow.com/questions/10179995/how-to-simulate-a-unicode-char-key-press-in-mac-os-x-using-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!