More on Keyboard Event posting
-
Hi everyone,
I eventually figured out my previous problem with keyboard event
posting when a colleague of mine pointed out the CGPostKeyboardEvent
had been deprecated. After digging around some more, we came up with
the following:
[window makeFirstResponder:textfield];
CGEventSourceRef sourceRef =
CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
if (sourceRef == nil) { return; }
//40 = "k"
CGEventRef eventDown = CGEventCreateKeyboardEvent(sourceRef, 40, true);
CGEventSetFlags(eventDown, kCGEventFlagMaskAlternate
+kCGEventFlagMaskShift);
CGEventRef eventUp = CGEventCreateKeyboardEvent(sourceRef, 40, false);
CGEventPost(kCGAnnotatedSessionEventTap, eventDown);
CGEventPost(kCGAnnotatedSessionEventTap, eventUp);
CFRelease(eventDown);
CFRelease(eventUp);
CFRelease(sourceRef);
Running that code will correctly produce an apple symbol in the
TextField. Other local commands that we send (Command-A, Command-C,
Command-Q, etc) all work correctly. Even shortcuts for Services work.
However some keyevents, such as Command-Space to activate Quicksilver
on my machine (usually Spotlight) or Shift-Command-3 to take a
screenshot or Command-Tab to activate the application switcher, only
produce a system beep. Any ideas why and how we can get around that?
We thought it might be the event source state used to create the
CGEventSourceRef, but changing it to kCGEventSourceStateHIDSystemState
or even kCGEventSourceStatePrivate did not produce the desired results.
Any direction would be appreciated.
Thanks,
Dave -
On Sep 29, 2008, at 10:25 AM, Dave DeLong wrote:
> However some keyevents, such as Command-Space to activate
> Quicksilver on my machine (usually Spotlight) or Shift-Command-3 to
> take a screenshot or Command-Tab to activate the application
> switcher, only produce a system beep. Any ideas why and how we can
> get around that?
>
> We thought it might be the event source state used to create the
> CGEventSourceRef, but changing it to
> kCGEventSourceStateHIDSystemState or even kCGEventSourceStatePrivate
> did not produce the desired results.
I think the problem is the event tap point at which you're inserting
the event. Instead of kCGAnnotatedSessionEventTap, try
kCGSessionEventTap or kCGHIDEventTap.
Cheers,
Ken -
Rock on! Changing that eventTap to kCGHIDEventTap worked perfectly!
Thank you so much!
Dave
On Sep 29, 2008, at 4:16 PM, Ken Thomases wrote:
> On Sep 29, 2008, at 10:25 AM, Dave DeLong wrote:
>
>> However some keyevents, such as Command-Space to activate
>> Quicksilver on my machine (usually Spotlight) or Shift-Command-3 to
>> take a screenshot or Command-Tab to activate the application
>> switcher, only produce a system beep. Any ideas why and how we can
>> get around that?
>>
>> We thought it might be the event source state used to create the
>> CGEventSourceRef, but changing it to
>> kCGEventSourceStateHIDSystemState or even
>> kCGEventSourceStatePrivate did not produce the desired results.
>
> I think the problem is the event tap point at which you're inserting
> the event. Instead of kCGAnnotatedSessionEventTap, try
> kCGSessionEventTap or kCGHIDEventTap.
>
> Cheers,
> Ken
>



