Time for action – binding commands to keys

Hooking up the command to a keystroke requires a KeyBinding. This allows a key (or series of keys) to be used to invoke the command instead of only via the menu. KeyBindings are set up inside a Binding Table and associated with a Binding Context.

  1. Open the fragment.e4xmi in the clock.ui project.
  2. In the imports, select the Binding Context from the dropdown and click on Add. In the Reference-ID, click on Find… and a dialog will be shown with the contexts. Choose the org.eclipse.ui.contexts.dialogAndWindow context:
    Time for action – binding commands to keys
  3. Once the binding context has been imported, it can be used within a binding table. Click on Model Fragments and select Add to create a new fragment. Fill in the details as follows:
    1. Extended Element ID: org.eclipse.e4.legacy.ide.application
    2. Feature Name: bindingTables
  4. In the BindingTable element, choose the org.eclipse.ui.contexts.dialogAndWindow context. This will ensure that the key binding is available in all windows and dialogs:
    Time for action – binding commands to keys
  5. Click on Add and a KeyBinding window will be shown. In the Sequence, enter M1+9. Next to the Command, click on Find... and choose the Hello command:
    Time for action – binding commands to keys
  6. Run the target Eclipse instance, and press Cmd + 9 (for macOS) or Ctrl + 9 (for Windows/Linux). The same Hello dialog should be displayed, as if it were shown from the menu. The same keystroke should be displayed in the Help menu.

What just happened?

The M1 key is the primary meta key, which is Cmd on macOS and Ctrl on Windows/Linux. This is typically used for the main operations; for example, M1+C is copy and M1+V is paste on all systems. The sequence notation M1+9 is used to indicate pressing both keys at the same time.

The command that gets invoked is referenced by its commandId. This may be defined in the same plug-in, but it does not have to be; it is possible for one application to provide a set of commands and another plug-in to provide keystrokes that bind them.

It is also possible to set up a sequence of key presses; for example, M1+9 8 7 would require pressing Cmd (or Ctrl) with 9 followed by 8 and then 7 before the command is executed. This allows a set of keystrokes to be used to invoke a command; for example, it's possible to emulate an Emacs quit operation with the KeyBinding Ctrl + X Ctrl + C to the quit command.

Other modifier keys include M2 (Shift), M3 (Alt or Option), and M4 (Ctrl on macOS). It is possible to use CTRL, SHIFT or ALT as long names but the meta names are preferred, since M1 tends to be bound to different keys on different operating systems.

The non-modifier keys themselves can either be single characters (A-Z), numbers (0-9), or one of a set of longer name key-codes such as F12, ARROW_UP, TAB, and PAGE_UP. Certain common variations are allowed, for example, ESC/ESCAPE and ENTER/RETURN.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.22.50