Reputation: 363
I have a double-click functionality for the tree viewers wherein the tree items expand\collapse when I double-click on them. That is completely fine but the issue is that when I press CTRL+M on the keyboard even then the tree items expand\collapse, I don't want this to happen. My code for double-clicking tree items is as follows:
private class DoubleClickListener implements IDoubleClickListener
{
@Override
public void doubleClick(final DoubleClickEvent event)
{
final IStructuredSelection selection = (IStructuredSelection)event.getSelection();
if (selection == null || selection.isEmpty())
return;
final Object sel = selection.getFirstElement();
final ITreeContentProvider provider = (ITreeContentProvider)treeViewer.getContentProvider();
if (!provider.hasChildren(sel))
return;
if (treeViewer.getExpandedState(sel))
treeViewer.collapseToLevel(sel, AbstractTreeViewer.ALL_LEVELS);
else
treeViewer.expandToLevel(sel, 1);
}
}
This behavior (CTRL+m expanding tree items) only happens When I use the IDoubleClickListener interface and override the method doubleClick(), but the same behavior (CTRL+m expanding tree items) does not happen when I use : addMouseListener(new MouseListener()) and override the method : mouseDoubleClick(). Is the behavior (CTRL+m) expanding tree items related to IDoubleClickListener interface(If so what is the reason) or is this problem generic? I feel it should not be related to IDoubleClickListener, Can someone please tell me why is there a difference in using these two logic?
Upvotes: 0
Views: 421
Reputation: 111142
Ctrl+M is often treated the same as the Return key. The native tree control used by SWT usually treats Return as meaning expand / collapse the current tree node.
To stop this add a KeyListener
to the tree and suppress the unwanted key events:
treeViewer.getTree().addKeyListener(new KeyAdapter()
{
@Override
public void keyPressed(final KeyEvent event)
{
if (event.keyCode == SWT.CR ||
(event.keyCode == 'm' && event.stateMask == SWT.CTRL))
{
event.doit = false;
}
}
});
Update:
Use:
if (e.keyCode == 'm' && e.stateMask == (SWT.CTRL | SWT.SHIFT))
to test for Ctrl+Shift+m
if (e.keyCode == 'm' && e.stateMask == SWT.CTRL)
to test for Ctrl+m
if (e.keyCode == SWT.CR)
to test for Enter.
Upvotes: 2