Pharo offers a Keymapping framework (developed originally by Guillermo Polito for managing keybindings throughout the Morphic interface.
For example, defining an action bound to a keybinding is as elegant as:
Morph new bindKeyCombination: $, command toAction: [ self inform: 'Action!' ]; openInWindow; takeKeyboardFocus
Pressing Cmd+, after executing the above code will trigger the action from the block.
This is a simple sample of how to add anonymous actions directly to a morph. But, the framework handles significantly more than that:
The framework is now used everywhere in the Pharo environment. In my current image, doing:
KMKeymap allInstances size
... reveals that there are some 400 such registered keys. This means that when developing a Morphic interface, managing keybindings can be a problematic issue due to the interferences that can occur when defining the same keybinding for different actions. The GTInspector can help you in several ways.
There are at least two ways you want to look at how keybindings are associated to morphs:
Let’s look at all statically defined keybindings. They are held in the KMRepository default
. Inspecting it offers us an overview of all the bindings. The presentation presents them as a table with shortcut, description, action and category columns:
Given that there exist many such keybindings, to find your specific one you might want to search for it. There are two ways to filter the list. First, clicking on the category tag to the right shows only the keybindings from the respective category. Second, the input field at the bottom provides a custom search functionality. For example, below you can see the result of searching for Ctrl + D.
A recurrent issue is to relate an installed keybinding to its definition in source code. For example, when selecting the Ctrl + D keybinding from RubSmalltalkEditor
we get a KMKeymap
that offers a presentation with the definition of the source code as well.
The other way to relate to keybindings is from the point of view of a specific morph. For example, let’s look at a simple morph that defines a keybinding as in the first snippet from this post. Inspecting the morph reveals that the Cmd + , key is installed:
The ability of inspecting the keys associated to a morph gets particularly relevant when reasoning about hierarchies of morphs. For example, when inspecting a RubScrolledTextMorph
, we can find most of the relevant text editing keybindings associated with the RubEditingArea
submorph.
Keymapping is a nice framework, and now it is easily manageable from within the most basic development tool: the inspector.