The method isKeyboardTrigger of the class ContextMenuEvent returns false also for keyboard triggered context menus.
import javafx.embed.swt.FXCanvas;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class TwoButtons {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout(SWT.VERTICAL));
final FXCanvas fxCanvas = new FXCanvas(shell, SWT.NONE);
Button button = new Button("Right click or press menu key!");
Scene scene = new Scene(button);
fxCanvas.setScene(scene);
button.setOnContextMenuRequested(event ->
button.setText("trigger is: " + (event.isKeyboardTrigger() ? "keyboard" : "mouse")));
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
import javafx.embed.swt.FXCanvas;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class TwoButtons {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout(SWT.VERTICAL));
final FXCanvas fxCanvas = new FXCanvas(shell, SWT.NONE);
Button button = new Button("Right click or press menu key!");
Scene scene = new Scene(button);
fxCanvas.setScene(scene);
button.setOnContextMenuRequested(event ->
button.setText("trigger is: " + (event.isKeyboardTrigger() ? "keyboard" : "mouse")));
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}