如何分辨用户点击的键盘事件和生成的键盘事件之间的区别? [英] How to tell the difference between a user-tapped keyboard event and a generated one?

查看:112
本文介绍了如何分辨用户点击的键盘事件和生成的键盘事件之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了键盘挂钩:

I've installed a keyboard hook:

CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {

基本上,我想取用户的键盘水龙头,吃掉输入内容,然后发布我自己的输入内容.

Basically I want to take the user's keyboard taps, eat the input, and post my own input instead.

因此,如果他点按"g",我可能要在文本字段中张贴"foo".

So if he taps "g" I might want to post "foo" to the textfield.

我正在使用CGEventPostCGEventSetUnicodeString写入文本字段,如下所示: http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg23343.html

I'm writing to the textfield with CGEventPost and CGEventSetUnicodeString as found here: http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg23343.html

问题是我以编程方式输入的每个字符都在敲键盘.因此,我不能在键盘挂钩中return NULL来阻止用户输入...也将阻止所有程序的输入!

The problem is that each of my programmatically entered characters is hitting the keyboard hook. So I can't return NULL in the keyboard hook to block the user input... that blocks all the program's input as well!

我在Windows的C#中使用已插入"标志对其进行了区分,请在一年前在此处查看我的问题:

I differentiated them on the Windows side in C# with the 'injected' flag, see my question a year ago here: How do I use low-level 8 bit flags as conditionals?

在Objective-C中寻找类似的东西.

Looking for something similar in Objective-C.

推荐答案

看看CGEventSource.h中的注释.与使用

Take a look at the comments in CGEventSource.h. It's a little bit easier to put the information together than using the Event Services Reference. The long, but more correct, way around looks like creating an event source (which is subject to memory management rules; you need to CFRelease it if you're done using it before program termination):

myEventSource = CGEventSourceCreate(kCGEventSourceStatePrivate);

这将创建您自己的具有唯一ID的私人事件源;然后,您指示您创建的事件来自那里:

This will create your own private event source with a unique ID; you then indicate that events you create came from there:

CGEventRef myKeyboardEvent = CGEventCreateKeyboardEvent(myEventSource, 
                                                        keyCode, true);

有事件发生时,请检查事件是否来自您自己:

When an event comes in, check to see if it's from yourself:

 if( (CGEventGetType(newEvent) == kCGEventKeyDown) &&
     (CGEventGetIntegerValueField(newEvent, kCGEventSourceStateID) == CGEventSourceGetSourceStateID(myEventSource) ) {

还有一个

There's also a user data field for the source that lets you pass around an arbitrary 64 bits, should you need to.

一种快速而肮脏的方法是尝试选择一个不太可能对键盘事件有意义的事件字段,例如kCGMouseEventPressure并将其转换为签名:

The quick and dirty way would be to try picking an event field that isn't likely to be a meaningful value for a keyboard event, like kCGMouseEventPressure and turn it into a signature:

CGEventSetIntegerValueField(myKeyboardEvent, 
                            kCGMouseEventPressure, 
                            0xFEEDFACE);
// The field is an int_64 so you could make the sig longer

这篇关于如何分辨用户点击的键盘事件和生成的键盘事件之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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