The 10.6.3 os x update broke simulated key-presses for Nestopia - objective-c

The iPhone app that I released is a wireless game controller, it translates touches on the device into key-presses on the networked Mac. This allowed for playing emulator (e.g. Nestopia) games using the iPhone as a controller. Of course, the day that I released it coincided with an os x update. After installing this update, the simulated key-presses no longer work in Nestopia! The crazier thing is, when I go to 'File > Open' within Nestopia, I can cycle through the file list by hitting the up-arrow on my iphone controller; i.e. the simulated key-presses work in menu items, but not in the game itself. The code that I use to simulate keys is below. Given the list of changes here, can anyone identify which change would cause this problem?
Thanks!!
#define UP false
#define DOWN true
-(void)sendKey:(CGKeyCode)keycode andKeyDirection:(BOOL)keydirection{
CGEventRef eventRef = CGEventCreateKeyboardEvent(NULL, keycode, keydirection);
CGEventPost(kCGSessionEventTap, eventRef);
CFRelease(eventRef);
}

The author of Mac Nestopia is using an older call, GetKeys(), to capture key events. As of 10.6.3, GetKeys does not catch generated key presses using the methods detailed in this post. The workaround I found was to use this instead:
-(void)sendKey:(CGKeyCode)keycode andKeyDirection:(BOOL)keydirection{
AXUIElementRef axSystemWideElement = AXUIElementCreateSystemWide();
AXError err = AXUIElementPostKeyboardEvent(axSystemWideElement, 0, keycode, keydirection);
if (err != kAXErrorSuccess)
NSLog(#" Did not post key press!");
}
Huge thanks to Richard Bannister for his quick email responses!

I think it's a problem with your code and not with 10.6.3. I have an app I'm writing that simulates key presses, and I've upgraded to 10.6.3, and my simulated key presses still work just fine.
Here's what I'm doing:
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef keyDownPress = CGEventCreateKeyboardEvent(source, (CGKeyCode)keyCode, YES);
CGEventSetFlags(keyDownPress, (CGEventFlags)flags);
CGEventRef keyUpPress = CGEventCreateKeyboardEvent(source, (CGKeyCode)keyCode, NO);
CGEventPost(kCGAnnotatedSessionEventTap, keyDownPress);
CGEventPost(kCGAnnotatedSessionEventTap, keyUpPress);
CFRelease(keyDownPress);
CFRelease(keyUpPress);
CFRelease(source);

Related

(Mac) Simulating keyboard shortcuts - Arrow keys don't work

I'm trying to use Quartz Event Services to programmatically trigger keyboard shortcuts:
CGEventSourceRef src = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
CGEventRef leftDown = CGEventCreateKeyboardEvent(src, 0x7b, true); // creating left arrow down event
CGEventRef leftUp = CGEventCreateKeyboardEvent(src, 0x7b, false); // creating left arrow up event
CGEventSetFlags(leftDown, kCGEventFlagMaskCommand); // setting command key modifier flag
CGEventSetFlags(leftUp, kCGEventFlagMaskCommand); // ""
CGEventTapLocation loc = kCGHIDEventTap;
CGEventPost(loc, leftDown);
CGEventPost(loc, leftUp);
CFRelease(leftDown);
CFRelease(leftUp);
CFRelease(src);
This code works for triggering system functions, if you use something other than an arrow key. But if you do use an arrow key, the code will just make your cursor move instead.
Triggering, for example, command-space with this code (0x31, kCGEventFlagMaskCommand) successfully brings up spotlight, but if you map spotlight to command-left_arrow, and then trigger command-left_arrow with this code, it will just make the cursor jump to the start of the line.
Something interesting to note is that Apple Script behaves the exact same way when I try to trigger system shortcuts incorporating arrow keys with it.
Is there a workaround for this?
Thank you for your help, I appreciate it.
For some reasons the arrow keys only work with a "private" CGEventSourceStateID in the way that you expect it. Change the line
CGEventSourceRef src = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
to
CGEventSourceRef src = CGEventSourceCreate(kCGEventSourceStatePrivate);
and it will work as expected. (Tested on macOS 10.13.4)

Mac OS X: Intercept keyboard layout change

I have a problem. I have two keyboard layouts in my Mac because I have to type in two different languages while communicating with different people. I use the keyboard shortcut Cmd+Space to switch from one layout (language) to another.
I wonder if I can run custom script when Cmd+Space is pressed? I know there is an app called Punto Switcher that can do that.
My idea is to change keyboard highlighting level to indicate current language.
Bright = German (or Russian or whatever)
Dim = English
The question is where to find API that can
intercept keyboard layout in Mac OS X
change brightness of the keyboard highlight
Neat pointer to the LED brightness stuff from #Anoop Vaidya -- looks interesting!
The system sends a notification when the input method changes.
First, declare a function to receive the notification:
void theKeyboardChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
NSLog(#"Keyboard/input method changed.");
}
Then register for the change notification:
CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(),
myContextInfo, theKeyboardChanged,
kTISNotifySelectedKeyboardInputSourceChanged, NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
I found a blog of Amit Singh, where he gave idea as in undocumented APIs , he used C, for this, you can surely find some sort of help from this.
Experimenting With Light.
Or you can try with these codes:
UInt64 lightInsideGetLEDBrightness(){
kern_return_t kr = 0;
IOItemCount scalarInputCount = 1;
IOItemCount scalarOutputCount = 1;
UInt64 in_unknown = 0, out_brightness;
kr = IOConnectCallScalarMethod(dataPort, kGetLEDBrightnessID, &in_unknown, scalarInputCount, &out_brightness, &scalarOutputCount);
return out_brightness;
}

headphone plug-in plug-out event when audio route doesn't change - iOS

I'm working on iPad.
I would like to detect when user plug-out headphone. First I used a listener on the property kAudioSessionProperty_AudioRouteChange. So all was working well until I've decided to add a button to switch to speakers when headphone was still plugged. So I’m now facing a problem, maybe someone would have an idea to fix it.
Here is the scenario :
I plug a headphone -> my audio route change callback is called
then I switch sound to speakers (without unplugging my headphone) -> audio route change callback is called
then I unplug my headphone (when sound is still outputting to speakers) -> audio route change callback is NOT called, which seems logical.
But here is my problem ! So my question is : Do you see a way to detect that headphone was unplugged for this last case ?
Thanks for your help
EDIT :
Ok I found a workaround :
To detect whether or not headphones are plugged, I execute a test function all the times I need to know it (instead using a boolean), this might be less good for performances but it's working, here is my code for the ones who may need it :
//set back the default audio route
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);
//check if this default audio route is Heaphone or Speaker
CFStringRef newAudioRoute;
UInt32 newAudioRouteSize = sizeof(newAudioRoute);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &newAudioRouteSize, &newAudioRoute);
NSString *newAudioRouteString = (__bridge NSString *)newAudioRoute;
CFRelease(newAudioRoute);
//if this default audio route is not Headphone, it means no headphone is plugged
if ([newAudioRouteString rangeOfString:#"Headphones"].location != NSNotFound){
NSLog(#"Earphone available");
return true;
}
else {
NSLog(#"No Earphone available");
return false;
}
Hope it will help someone !
I imagine a solution it in the following way:
You create in the AppDelegate a boolean for the speakers, let's say:
BOOL isSpeakerOn. And every time the audio route callback is called you have to verify what the current situation with the speakers and what you want to do then.
This is the best tutorial dealing this issue:
http://www.techotopia.com/index.php/Detecting_when_an_iPhone_Headphone_or_Docking_Connector_is_Unplugged_(iOS_4)

Send auto-repeated key using CoreGraphics methods (Mac OS X Snow Leopard)

I have been successful sending keystrokes in order to automate a particular software package for drawing that I use. This software relies a lot of keyboard shortcuts so I wrote something that could call some of these keyboard shortcuts in order to streamline my workflow. As I said, this has worked out good.
My library is a Cocoa library that is loaded as a plugin to the software package. Here is a snippet of code that I have been using for sending my keystrokes.
CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
CGEventRef eventDown = CGEventCreateKeyboardEvent(eventSource, (CGKeyCode)1, true);
CGEventRef eventUp = CGEventCreateKeyboardEvent(eventSource, (CGKeyCode)1, false);
//setting the process number somewhere else
CGEventPostToPSN(&number, eventDown);
CGEventPostToPSN(&number, eventUp);
For some procedures in the drawing package if you continue to hold the Shift key then you activate a special tool. I have been unable to simulate this. I thought I could send the Shift key and say that I wanted it to auto-repeat but that doesn't seem to work. I have been using the following code to set the auto-repeat:
//This is done before sending the key
CGEventSetIntegerValueField(eventDown, kCGKeyboardEventAutorepeat, 1);
In my testing I have been unable to make any key auto-repeat. It just send the key once and that is it.
Is there anyone that have been successful autorepeating a key using the above method? I have searched the Internet for answers but all I have found are some unanswered questions from 2008... Any help is greatly appreciated.
Thanks,
mobbe
The code that OP finally came up with to solve the problem (transferred here from a comment under other answer):
CGEventRef flagsChanged = CGEventCreate(eventSource);
CGEventSetType(flagsChanged, kCGEventFlagsChanged);
CGEventSetIntegerValueField(flagsChanged, kCGKeyboardEventKeycode, 56);
CGEventSetFlags(flagsChanged, 131330);
CGEventPostToPSN(&number, flagsChanged);
CFRelease(flagsChanged); CFRelease(eventSource);
131330 is a constant indicating the Shift key; it is related to NSShiftKeyMask and kCGEventFlagMaskShift, which are 131072 (0x00020000). 131330 - 256 - 2 == 131072.
UPDATE: the Shift key's code isn't 56, according to Events.h:
...
kVK_Shift = 0x38,
...
(EDIT: of course those of you who are paying attention (I wasn't) realize that HEX 38 == DEC 56.)
I also realized how to get modifier key presses: flagsChanged:. So using this keycode, I can catch Shift key presses in flagsChanged:, and the repeat works fine; I also get repeated key events for "normal" keys in keyDown: and keyUp: without difficulty.
It sounds like you may not have access to/want to change the event-handling code (to add flagsChanged:) though, so if that keycode doesn't work for you, I'm stumped and can only say "Good luck!"
I believe that the field you're setting is used to indicate not that the event should be repeated by the system, but that an event is an auto-repeat of a previous event. You still have to generate each event yourself. Something like (EDITED to use a timer instead of a loop):
CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
CGEventRef eventDown = CGEventCreateKeyboardEvent(eventSource, (CGKeyCode)1, true);
// Post the initial keydown
CGEventPostToPSN(&pidNumber, eventDown);
// Keep track of how many Shift keydown events have been sent
shiftKeyRepeatCount = 1;
[NSTimer scheduledTimerWithTimeInterval:0.3 // I don't know exactly what the interval should be, of course
target:self
selector:#selector(repeatShiftKeyDown:)
userInfo:nil
repeats:YES];
CFRelease(eventDown);
- (void)repeatShiftKeyDown:(NSTimer *)tim {
if( shiftKeyRepeatCount >= kRepeatCountForSpecialTool ){
[tim invalidate];
[self sendShiftKeyUp];
return;
}
shiftKeyRepeatCount++;
GEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
CGEventRef eventDown = CGEventCreateKeyboardEvent(eventSource, (CGKeyCode)1, true);
// Set the auto-repeat field
CGEventSetIntegerValueField(eventDown, kCGKeyboardEventAutorepeat, 1);
CGEventPostToPSN(&pidNumber, eventDown);
CFRelease(eventDown);
}
I'm not certain whether you can reuse the first event with a changed field or you'll need to generate a new event to use when repeating.

Issue with CGEventTapCreate() call

I'm trying to register for global key events using this code :
void function()
{
CFMachPortRef keyUpEventTap = CGEventTapCreate(kCGHIDEventTap,kCGHeadInsertEventTap,kCGEventTapOptionListenOnly,CGEventMaskBit(kCGEventKeyUp),&keyUpCallback,NULL);
CFRunLoopSourceRef keyUpRunLoopSourceRef = CFMachPortCreateRunLoopSource(NULL, keyUpEventTap, 0);
CFRelease(keyUpEventTap);
CFRunLoopAddSource(CFRunLoopGetCurrent(), keyUpRunLoopSourceRef, kCFRunLoopDefaultMode);
CFRelease(keyUpRunLoopSourceRef);
}
The application crashes while executing CFMachPortCreateRunLoopSource() call. I think the crash is because of CGEventMaskBit(kCGEventKeyUp) when I create an event tap.
But if I create event tap using CGEventTapCreate(kCGHIDEventTap,kCGHeadInsertEventTap,kCGEventTapOptionListenOnly,CGEventMaskBit(kCGEventFlagsChanged),&keyUpCallback,NULL), the application works fine. It does not crash. I'm getting callbacks when any modifier key is pressed. But I need to get callbacks for delete key pressed.
Any ideas?
Thanks,
Dheeraj.
I think you need special permission to register for keyboard events. I forget off hand what that is, but to test it run the program as root and see if it still crashes.
Edit:
According to this article you must either run the program as root or enable assistive devices.
The crash may just be because CGEventTapCreate returns NULL.