[fxruby-users] Handling Escape key-press in dialog window

Lyle Johnson
Fri, 11 Jan 2008 17:20:02 -0000

On Jan 11, 2008, at 11:04 AM, Vladimir Konrad wrote:

> I am trying to do a log-in screen for an application and have a
> problem
> with handling ESC key - when you press it, the dialog box closes and
> the application opens. What I would like is to quit the application if
> user hits ESC key while the initial log-in window is open.
>
> The LoginWindow class looks like:
>
> class LoginWindow < FXDialogBox
>
>   def initialize(owner)
>
>     super(owner, "gbrs - log in", :opts => DECOR_TITLE |
> DECOR_BORDER, :width => 300, :height => 250)
>
>     <paint the log-in window here>
>
>     # the following code should do it but it does not
>
>     self.accelTable.removeAccel(fxparseAccel("ESC"))
>     self.accelTable.addAccel(fxparseAccel("ESC"), getApp(), \
>       FXSEL(SEL_COMMAND, FXApp::ID_QUIT))
>   end
>
> end

The problem is that the FXDialogBox doesn''t store that mapping for
the "Esc" key in its accelerator table, as you''re assuming
here. The
FXDialogBox is just listening for SEL_KEYPRESS and SEL_KEYRELEASE
messages and checking to see if the key being pressed (and released)
is the Escape key (KEY_Escape).

See what happens if you do something like this:

	self.connect(SEL_KEYPRESS) do |sender, sel, event|
		app.exit(0) if event.code == KEY_Escape
		false
	end

In other words, we''re overriding the default handler for SEL_KEYPRESS
to check for the KEY_Escape keypress, and exiting the application if
we see it. If we don''t, the handler returns ''false''
to indicate that
we didn''t handle whatever key was pressed.

Hope this helps,

Lyle