Issue with CGEventTapCreate() call - objective-c

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.

Related

how to see mouseReleased() event outside canvas using processing.js

I have an object I want to drag around the screen with the mouse in Processing. I set acquired to true on mouse down over the object, and to false on mouse up, thus:
void mousePressed() {
if (overThing()) {
acquired = true;
}
}
void mouseReleased() {
acquired = false;
}
I then query acquired in my update(), and drag the object if it is true.
void update() {
\\ other stuff...
if (acquired) {
\\ drag thing code ...
}
}
This all works fine in Processing. mouseReleased() gets called whether I release the mouse inside or outside the active window.
However, when I move the code to Chrome, using processing.js (v1.4.8), mouseReleased() is not called if I release the mouse outside the canvas (whether the mouse is still over the web page, or outside the browser window). So when I return the (now unclicked) mouse to the canvas, the object is still getting dragged around.
I tried including a test of mousePressed in update(), but that also returns true in these circumstances.
Any help on what I need to do to make mouse state changes outside the canvas visible with processing.js?
I don't know about Processing specifically, but releasing mouse buttons outside a widget is a common issue in GUI development.
I suspect that you have no way of knowing the precise time when the mouse is released outside the widget, but you do have two options:
Set acquired = false in mouseOut(), as #Kevin suggests.
I assume there is some type of mouseEntered() method in Processing, and also some way of knowing if the mouse button is currently pressed (either a global variable, or an event object passed to mouseEntered()). You can catch the mouse entered event, check if the mouse has been released, and set acquired = false then.
Like so:
void mouseEntered() {
if (mouse button is pressed) {
acquired = false;
}
}
Edit: From your comments, #Susan, it seems like there is a bug in processing.js, where mousePressed is not set to false if the mouse button is released outside the canvas. One thing pointing to this being a bug is that the mouse movement example on the processing website also shows this behaviour.
Depending upon how much control you have over the website this is going on, and how much effort you want to go to, you could fix the bug yourself by writing some javascript (separate from your processing code):
Define a mouseUp() event on the page <body>, to catch all mouse release events on the page.
In the mouseUp() event, check if the event comes from your Processing control. (There is probably an event object passed to the mouseUp() function, and you might have to give your Processing control an ID to identify it)
If the event doesn't come from your Processing control, then fire a mouseUp event yourself, on the Processing control. This should (hopefully!) trigger a mouse event inside your Processing code.
I'm not sure what Processing will make of the mouse (x,y) position being outside its control when it handles the event you send it. You might want to set a flag on the event object (assuming you can add extra data to the event object) to say "don't use the (x,y) position of this event - it's outside the control".
Edit2: It was easier than I thought! Here is the JavaScript code to detect the mouse being released outside of the Processing canvas and send the mouseReleased event to the canvas. I've tested it on the mouse movement example from the Processing website, and it fixes the bug.
It uses jQuery (although it could be re-written to not use jQuery), and it assumes your Processing canvas has the ID "processingCanvas":
$(':not(processingCanvas)').mouseup(function(){
Processing.getInstanceById('processingCanvas').mouseReleased();
});
To use this code, include it anywhere in your page (in a JavaScript file or in <script> tags) and make sure you have the jQuery library included before this code.
The Processing object allows JavaScript to call any functions defined in your Processing code. Here I've used it to call Processing's built in mouseReleased() function, but if you wanted to call a custom function to handle the mouse-released-outside state differently, then you could.
You should use the mouseOut() function to detect when the mouse leaves the sketch:
void mouseOut() {
acquired = false;
}
More info in the reference here.

Catching Logoff (not power off) event on MAC using objective C

Can anybody tell me how can I catch user logoff event? I am able to catch poweroff event using NSWorkspaceWillPowerOffNotification notification. But it is not showing any difference in logoff and power off events. I want to catch log off and power off separately. I am able to catch the same on windows.
First, you have to make sure your app is not participating in sudden termination. If it is, then it can be killed at logout without any opportunity to react.
Now, when the user logs out, your app will get a kAEQuitApplication ('quit') Apple Event. This will have the same effect as an invocation of -[NSApplication terminate:]. You can implement the application delegate method -applicationShouldTerminate: to be notified of the request to quit and control your app's response to it.
In that delegate method, you can use code like this to examine the quit event and learn the reason for the quit request:
NSAppleEventManager* m = [NSAppleEventManager sharedAppleEventManager];
NSAppleEventDescriptor* desc = [m currentAppleEvent];
switch ([[desc attributeDescriptorForKeyword:kAEQuitReason] int32Value])
{
case kAELogOut:
case kAEReallyLogOut:
// log out
break;
case kAEShowRestartDialog:
case kAERestart:
// system restart
break;
case kAEShowShutdownDialog:
case kAEShutDown:
// system shutdown
break;
default:
// ordinary quit
break;
}

CoreDispatcher.ProcessEvents() causes an indirect crash?

I have to port some legacy code, that uses modal dialog boxes all over the place to Metro/WinRT (using C++/CX). Because these dialog boxes provide their own message loop (using DialogBoxParam()), the calling code will wait until the user has clicked a button on the message box.
I'm currently trying to write a replacement for the old message box class, that uses XAML and the popup control. To reproduce the same behavior, I have to wait in the calling thread, but also have to keep the UI responsive. I've found out, that CoreDispatcher::ProcessEvents() can be used in a loop, to keep processing events (yeah I realize that this isn't very beautiful, but I don't want to change all of our legacy code to a new threading model). However I'm running into an issue that keeps crashing my app.
Here is a minimal example that reproduces the issue (just create a XAML app and wire this to a button):
void CPPXamlTest::MainPage::Button_Click_1(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
bool cancel = false;
auto popup = ref new Popup();
auto button = ref new Button();
button->Content = "Boom";
auto token = (button->Click += ref new RoutedEventHandler([&cancel] (Object ^, RoutedEventArgs ^) { cancel = true; }));
popup->Child = button;
popup->IsOpen = true;
while (!cancel)
{
Window::Current->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
}
popup->IsOpen = false;
button->Click -= token;
}
This seems to work well for the first one or two tries of opening and closing the popup, using the two buttons. After a few tries however, the application will instantly crash deep in Windows.UI.Xaml.dll, while trying to dereference a null pointer. I can also reproduce this in C# (with practically the same code).
Does anyone have an idea, what is going on in here? Or a suggestion for an alternative approach?
If anyone is interested: I asked the same question a few days later on the MSDN forums and got a response there from a Microsoft employee:
http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/11fa65e7-90b7-41f5-9884-80064ec6e2d8/
Apparently the problem here is the nested message loop that is caused by calling ProcessEvents inside an event handler. It seems that this is not supported by WinRT, but instead of failing in a well-defined manner, this will or may cause a crash.
Alas this was the best and only answer I could find, so I ended up working around the problem, by dispatching the event handler (and a lot of other code) into another thread. I could then emulate the waiting behavior of DialogBox()/DialogBoxParam() (outside the main thread), by waiting on an event that was signaled when the user clicked/tapped a button on my XAML "dialog" popup.
A workaround that works fine for me is to replace the line:
Window::Current->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
with:
auto myDispatchedHandler = ref new DispatchedHandler([&](){
Window::Current->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
});
dispatcher->RunAsync(CoreDispatcherPriority::Normal,myDispatchedHandler);
For more info see this post at MSDN.

Objective-C: Listen to keyboard shortcuts and act on them

I am developing an app for Mac OS X and I need to listen to keyboard shortcut inputs so I can act on them.
For example:
Up and down keys move up and down on a table view.
⌘ + ⌦ drops an item.
⌘ + ⇧ + N creates a new item.
It shouldn't be restricted to events on the focused control.
Any ideas?
Thanks in advance.
I think your best option* would be +[NSEvent addLocalMonitorForEventsMatchingMask:handler:]. This creates an object which will call a block handler whenever your application receives an event of the specified type. The handling takes place right before your NSApplication dispatches the event to a window, and you have the opportunity to modify the event or stop it from proceeding further.
You can thus catch key down events as they get passed to your app and do whatever you like with them before any controls get a chance to see them. I posted this originally in another question, but here's a snippet for doing things with arrow key presses:
NSEvent * (^monitorHandler)(NSEvent *);
monitorHandler = ^NSEvent * (NSEvent * theEvent){
switch ([theEvent keyCode]) {
case 123: // Left arrow
NSLog(#"Left behind.");
break;
case 124: // Right arrow
NSLog(#"Right as always!");
break;
case 125: // Down arrow
NSLog(#"Downward is Heavenward");
break;
case 126: // Up arrow
NSLog(#"Up, up, and away!");
break;
default:
break;
}
// Return the event, a new event, or, to stop
// the event from being dispatched, nil
return theEvent;
};
// Creates an object we do not own, but must keep track of so that
// it can be "removed" when we're done; therefore, put it in an ivar.
eventMon = [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask
handler:monitorHandler];
See the Event-Handling Guide for some details about what you're supposed to do with that monitor object. Specifically, Apple apparently "discourages" removing it inside of dealloc, but doesn't give a reason.
*So long as you can require Snow Leopard, at least.
You may need to implements callbacks in your application. Take a look to CGEventTapCreate to start listening for hotkeys.
CGEventTapCreate(kCGSessionEventTap,
kCGTailAppendEventTap,
kCGEventTapOptionDefault,
kCGEventKeyDown
myEventTapCallback,
NULL);
myEventTapCallback should be conform to CGEventTapCallBack and gets called when a key is pressed. Then, inside myEventTapCallback you'll have enough information on keys pressed and you can implement your custom functionality.
Have a look at the Cocoa Event-Handling Guide. There are a few places you can intercept events before they get to the key view. You can intercept all events in the application by overriding -[NSApplication sendEvent:], or you can intercept events at a per-window level by overriding-[NSWindow sendEvent:].

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.