News : The level of daily SPAM has reached insane proportions, all registrations are now manual. I ask you to send me an e-mail (john (at) murga (dot) org), to confirm that you want me to create an account for you.


Post Reply  Post Thread 
Escape Key exits window
Author Message
iGame3D
Moderator
***


Posts: 231
Group: Moderators
Joined: Apr 2007
Status: Offline
Reputation: 0
Post: #1
Escape Key exits window

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.

06-21-2007 06:39 AM
Visit this user's website Find all posts by this user Quote this message in a reply
mikshaw
Senior Member
****


Posts: 522
Group: Registered
Joined: Apr 2007
Status: Offline
Reputation: 5
Post: #2
RE: Escape Key exits window

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.

06-22-2007 07:38 AM
Find all posts by this user Quote this message in a reply
JohnMurga
Administrator
*******


Posts: 381
Group: Administrators
Joined: Apr 2007
Status: Offline
Reputation: 2
Post: #3
RE: Escape Key exits window

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

This post was last modified: 06-22-2007 11:43 PM by JohnMurga.

06-22-2007 10:02 PM
Visit this user's website Find all posts by this user Quote this message in a reply
JohnMurga
Administrator
*******


Posts: 381
Group: Administrators
Joined: Apr 2007
Status: Offline
Reputation: 2
Post: #4
RE: Escape Key exits window

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 ...

06-23-2007 12:29 AM
Visit this user's website Find all posts by this user Quote this message in a reply
mikshaw
Senior Member
****


Posts: 522
Group: Registered
Joined: Apr 2007
Status: Offline
Reputation: 5
Post: #5
RE: Escape Key exits window

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

06-23-2007 01:34 AM
Find all posts by this user Quote this message in a reply
JohnMurga
Administrator
*******


Posts: 381
Group: Administrators
Joined: Apr 2007
Status: Offline
Reputation: 2
Post: #6
RE: Escape Key exits window

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

06-23-2007 01:57 AM
Visit this user's website Find all posts by this user Quote this message in a reply
mikshaw
Senior Member
****


Posts: 522
Group: Registered
Joined: Apr 2007
Status: Offline
Reputation: 5
Post: #7
RE: Escape Key exits window

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)

This post was last modified: 06-23-2007 06:39 AM by mikshaw.

06-23-2007 06:22 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: