Application exit event - objective-c

I am developing an application in cocoa. My application shows a pop up sheet initially. I need to know which event is fired when we try to exit the application by right clicking and selecting "exit" on the icon in dock, because I can't exit the application because of the popup sheet. How can I solve this?

Your app is sent a quit Apple Event when the Quit item is selected in the Dock menu. If you want to intercept this you will need to install a custom Apple Event Handler for this event. Note that it's normal for sheets to prevent application termination until the sheet is dismissed, so if you change this behavior your app will work differently to other applications.
Here is a simple example of how to override the default handler for quit Apple Events in your application delegate:
- (void)applicationDidFinishLaunching:(NSNotification*)notification
{
//install the custom quit event handler
NSAppleEventManager* appleEventManager = [NSAppleEventManager sharedAppleEventManager];
[appleEventManager setEventHandler:self andSelector:#selector(handleQuitEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEQuitApplication];
}
//handler for the quit apple event
- (void)handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
{
[self terminate:self];
}

Related

wxWidgets Closing window from system menu

I am working on wxWidgets, In wxFrame I handled wxEVT_CLOSE event, inside the event handler I called Iconize(). When pressing the close button my window will be minimized it is work correctly. But while clicking quit from my system menu that time also my window is minimizing. But in my scenario I want to close my window from system menu and want to minimize while clicking close button.
This is my handler.
void Frame::OnClose(wxCloseEvent& event)
{
Iconize(true);
}
You can't distinguish between closing the window from the system menu and using the close button at wxWidgets API level. You can, however, do it using platform-specific code. For example, for MSW you can override MSWWindowProc() in your wxFrame-derived class and handle WM_SYSCOMMAND there and explicitly exit the application, instead of just iconizing it, when SC_CLOSE is received.
Just process menu command in other handler and call Destroy directly.
BEGIN_EVENT_TABLE(wxMyFrame,wxFrame)
EVT_MENU(wxMyFrame::idMenuQuit, wxMyFrame::OnMenuClose) // sample for using event table
END_EVENT_TABLE()
void wxMyFrame::OnMenuClose(wxCommandEvent& event)
{
Destroy();
}

Dojo emit click event to dojox.mobile.ToolBarButton

Im trying to fire a click event on a tool bar button Dojo 1.8.1.
<div data-dojo-type="dojox.mobile.ToolBarButton" class="backButton" moveTo="cartView" transition="slide">
I have tried
function backClick(){
var backButton = dojo.query(".backButton", dojo.byId(currentView.id))[0];
writeLog("backClick::"+backButton);
if(backButton){
var backDijit = dijit.registry.byId(backButton.id);
writeLog("backDijit::" + backDijit.id);
writeLog("emit Click");
backDijit.emit("click", {bubbles:true});
writeLog("emit mousedown");
backDijit.emit("mousedown",{bubbles:true});
writeLog("emit mouseup");
backDijit.emit("mouseup",{bubbles:true});
writeLog("touchstart");
backDijit.emit("touchstart");
writeLog("touchEnd");
backDijit.emit("touchend");
}else{
//Exit App notification
navigator.app.exitApp();
}
}
mousedown/mouseup combo works on the browser.
But on the device it does not work(Android). What is the event that i have to send to 'click' the button on a device?
It seems that the sequence of events that will activate the toolbar button 'click' on a touch device is touchstart followed by touchend.
I suppose that your example do not work because the synthetic touch events that you are emitting do not contains any data, which fails the different methods that handle it.
Another way to trigger the click handler of the toolbar button would be to call its _onClick method.

mouse clicks and keystrokes count clicked any where

I want to implement a functionality in my mac application which will return me the mouse clicks and keystrokes count clicked any where (in my application or outside). Please guide me.
Thank You
NSEvent's + (id)addGlobalMonitorForEventsMatchingMask:(NSEventMask)mask handler:(void (^)(NSEvent*))block provides this functionality.
Here is a quick example:
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask|NSLeftMouseDownMask handler:^(NSEvent *anEvent){
if(anEvent.type==NSKeyDown)
{
NSLog(#"Had key down event: %#",anEvent);
}
if(anEvent.type==NSLeftMouseDown)
{
NSLog(#"Had left mouse down event: %#",anEvent);
}
}];
You could add this somewhere during your apps initialization, for example in you application delegate's -applicationDidFinishLaunching: method.
The above does NOT register events in your application, only in other applications. If you also need events in your app, you can add a local monitor (slightly different as it returns an event):
[NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask|NSLeftMouseDownMask handler:^NSEvent *(NSEvent *anEvent){
if(anEvent.type==NSKeyDown)
{
NSLog(#"Had local key down event: %#",anEvent);
}
if(anEvent.type==NSLeftMouseDown)
{
NSLog(#"Had local left mouse down event: %#",anEvent);
}
return anEvent;
}];
Also note that according to the NSEvent documentation: Key-related events may only be monitored if accessibility is enabled or if your application is trusted for accessibility access (see AXIsProcessTrusted).

MessageBox.Show in App Closing/Deactivated events

I have a MessageBox being shown in Application Closing/Deactivated methods in Windows Phone 7/8 application. It is used to warn the user for active timer being disabled because app is closing. The App Closing/Deactivated events are perfect for this, because putting logic in all application pages would be a killer - too many pages and paths for navigation. This works just fine - message box displays OK in WP7.
I also know for breaking changes in the API of WP8. There it is clearly stated that MessageBox.Show in Activated and Launching will cause exception.
The problem is that in WP8 the message box does not get shown on app closing. Code is executed without exception, but no message appears.
P.S. I've asked this on MS WP Dev forum but obviously no one knew.
Move the msgBox code from the app closing events and into your main page codebehind. Override the on back key press event and place your code there. This is how it was done on 7.x:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
if (MessageBox.Show("Do you want to exit XXXXX?", "Application Closing", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
{
// Cancel default navigation
e.Cancel = true;
}
}
FYI - On WP8 it looks like you have to dispatch the MsgBox Show to a new thread.
This prompts the user before the app ever actually starts to close in the event model. If the user accepts the back key press is allowed to happen, otherwise its canceled. You are not allowed to override the home button press, it must always go immediately to the home screen. You should look into background agents to persist your timer code through suspend / resume.
Register BackKeyPress event on RootFrame.
RootFrame.BackKeyPress += BackKeyPressed;
private void BackKeyPressed(object sender, CancelEventArgs e)
{
var result = (MessageBox.Show("Do you want to exit XXXXX?", "Application Closing", MessageBoxButton.OKCancel));
if (result == MessageBoxResult.Cancel)
{
// Cancel default navigation
e.Cancel = true;
}
}

How can I interact with "iPad keyboard hiding button" programmatically?

There is a button at bottom right of iPad keyboard which is to hide the keypad.
How can I interact with it programmatically? (get the button then send UIControlEventTouchUpInside to it).
Does anyone know this?
[Edit]
In my case, the keyboard is shown on a modal view.
Overriding disablesAutomaticKeyboardDismissal to return NO as below allows you to dismiss the keyboard when you resignFirstResponder, even when your UITextView is on a modal view. You should put this code to your view controller, from which you initiate the keyboard:
- (BOOL)disablesAutomaticKeyboardDismissal {
return NO;
}
Source: https://stackoverflow.com/a/6268520
In general, you would send the resignFirsResponder message to the active input view.
Something like this? I can't remember where I found this code but I used it to toggle the on-screen keyboard because it would be hidden by default if a bluetooth one was connected.
- (void) toggleKeyboard(UIKeyboardImpl * keyImpl){
if (UIKeyboardAutomaticIsOnScreen()) {
UIKeyboardOrderOutAutomatic();
} else {
UIKeyboardOrderInAutomatic();
}
Edit
I found where I got this code from. It works fine but the catch is that you need to import the private framework GraphicsServices, which would most likely get your app rejected from the App store.