我无法知道点击了哪个按钮,因为以下所有方法都返回false。我试图抓取HBox
控件上的点击类型并使用下面的代码。如何区分左右键?
hb.addEventHandler(MouseEvent.MOUSE_CLICKED, event ->
{
System.out.println("Meta Down?" + event.isMetaDown());
System.out.println("Middle Down?" + event.isMiddleButtonDown());
System.out.println("Primary Down?" + event.isPrimaryButtonDown());
System.out.println("Secondary Down?" + event.isSecondaryButtonDown());
System.out.println("Synthesized?" + event.isSynthesized());
}
输出;
Meta Down?false
Middle Down?false
Primary Down?false
Secondary Down?false
Synthesized?false
答案 0 :(得分:4)
它们都没有关闭的原因是因为当按下鼠标按钮然后释放鼠标按钮时会调用MouseClicked,换句话说,它是一个鼠标按下事件,然后是鼠标释放事件。
如果你需要知道触发它的是什么,请查看event.getButton()
并查看它是否是MouseButton
中的任何一个。尝试将代码更改为以下内容:
hb.addEventHandler(MouseEvent.MOUSE_CLICKED, event ->
{
System.out.println("Middle Clicked?" + event.getButton()== MouseButton.MIDDLE);
System.out.println("Primary Clicked?" + event.getButton()== MouseButton.PRIMARY);
System.out.println("Secondary Clicked?" + event.getButton()== MouseButton.SECONDARY);
System.out.println("None Clicked?" + event.getButton()== MouseButton.NONE);
}