wxWidgets: Keyboard Shortcuts without a Menu
Quick entry. Sort of a preview. I’m working on a tutorial for dream.in.code about creating keyboard shortcuts in a wxWidgets application (particularly using C++), and figured I’d share the code here, too.
(After I’ve finally gotten around to installing this syntax highlighting plugin for WordPress)
Aaaaannddd… Done!
Right. Back onto the original topic. Damn sidetracked mind. So, when I write a tutorial, I write a whole chunk of code first, and then add the tutorial content around it. Here’s the code for allocating keyboard shortcuts to do different stuff in wxWidgets without using menu items.
// basic setup code, I'll fly past this. If you're not familiar, check out this tutorial: // http://www.dreamincode.net/forums/showtopic66948.htm #include <wx/wx.h> class app : public wxApp { public: virtual bool OnInit(); private: wxTextCtrl* txt; wxCheckBox* chk; void key_shortcut(wxKeyEvent&); DECLARE_EVENT_TABLE() }; // if you're not familiar with events, check out this tutorial: // http://www.dreamincode.net/forums/showtopic67058.htm BEGIN_EVENT_TABLE(app, wxApp) EVT_KEY_DOWN(app::key_shortcut) END_EVENT_TABLE() bool app::OnInit() { wxFrame* win = new wxFrame(NULL, wxID_ANY, wxT("Keyboard Shortcuts, No Menu!"), wxDefaultPosition, wxSize(250, 125)); win->Show(true); this->txt = new wxTextCtrl(win, wxID_ANY, wxT("Press Ctrl+G to append text to me"), wxPoint(0,0), wxSize(250, 50), wxTE_MULTILINE); this->chk = new wxCheckBox(win, wxID_ANY, wxT("Press Ctrl+Left to toggle me."), wxPoint(0, 55), wxDefaultSize); return true; } // this is the method I'll be focusing on. The key_shortcut event which we create // to do fancy stuff without adding billions of menu items void app::key_shortcut(wxKeyEvent& e) { // of course, it doesn't have to be the control key. You can use others: // http://docs.wxwidgets.org/stable/wx_wxkeyevent.html if(e.GetModifiers() == wxMOD_CONTROL) { switch(e.GetKeyCode()) { case 'G': // can return the upper ASCII value of a key // do whatever you like for a Ctrl+G event here! this->txt->AppendText(wxT(" gabehabe ftw!")); break; case WXK_LEFT: // we also have special keycodes for non-ascii values. // get a full list of special keycodes here: // http://docs.wxwidgets.org/stable/wx_keycodes.html this->chk->SetValue(!this->chk->GetValue()); break; default: // do nothing break; } } } IMPLEMENT_APP(app)
Link to follow when the tutorial is completed and approved.
As promised, here’s the tutorial in all it’s glory:
wxWidgets keyboard shortcuts with no menu
Categories
Blogroll
- My other blog
- Beki Sutcliffe
- Charlie Baker
- Denis Delimarschi
- Derek Ackley
- Ian Mellett
- Joe Otero
- Josh Erickson
- Ryan Alford
- Cupcake Hax0r (I write here!)
September 11th, 2009 - 23:01
Did you consider using a wxAcceleratorTable? Since accelerator entries are processed before the key events, it can be very helpful for defining a set of accelerators on text controls, listviews, etc.. I also find it easier to set the same shortcut key(s) on multiple controls and have it be context-sensitive to the control that currently has the focus.
September 11th, 2009 - 23:07
To be honest, I’d never even heard of them! ;)
However, I’ve just looked up the wxAcceleratorTable, and I can see a problem in the use of it. I often write cross-platform code, so I don’t like my applications to use anything that might act differently on other operating systems. Looking at the documentation it seems that it only works with menu items under GTK? I may experiment with them over the weekend. Thanks for the tip! :)
[And yes, I know using wxMOD_CONTROL will vary across operating systems because Mac keyboards have the command key instead (wxMOD_CMD) ... but it's Mac, so who cares?] ;)