Using CGEventSetIntegerValueField to edit a keyboard event gives error

丶灬走出姿态 提交于 2020-01-04 06:38:38

问题


So, I asked another question here:

How to tell the difference between a user-tapped keyboard event and a generated one?

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.

So, I'm trying:

CGEventSetIntegerValueField(myKeyboardEvent, kCGMouseEventPressure, 0xFEEDFACE);

The problem is:

  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


回答1:


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.

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.



来源:https://stackoverflow.com/questions/7102883/using-cgeventsetintegervaluefield-to-edit-a-keyboard-event-gives-error

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