How to get key code from "SysDefined" Carbon Event
How to get key code from "SysDefined" Carbon Event
- Subject: How to get key code from "SysDefined" Carbon Event
- From: Jerry Krinock <email@hidden>
- Date: Tue, 18 Oct 2011 13:00:40 -0700
I've registered a handler for global hot keys with the Carbon Event Manager, using InstallEventHandler(). However, I'd like the user to be able to specify *two* different global hot key functions. Thus, in my callback/handler, I need the key code and modifier flags of the event, to determine which function they want.
In my handler, I convert the given Carbon EventRef to a NSEvent using +[NSEvent eventWithEventRef:]. If I send this NSEvent a -keyCode, -characters, or -charactersIgnoringModifiers message, an assertion is raised. I understand that this is because the event type is not a key event[1]. Actually, it is this weird "SysDefined" event [2].
I also tried to get the -CGEvent of this event, hoping that it might be the "underlying key event", but unfortunately the result appears to be just a copy of the same SysDefined event, which behaves in the same way. Similarly, -[NSApp currentEvent] gives me the same stupid thing.
Getting the modifier flags (cmd, option, etc.) via -modifierFlags works as desired!
It is odd that Apple would give me the modifier flags but not the key code. Does anyone know how to get the key code, or at least the character, in this situation?
Thanks,
Jerry Krinock
I understand that my handler also gets userData. Unfortunately this userData is useless for my purpose, since it is set in InstallEventHandler() and not in RegisterEventHotKey(). InstallEventHandler() installs the same handler with the same user data for all events of the same type, i.e. kEventClassKeyboard:kEventHotKeyPressed.
I suppose that another way to solve this problem would be if there were some kind of system function that would tell me, post-event, "What was the last key that the user pressed?" Is there such a thing?
[1] http://www.cocoabuilder.com/archive/cocoa/129171-nsevent-handling-in-panther.html?q=NSEvent+keyCode+invalid+message#129171
[2] For example, it logs the following -description…
NSEvent: type=SysDefined loc=(335,409) time=116856.4 flags=0x100108 win=0x0 winNum=0 ctxt=0x26b5b subtype=6 data1=61748032 data2=61748032
The numbers data1 and data2 are always equal to each other, but are different, a higher value, with each hot key event, even if user hits the same hot key. I suppose they could be pointers (this is a 32-bit app), but to what?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden