使用CGEventSetIntegerValueField编辑键盘事件会产生错误 [英] Using CGEventSetIntegerValueField to edit a keyboard event gives error

查看:168
本文介绍了使用CGEventSetIntegerValueField编辑键盘事件会产生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我在这里问了另一个问题:

So, I asked another question here:

如何分辨用户点击的键盘事件与生成的键盘事件之间的区别?

出现的答案之一是根据需要使用鼠标压力"字段(不太可能用于键盘事件)标记事件.

One of the answers that came up was to use the "mouse pressure" field (unlikely to be used for a keyboard event) to mark the event as needed.

所以,我正在尝试:

CGEventSetIntegerValueField(myKeyboardEvent, kCGMouseEventPressure, 0xFEEDFACE);

CGEventSetIntegerValueField(myKeyboardEvent, kCGMouseEventPressure, 0xFEEDFACE);

问题是:

  1. 我不知道0xFEEDFACE是什么意思.该值默认为什么?我应该设置为什么?我如何检查它是否已被我标记?
  2. 运行上面的代码给我一个错误:
  1. I don't know what 0xFEEDFACE means. What does that value default to? What should I set it to? How can I check whether it has been marked by me?
  2. Running the above code gives me an error:

<Error>: kCGErrorIllegalArgument: CGEventSetIntegerValueField: invalid event

推荐答案

0xFEEDFACE只是一个虚拟值.这是许多带有愚蠢的十六进制拼写的4字节数字之一,当您查看内存时,它将脱颖而出.

0xFEEDFACE is just a dummy value. It's one of a number of 4-byte numbers with silly hex spellings that will stand out when you're looking at memory.

我用 0xFEEDFACE表示您可以尝试在其中放置一个任意的"magic"值,然后在接收到该事件时使用

The function takes three arguments: an event, a CGEventField (which is an enum), and an int64_t (8-byte signed integer) whose range of valid values probably depends on the field. I used 0xFEEDFACE to indicate that you could try putting an arbitrary "magic" value in there,* and then check for it when you received the event, using CGEventGetIntegerValueField. If your event tap receives a new event that has your "magic number", then it was (almost certainly) sent by you. Otherwise, it was sent by the system.

总结一下并回答您的明确问题:该参数不默认为任何值.您应该传递事件系统不太可能使用的任意内容(您可能希望检查系统发送的一系列事件,以查看真实"值是什么样的);这个想法是,您可以使用任意值来检查事件是否由您标记.

To sum up and answer your explicit questions: that argument doesn't default to anything; you should pass something arbitrary that is unlikely to be used by the events system (you might want to inspect a series of events sent by the system to see what "real" values look like); the idea is that your arbitrary value allows you to check that the event was marked by you.

至于错误,看来我是错的.我认为您可以在任何类型的事件上设置任何字段(例如,在键盘事件上使用鼠标压力"字段,因为系统几乎肯定不会设置该字段)以传递任意信息(并且因此,请确定您自己构建的事件).但是,事件服务似乎不允许您这样做.您可能还可以使用其他字段,但是我不确定.

As for the error, it looks like I was wrong. I thought that you might be able to set any field on any kind of event (e.g., using the "mouse pressure" field on a keyboard event, since the system almost certainly won't have set that) to pass along arbitrary information (and so identify the events that you've constructed yourself). It seems, however, that Event Services won't let you do that. There may be other fields that you can use, but I don't know for sure.

*我以为您会理解这是一个垃圾值;抱歉.

*I assumed you would understand that it was a junk value; sorry.

这篇关于使用CGEventSetIntegerValueField编辑键盘事件会产生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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