Murga-Projects Forums
Escape Key exits window - Printable Version

+- Murga-Projects Forums (http://www.murga-projects.com/forum)
+-- Forum: Project Forums (/forumdisplay.php?fid=1)
+--- Forum: MurgaLua - General (/forumdisplay.php?fid=2)
+--- Thread: Escape Key exits window (/showthread.php?tid=51)


Escape Key exits window - iGame3D - 06-21-2007 06:39 AM

How do I prevent the escape key from closing or hiding a window?

In some instances I might want this, but having control over it would be better.


RE: Escape Key exits window - mikshaw - 06-22-2007 07:38 AM

I haven't found anything in the FLTK documentation that I could see relating to it, but the "keyboard" c++ application that comes with the FLTK source will not close when Esc is pressed. I don't yet know how it's done, or whether or not it will work in murgaLua.


RE: Escape Key exits window - JohnMurga - 06-22-2007 10:02 PM

Hi,

UPDATED

Simple :-)

Just set a callback for the window ... When the escape key is pressed you can decide what to do ...

I'm about to try it ...

Cheers
JohnM


RE: Escape Key exits window - JohnMurga - 06-23-2007 12:29 AM

This'll do it :

Code:
function esc_callback(myValue)

   if (Fl:event() == 12) and (Fl:event_key() == fltk.FL_Escape) then
       print ("No quit on Escape")
       io.flush()
   else
       os.exit()
   end
end

w=fltk:Fl_Window(0,0, 200, 200, "esc demo")

w:callback(esc_callback)

... Rest of program ...




RE: Escape Key exits window - mikshaw - 06-23-2007 01:34 AM

What is the purpose of checking for event 12? (FL_DEACTIVATE?)


RE: Escape Key exits window - JohnMurga - 06-23-2007 01:57 AM

mikshaw Wrote:
What is the purpose of checking for event 12? (FL_DEACTIVATE?)


It's an FLTK inconsistency ... Basically if you just check for

Code:
Fl:event_key() == fltk.FL_Escape


Then it'll take a few clicks on the "close window" to exit the program, I was hoping that the event type for escape was "KEYBOARD", but it was something else (12), while for "close window" it was 10 (I think), so I just used 12.

I know, a little bit hacky, but I was in a hurry :-)

Maybe it's enough to just check for event 12, but this is a pretty robust check.

Cheers
JohnM


RE: Escape Key exits window - mikshaw - 06-23-2007 06:22 AM

This could be a very useful way to make a more friendly application.
point 1: Many people are accustomed to pressing Esc to cancel an action or close a transient window, but not so much to close a main application window. I know I was a little surprised by this behavior at the beginning.
point 2: Using an escape-ignoring function tied to the main window allows you to force another function at close, for tasks such as removing temp files, saving buffers, or simply leaving a nice goodbye message or exit confirmation.

I just tested the second point, and it seems that checking for event 12 has more use than I suspected. Apparently this allows you to call an exit function regardless of whether you create an exit button or send a close signal to the app:

Code:
#!/usr/bin/murgaLua
function exit() print("goodbye"); os.exit(0) end

function esc_callback(myValue)
   if (Fl:event() == 12) and (Fl:event_key() == fltk.FL_Escape) then
       print ("No quit on Escape")
       io.flush()
   else exit()
   end
end
w=fltk:Fl_Window(0,0, 200, 200, "there is no escape")
w:callback(esc_callback)
quit=fltk:Fl_Button(10,10,100,30,"quit"); quit:callback(exit)
w:show()
Fl:run()


Actually, now that I think about it, I don't see how event 12 relates to my last point.
But anyway, it works =o)