Using Alternative Modifiers for ViewMode Mouse Gestures

Tips & Tricks

Summary

How to use extended modifiers to trigger ViewMode mouse gestures.
For a better user experience, please go to the integrated documentation viewer to read this article.

Description

This article has been updated for version 2.7 (and higher) of yFiles for Java. For version 2.6 or 2.5 of yFiles for Java use the HierarchyMoveSelectionMode in the following instead of the MoveSelectionMode.

View modes that handle special mouse gestures, like, e.g., MoveSelectionMode, often also handle a related gesture in addition, e.g., adding selected nodes to a group node. Most commonly, a user can trigger the related gesture by holding down a modifier key (for example, the SHIFT key).

The mechanism to recognize if a specific modifier key is pressed is defined in class ViewMode: Protected method isModifierPressed(java.awt.event.MouseEvent) in conjunction with setModifierMask(int) handle modifier keys in view mode implementations. The latter method allows to easily change the modifier key by simply setting another modifier mask.
For example, by means of the following line the ALT key is used as the modifier. myViewMode.setModifierMask(InputEvent.ALT_MASK);

Using Extended Modifiers

However, setting another modifier mask does not support extended modifiers, which are recommended when looking at the Javadoc comments in class InputEvent. In order to use these, the isModifierPressed method needs to be appropriately customized. The following source code example shows how this can be done.

MoveSelectionMode myMSM = new MoveSelectionMode() {
  protected boolean isModifierPressed(MouseEvent me) {
    return ((me.getModifiersEx() & me.ALT_DOWN_MASK) != 0);
  }
};

The attached tutorial demo code AlternativeModifiersDemo27.java presents this and additionally shows how to register a key listener with the canvas component of the Graph2DView that displays the graph in order to use the key combination ALT-M as a modifier.

For version 2.6 or 2.5 of yFiles for Java use the AlternativeModifiersDemo.java.

Resources

Categories this article belongs to:
yFiles for Java > yFiles Viewer > Displaying and Editing Graphs > User Interaction
Applies to:
yFiles for Java 2: 2.5, 2.6, 2.7, 2.8, 2.9, 2.10, 2.11, 2.12, 2.13, 2.14, 2.15, 2.16, 2.17, 2.18
Keywords:
extended - modifiers - modifier - mask - view - mode - ViewMode - modes - hierarchy - move - selection