Reading Special MouseButtons

风流意气都作罢 提交于 2019-12-11 08:02:07

问题


I am interested in capturing input from the explorer mouse buttons (the special buttons on the side of mice normally used to go forward and back on web browsers).

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.input.MouseButton;
import static java.lang.System.*;

public class MouseThing extends Application
{
    @Override
    public void start(Stage stage) throws Exception
    {
        Pane pane = new Pane();

        pane.setOnMousePressed(e -> {
            if(e.getButton() == MouseButton.PRIMARY)
                out.println("LEFT"); 
            if(e.getButton() == MouseButton.SECONDARY)
                out.println("RIGHT");      
            if(e.getButton() == MouseButton.MIDDLE)
                out.println("MIDDLE");    
            if(e.getButton() == MouseButton.NONE)
                out.println("OTHER");  
            // How read explorer buttons?  
            out.println("click");
        });

        Scene scene = new Scene(pane, 300, 100);
        stage.setTitle("Demo");
        stage.setScene(scene);
        stage.show();
    }
}

Just for the example, I would like to record that the advance and retreat buttons were activated. The only interaction I can achieve at this point is left, right and middle clicks. Pressing the forward and back buttons on the mouse doesn't even register the click print.

There was this post for Javascript: JS special mouse buttons, but it wasn't useful for my purposes.


回答1:


Since JavaFX 12, the forward and back buttons on the mouse are recognized, so you can do:

pane.setOnMousePressed(e -> {
    if(e.getButton() == MouseButton.PRIMARY)
        out.println("LEFT");
    if(e.getButton() == MouseButton.SECONDARY)
        out.println("RIGHT");
    if(e.getButton() == MouseButton.MIDDLE)
        out.println("MIDDLE");
    if(e.getButton() == MouseButton.BACK)
        out.println("BACK");
    if(e.getButton() == MouseButton.FORWARD)
        out.println("FORWARD");
    if(e.getButton() == MouseButton.NONE)
        out.println("NONE");  
    out.println("click");
});


来源:https://stackoverflow.com/questions/47701036/reading-special-mousebuttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!