[Mono-osx] Close GTK# application with apple+q

ChristianManthey info at manthey-it.de
Fri Sep 24 10:52:58 EDT 2010

After implementing the "Integrating a GTK# Application with the Mac" articel
from Michael Hutchinson i was very much releaved that my GTK# application
was able to react on the shutdown command from OSX. But my user like to use
the CMD+Q (apple+q) Keyboard combination so i was searching for a way to
implement this functionality. 

After some trial and error i came with the following solution, that might
also help others. If someone has a better way of solving this, i'm also
happy for comments on how to improve the below.

1. I used the KeyReleaseEvent from the GTK# application to check whether my
user is trying to quit the application and call the following statement
directly after the Build(); function

             this.KeyReleaseEvent += new

2. As the above mentioned event will be raised two times for cmd+q i created
the following 4 vars in my gtk.Window class.
        private bool appleKey_clicked = false;
        private bool q_clicked = false;
        private int time_control_clicked = 0;
        private int time_q_clicked = 0;

3. The function checkKey has the following logic implemented:

        protected virtual void checkKey (object o, KeyReleaseEventArgs args)
            int time_diff = 0;
            if (args.Event.Key.Equals(Gdk.Key.Meta_L) ||
                appleKey_clicked = true;
                time_control_clicked = (int)args.Event.Time;
            } else if (appleKey_clicked && (args.Event.Key.Equals(Gdk.Key.q)
|| args.Event.Key.Equals(Gdk.Key.Q))){
                q_clicked = true;
                time_q_clicked = (int)args.Event.Time;

            if (appleKey_clicked && q_clicked) {
                time_diff = time_control_clicked - time_q_clicked;
                if (time_diff < 0)
                    time_diff = time_diff * (-1);
                if (time_diff < 60)
                    OnClose(null, null);
                    appleKey_clicked = false;
                    q_clicked = false;
                    time_control_clicked = 0;
                    time_q_clicked = 0;

It's tested on Snow Leopard and seems to work quite well.

View this message in context: http://mono.1490590.n4.nabble.com/Close-GTK-application-with-apple-q-tp2553601p2553601.html
Sent from the Mono - OSX mailing list archive at Nabble.com.

More information about the Mono-osx mailing list