如何模拟Unicode字符“按键"?在Mac OS X中使用Objective-C? [英] How to simulate a Unicode Char "key press" in Mac Os X using Objective-C?

查看:128
本文介绍了如何模拟Unicode字符“按键"?在Mac OS X中使用Objective-C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Mac OS X中模拟一个Unicode字符以发送到Foreground应用程序. 我的意思是我有一个Unicode字符(可以包含阿拉伯语,中文等),例如"a",我想输入它.请注意,我不尝试使用虚拟密钥或密钥代码.只是一个字符.

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.

真心的, 佩曼·莫尔塔扎维(peyman Mortazavi)

Sincerely yours, Peyman Mortazavi

推荐答案

我发现了一种非常好的方法,但是仍然存在问题.看一下代码,然后写出当前的问题.

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

问题在于此事件仅限于某些会话(如搜索框),这是因为 kCGAnnotatedSessionEventTap 如果将其更改为 kCGSessionEventTap ,则该事件仅适用于搜索盒子.您不能帮我将它们结合起来,使它将此事件应用于每个会话.

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.

关于, 佩曼·莫尔塔扎维(peyman Mortazavi)

Regards, Peyman Mortazavi

这篇关于如何模拟Unicode字符“按键"?在Mac OS X中使用Objective-C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆