AXUIElementPostKeyboardEvent and Carbon applications
AXUIElementPostKeyboardEvent and Carbon applications
- Subject: AXUIElementPostKeyboardEvent and Carbon applications
- From: AstroK Software <email@hidden>
- Date: Wed, 22 Sep 2010 00:59:55 +0200
- Mailscanner-null-check: 1285714797.73131@DdnOdD6wxFgnbWW04y4ssg
Hi,
I am writing a Cocoa application for disabled that must be able to send key presses to background application. For that I am using AXUIElementPostKeyboardEvent which works quite fine, except when I want to send key presses to Carbon apps like Microsoft Word or iTunes when they are in the background. For Cocoa apps there is no absolutely no problem! Have you ever encountered a behavior like this? Maybe I forgot to call some function...
Basically, what my code does is to install an "event tap" that receives all key presses. If the key presses are for a background application, I retrieve the UI element with AXUIElementCopyElementAtPosition(), find the application it belongs by going up in the parent/children chain and then use the following code to effectively send the key presses event:
if (type == kCGEventKeyDown)
{
keyDown = true;
err = AXUIElementPostKeyboardEvent(backgroundApp, (CGCharCode)0, (CGKeyCode)keyCode, keyDown);
}
else if (type == kCGEventKeyUp)
{
keyDown = false;
err = AXUIElementPostKeyboardEvent(backgroundApp, (CGCharCode)0, (CGKeyCode)keyCode, keyDown);
}
else if (type == kCGEventFlagsChanged)
{
switch (keyCode)
{
case kCKShiftKey:
keyDown = (modifiers & kCGEventFlagMaskShift) == kCGEventFlagMaskShift;
break;
case kCKCommandKey:
keyDown = (modifiers & kCGEventFlagMaskCommand) == kCGEventFlagMaskCommand;
break;
case kCKOptionKey:
keyDown = (modifiers & kCGEventFlagMaskAlternate) == kCGEventFlagMaskAlternate;
break;
case kCKControlKey:
keyDown = (modifiers & kCGEventFlagMaskControl) == kCGEventFlagMaskControl;
break;
case kCKCapsLockKey:
keyDown = (modifiers & kCGEventFlagMaskAlphaShift) == kCGEventFlagMaskAlphaShift;
break;
}
if (keyDown == true)
err = AXUIElementPostKeyboardEvent(backgroundApp, (CGCharCode)0, keyCode, keyDown);
else
err = AXUIElementPostKeyboardEvent(backgroundApp, (CGCharCode)0, keyCode, keyDown);
}
This is strange because when Carbon apps are in the foreground it works fine. Do you think it could be related to the fact that my application is written in Cocoa? I ask this specific question because I have found the CloneKeys project (http://code.google.com/p/clonekeys/) which allows broadcasting key presses to several applications at the same time, and it works with Carbon applications in the background. I didn't have time to study its source code because it's Carbon and C++, and I am not familiar with any of them
If anyone can help me, it would be most welcomed :)
-- Arthur;
--
Arthur VIGAN
AstroK Software
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Accessibility-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden