1. Capture ESC Key for Dialog in wxEuphoria
- Posted by euphoric (admin) Jun 18, 2012
- 1673 views
How do I capture the ESC key when displaying a wxDialog? I've tried the following, which doesn't work:
procedure dlg_AboutKeyDown(atom this, atom event_type, atom id, atom event) if get_key_code( event ) = 27 then end_modal( dlg_About, 1 ) else skip( event ) end if end procedure set_event_handler( dlg_About, -1, wxEVT_CHAR, routine_id("dlg_AboutKeyDown"))
I also tried the wxEVT_KEYDOWN event as well as the wxEVT_CHAR event shown above.
The above code works when I attach it to a edit text control for another dialog box, but there are no such controls on this dialog (except a button). So how do I make it apply to the entire dialog, not just a control?
2. Re: Capture ESC Key for Dialog in wxEuphoria
- Posted by ghaberek (admin) Jun 18, 2012
- 1697 views
The library will handle this for you automatically as long as you have a button with wxID_CANCEL.
You can either create the buttons manually, or use a button sizer and create them automatically.
-- dialog_test1.exw include "wxeu/wxeud.e" constant frmMain = create( wxFrame, {0, -1, "Dialog Test", -1, -1, 320, 240} ) constant pnlMain = create( wxPanel, {frmMain} ) constant btnOpen = create( wxButton, {pnlMain, -1, "&Show dialog", 10, 10, 75, 23} ) constant txtOutput = create( wxTextCtrl, {pnlMain, -1, "", 10, 40, 284, 154, wxTE_MULTILINE+wxTE_READONLY} ) constant dlgDialog = create( wxDialog, {0, -1, "Dialog", -1, -1, 480, 360} ) constant btnOK = create( wxButton, {dlgDialog, wxID_OK, "OK", 10, 10, 75, 23} ) constant btnCancel = create( wxButton, {dlgDialog, wxID_CANCEL, "Cancel", 95, 10, 75, 23} ) procedure btnOpen_onClick( atom this, atom event_type, atom id, atom event ) sequence text if show_modal(dlgDialog) = wxID_OK then text = "result = OK\r\n" else text = "result = Cancel\r\n" end if append_text( txtOutput, text ) end procedure set_event_handler( btnOpen, -1, wxEVT_COMMAND_BUTTON_CLICKED, routine_id("btnOpen_onClick") ) wxMain( frmMain )
-- dialog_test2.exw include "wxeu/wxeud.e" constant frmMain = create( wxFrame, {0, -1, "Dialog Test", -1, -1, 320, 240} ) constant pnlMain = create( wxPanel, {frmMain} ) constant btnOpen = create( wxButton, {pnlMain, -1, "&Show dialog", 10, 10, 75, 23} ) constant txtOutput = create( wxTextCtrl, {pnlMain, -1, "", 10, 40, 284, 154, wxTE_MULTILINE+wxTE_READONLY} ) constant dlgDialog = create( wxDialog, {0, -1, "Dialog", -1, -1, 480, 360} ) constant pnlDialog = create( wxPanel, {dlgDialog} ) constant dlgSizer = create( wxBoxSizer, {wxVERTICAL} ) constant btnSizer = create_button_sizer( dlgDialog, wxOK+wxCANCEL ) add_window_to_sizer( dlgSizer, pnlDialog, 1, wxGROW, 0 ) add_sizer_to_sizer( dlgSizer, btnSizer, 0, wxGROW+wxBOTTOM, 4 ) set_sizer( dlgDialog, dlgSizer ) procedure btnOpen_onClick( atom this, atom event_type, atom id, atom event ) sequence text if show_modal(dlgDialog) = wxID_OK then text = "result = OK\r\n" else text = "result = Cancel\r\n" end if append_text( txtOutput, text ) end procedure set_event_handler( btnOpen, -1, wxEVT_COMMAND_BUTTON_CLICKED, routine_id("btnOpen_onClick") ) wxMain( frmMain )
Also, check out wxAboutDialogInfo for an easy way to make an "about" dialog (which it seems like you're trying to do).
-Greg
3. Re: Capture ESC Key for Dialog in wxEuphoria
- Posted by euphoric (admin) Jun 18, 2012
- 1689 views
The library will handle this for you automatically as long as you have a button with wxID_CANCEL.
Ah! OK. Good to know.
However, what if I wanted to dismiss the main window (your frmMain in the examples), or any window/dialog, with the ESC key, but not show a Cancel button? I'm sure that's possible, and no doubt more difficult...
Also, check out wxAboutDialogInfo for an easy way to make an "about" dialog (which it seems like you're trying to do).
It's more like an "About this System" dialog, where I need to show information related to the user's applications, his OS, and the program I've provided to him (version numbers, etc.). I'm thinking of using a properties grid control, because that's what I imagine it looking like. HOWEVER, I will check out the wxAboutDialog to see if it fits.
Thanks, Greg!
4. Re: Capture ESC Key for Dialog in wxEuphoria
- Posted by ghaberek (admin) Jun 18, 2012
- 1670 views
This seems to work fine for me...
-- key_test.exw include "wxeu/wxeud.e" constant frmMain = create( wxFrame, {0, -1, "Key Test", -1, -1, 480, 360} ) constant pnlMain = create( wxPanel, {frmMain} ) constant btnShow = create( wxButton, {pnlMain, -1, "&Show dialog", 10, 10, 75, 23} ) constant dlgDialog = create( wxDialog, {0, -1, "Dialog", -1, -1, 640, 480} ) procedure btnShow_onClick( atom this, atom event_type, atom id, atom event ) show_modal( dlgDialog ) end procedure set_event_handler( btnShow, -1, wxEVT_COMMAND_BUTTON_CLICKED, routine_id("btnShow_onClick") ) procedure dlgDialog_onKeyDown( atom this, atom event_type, atom id, atom event ) atom keyCode = get_key_code( event ) if keyCode = WXK_ESCAPE then end_modal( this, wxID_CANCEL ) end if skip( event ) end procedure set_event_handler( dlgDialog, -1, wxEVT_KEY_DOWN, routine_id("dlgDialog_onKeyDown") ) wxMain( frmMain )
-Greg
5. Re: Capture ESC Key for Dialog in wxEuphoria
- Posted by euphoric (admin) Jun 19, 2012
- 1628 views
Greg, the addition of a button on your dialog screen prevents ESC from closing the dialog.
Just add
constant testbttn = create( wxButton, {dlgDialog,-1,"OK",-1,-1,-1,-1} )
after
constant dlgDialog = create( wxDialog, {0, -1, "Dialog", -1, -1, 640, 480} )
and the dialog can't be closed with ESC key.
Now what do we do?
6. Re: Capture ESC Key for Dialog in wxEuphoria
- Posted by ghaberek (admin) Jun 19, 2012
- 1568 views
Greg, the addition of a button on your dialog screen prevents ESC from closing the dialog.
Just add
constant testbttn = create( wxButton, {dlgDialog,-1,"OK",-1,-1,-1,-1} )
after
constant dlgDialog = create( wxDialog, {0, -1, "Dialog", -1, -1, 640, 480} )
and the dialog can't be closed with ESC key.
Now what do we do?
It still works fine for me, unless I'm doing something differently. Can you post a full source example of this?
If I can replicate the problem, I can try to work backwards and figure out what's causing the issue.
-Greg
7. Re: Capture ESC Key for Dialog in wxEuphoria
- Posted by euphoric (admin) Jun 20, 2012
- 1573 views
Using Windows 7, when I run the below code and get the dialog, when I press "ESC," it gives me a system beep sound and the dialog remains.
You must be testing with something other than Windows 7.
-- key_test.exw include "wxeu/wxeud.e" constant frmMain = create( wxFrame, {0, -1, "Key Test", -1, -1, 480, 360} ) constant pnlMain = create( wxPanel, {frmMain} ) constant btnShow = create( wxButton, {pnlMain, -1, "&Show dialog", 10, 10, 75, 23} ) constant dlgDialog = create( wxDialog, {0, -1, "Dialog", -1, -1, 640, 480} ) constant testbttn = create( wxButton, {dlgDialog,-1,"OK",-1,-1,-1,-1} ) procedure btnShow_onClick( atom this, atom event_type, atom id, atom event ) show_modal( dlgDialog ) end procedure set_event_handler( btnShow, -1, wxEVT_COMMAND_BUTTON_CLICKED, routine_id("btnShow_onClick") ) procedure dlgDialog_onKeyDown( atom this, atom event_type, atom id, atom event ) atom keyCode = get_key_code( event ) if keyCode = WXK_ESCAPE then end_modal( this, wxID_CANCEL ) end if skip( event ) end procedure set_event_handler( dlgDialog, -1, wxEVT_KEY_DOWN, routine_id("dlgDialog_onKeyDown") ) wxMain( frmMain )
8. Re: Capture ESC Key for Dialog in wxEuphoria
- Posted by EUWX Jun 20, 2012
- 1541 views
Using Windows 7, when I run the below code and get the dialog, when I press "ESC," it gives me a system beep sound and the dialog remains.
You must be testing with something other than Windows 7.
-- key_test.exw include "wxeu/wxeud.e" constant frmMain = create( wxFrame, {0, -1, "Key Test", -1, -1, 480, 360} ) constant pnlMain = create( wxPanel, {frmMain} ) constant btnShow = create( wxButton, {pnlMain, -1, "&Show dialog", 10, 10, 75, 23} ) constant dlgDialog = create( wxDialog, {0, -1, "Dialog", -1, -1, 640, 480} ) constant testbttn = create( wxButton, {dlgDialog,-1,"OK",-1,-1,-1,-1} ) procedure btnShow_onClick( atom this, atom event_type, atom id, atom event ) show_modal( dlgDialog ) end procedure set_event_handler( btnShow, -1, wxEVT_COMMAND_BUTTON_CLICKED, routine_id("btnShow_onClick") ) procedure dlgDialog_onKeyDown( atom this, atom event_type, atom id, atom event ) atom keyCode = get_key_code( event ) if keyCode = WXK_ESCAPE then end_modal( this, wxID_CANCEL ) end if skip( event ) end procedure set_event_handler( dlgDialog, -1, wxEVT_KEY_DOWN, routine_id("dlgDialog_onKeyDown") ) wxMain( frmMain )
It seems that esc key is set to beep. I have had problems with
skip( event )
Perhaps the gurus can explain its use whilst they are solving your problem.
9. Re: Capture ESC Key for Dialog in wxEuphoria
- Posted by euphoric (admin) Jun 20, 2012
- 1513 views
It seems that esc key is set to beep.
The ESC key works if there is no button on the dialog, so the problem is that the button has focus and (I guess) captures the ESC key before the keystroke gets passed onto the dialog. I thought there was some way to assign keystrokes to the main frame, I just don't remember how and can't find where it is in the docs.
10. Re: Capture ESC Key for Dialog in wxEuphoria
- Posted by mattlewis (admin) Jun 20, 2012
- 1530 views
Using Windows 7, when I run the below code and get the dialog, when I press "ESC," it gives me a system beep sound and the dialog remains.
You must be testing with something other than Windows 7.
constant testbttn = create( wxButton, {dlgDialog,-1,"OK",-1,-1,-1,-1} )
Here on Linux it seems to work correctly. However, I notice that you're allowing wxWidgets to create IDs for your buttons. In fact, there are special IDs predefined for dialog buttons: wxID_OK and wxID_CANCEL.
You might get better results if you use those. For instance, with the OK button, clicking on it causes it to close the modal dialog. Adding a wxID_CANCEL button might keep the automatic cancel behavior that (I think) you're looking for.
Matt
11. Re: Capture ESC Key for Dialog in wxEuphoria
- Posted by euphoric (admin) Jun 20, 2012
- 1519 views
Adding a wxID_CANCEL button might keep the automatic cancel behavior that (I think) you're looking for.
I just don't want to confuse the user with an "OK" button AND a "Cancel" button on an "about" dialog window.
I tried setting the button to wxID_CANCEL with a label of "OK," and that worked for this example.
constant testbttn = create( wxButton, {dlgDialog,wxID_CANCEL,"OK",-1,-1,-1,-1} )
It's not yet working with my XRC setup...