Another programming note, this time about wxWidgets and application wide shortcuts. Before going further, I want to state that I’m using the MSW version of wxWidgets. So maybe Gtk or other implementations don’t suffer from the same problem at all. In any case, keep in mind that I’m talking about the MSW implementation of wxWidgets :)
In wx, you can have “window wide” shortcuts through accelerator tables or system wide shortcuts through RegisterHotKey. Problems with accelerator tables is this one: you have a main window with such an accelerator table. Then you add a tool frame, make it floatable. Then the shortcuts stop working if your tool frame is floating and has the focus. It works fine if the tool frame is docked though … And system wide shortcuts should not be used for obvious reasons.
On top of that, there is a big flaw in the way events get processed in wxWidgets. Let’s say you want to add a shortcut on the keyboard key ‘t’ (for translation, for instance) Hell begins. Why ? Because now you can’t enter the letter ‘t’ in any text control of your application: the accelerator table catches it before the text control and process it as a shortcut …
Some might say “don’t you ‘t’, use ‘alt+t’. Yeah right: 3DS Max uses ‘w’ for translation, and you can still write a ‘w’ in the script editor window, obviously… And I don’t want to make a list of all the software that use single letters with no modifier as shortcuts, and still allow using those letter in scripting window, that would be too long. Just to name a few: 3DS Max, Maya, Blender, Photoshop, Visual Studio… well, basically every professional software out there.
Anyway, there is no way to have an application wide shortcut that doesn’t screw text controls, and work also when using floating panels, so enough writing, let’s add such a system !
The first thing is to add a shortcut system to your application:
class Application : public wxApp { public: //! application init virtual bool OnInit(); //! application uninit virtual int OnExit(); //! register an application wide shortcut template< typename T > bool RegisterShortcut(int modifier, int key, int id, const T& fctor); private: //! this is where we will filter events before wx can process them void OnCharHook(wxKeyEvent& evt); //! callback type for application wide events typedef boost::function CallbackType; //! a shortcut struct Shortcut { //! ctor Shortcut(int m, int k, int id, const CallbackType& c); //! modifier int Modifier; //! key code int KeyCode; //! the wx id int ID; //! callback CallbackType Callback; //! equality operator bool operator == (const SShortcut& o); //! equality operator with an accelerator entry bool operator == (const wxAcceleratorEntry& value); }; //! the registered application wide shortcuts std::vector m_Shortcuts; }; template bool Application::RegisterShortcut(int modifier, int key, int id, const T& fctor) { // check if the shortcut is not already registered Shortcut shortcut(modifier, key, id, fctor); if (std::find(m_Shortcuts.begin(), m_Shortcuts.end(), shortcut) != m_Shortcuts.end()) { return false; } m_Shortcuts.push_back(shortcut); return true; } inline Application::Shortcut::SShortcut(int m, int k, int id, const SCallback& c) : Modifier(m) , KeyCode(k) , ID(id) , Callback(c) { } inline bool Application::Shortcut::operator == (const Shortcut& o) { return o.Modifier == Modifier && o.KeyCode == KeyCode; } inline bool Application::Shortcut::operator == (const wxAcceleratorEntry& value) { return KeyCode == tolower(value.GetKeyCode()) && Modifier == value.GetFlags(); }
The idea is pretty simple: I created a SShortcut structure which hold a key code, a modifier, a wx id and a callback object. Thanks to boost::function and boost::bind and the templated function to register a shortcut, you can set this to almost anything: a free function, a method of an object, even a lambda if your compiler supports it (well, at least in Visual Studio 2010, it works)
Now the interesting part: how we handle those to avoid conflicts with text controls, and how do we manage to catch those shortcuts even if the focused window is a floating frame ? Like this:
bool Application::OnInit() { // bind on the char hook event. This event is sent before any // other, so we can catch it to intercept our shortcuts Bind(wxEVT_CHAR_HOOK, &Application::onCharHook, this); } bool Application::OnExit() { Unbind(wxEVT_CHAR_HOOK, &Application::onCharHook, this); } void Application::OnCharHook(wxKeyEvent& evt) { // dummy shortcut used for search Shortcut s(evt.GetModifiers(), evt.GetKeyCode(), -1, CallbackType()); std::vector::iterator shortcut = std::find(m_Shortcuts.begin(), m_Shortcuts.end(), s); // this is where we do the job: if we found a shortcut, we need // to make sure of a few things. First, that the application has // the focus. Then, that the focused control is not a text editing // one. wxWindow * focused = wxWindow::FindFocus(); if(shortcut != m_Shortcuts.end() && focused && !dynamic_cast< wxTextCtrl * >(focused) && !dynamic_cast< wxStyledTextCtrl * >(focused)) { // ok, we're clear, we can execute the callback of the shortcut ! wxCommandEvent e; e.SetId(shortcut->ID); shortcut->callback(e); } else { // let go of the event. If a text control has the focus, let // it handle it, etc. evt.Skip(); } }
So now, imagine that you have an application with a floating panel, in this floating panel you can just do that:
Application * app = static_cast< Application * >(wxApp::GetInstance()); app->RegisterShortcut(wxACCEL_NORMAL, (int)'T', AnID, boost::bind(&PanelClass::OnTranslate, this, _1));
And here you have it. ‘t’ is now a shortcut (in my case for ‘translation’) This shortcut will work from anywhere in the application, even from a floating frame, and more importantly, will still let you type the letter ‘t’ in text controls.
Note: AnID is the event id which will be sent to the OnTranslate method of the PanelClass class.
Going Further
I tried to keep this article short, so I simplified a lot of things. One of those is how I do when I try to register a shortcut that is already registered by some other part of my application. Their are a lot of ways to take care of that: store the wxWindow pointer which registered the shortcut, then in the OnCharHook you can check if the focused window is one of those, you can also add a simple priority order to the registration. With those 2 you’ll be able to call the correct shortcut whatever the situation.
There is also 1 pitfall with this approach: when you don’t want to make a shortcut application wide, the simplest way is to use accelerators. But with this method, if a focused panel has an accelerator on the letter ‘t’, it’s still the application wide shortcut that will be called. To overcome this, you need to get the focused window, go up its hierarchy, and for each window, get the accelerator table and test it against you current event. This part is a bit hacky on Windows, unfortunately. The “cleanest” way I found was to simply store in the wxAcceleratorTable the array of entries used to create the table (I had to modify the sources of wx) Maybe I’ll write another post about that.