Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MouseListener和KeyListener同时使用_Java_Swing_Awt_Keylistener_Mouselistener - Fatal编程技术网

Java MouseListener和KeyListener同时使用

Java MouseListener和KeyListener同时使用,java,swing,awt,keylistener,mouselistener,Java,Swing,Awt,Keylistener,Mouselistener,如何同时使用MouseListener和KeyListener 例如,如何做这样的事情 public void keyPressed( KeyEvent e){ // If the mouse button is clicked while any key is held down, print the key } 尝试创建一个布尔值isKeyPressed。在按下的键中,将其设置为true,在松开的键中,将其设置为false。然后,单击鼠标时,首先检查isKeyPressed是否为true。

如何同时使用MouseListener和KeyListener

例如,如何做这样的事情

public void keyPressed( KeyEvent e){
// If the mouse button is clicked while any key is held down, print the key
}

尝试创建一个布尔值
isKeyPressed
。在按下的
键中,将其设置为true,在松开的
键中,将其设置为false。然后,单击鼠标时,首先检查isKeyPressed是否为true。

尝试创建一个布尔值
isKeyPressed
。在按下的
键中,将其设置为true,在松开的
键中,将其设置为false。然后,单击鼠标时,首先检查isKeyPressed是否为true。

使用布尔值确定是否按下鼠标按钮,然后在鼠标侦听器中更新该变量

boolean buttonDown = false;

public class ExampleListener implements MouseListener {
    public void mousePressed(MouseEvent e) {
        buttonDown = true;
    }

    public void mouseReleased(MouseEvent e) {
        buttonDown = false;
    }

    //Other implemented methods unimportant to this post... 
}

然后,在KeyListener类中,只需测试buttonDown变量。

使用布尔值确定是否按下鼠标按钮,然后在MouseListener中更新该变量

boolean buttonDown = false;

public class ExampleListener implements MouseListener {
    public void mousePressed(MouseEvent e) {
        buttonDown = true;
    }

    public void mouseReleased(MouseEvent e) {
        buttonDown = false;
    }

    //Other implemented methods unimportant to this post... 
}

然后,在KeyListener类中,只需测试buttonDown变量。

您可以尝试检查
KeyEvent
修饰符的状态,例如

addKeyListener(new KeyAdapter() {
    @Override
    public void keyPressed(KeyEvent e) {
        int mods = e.getModifiers();
        System.out.println(mods);
        if ((mods & KeyEvent.BUTTON1_MASK) != 0) {
            System.out.println("Button1 down");
        }
    }
});
我还应该指出,你可以

int ext = e.getModifiersEx();
if ((ext & KeyEvent.BUTTON1_DOWN_MASK) != 0) {
    System.out.println("Button1_down_mask");
}

正如我刚刚发现的,它会为其他鼠标按钮生成结果…

例如,您可以尝试检查
KeyEvent
修改器的状态

addKeyListener(new KeyAdapter() {
    @Override
    public void keyPressed(KeyEvent e) {
        int mods = e.getModifiers();
        System.out.println(mods);
        if ((mods & KeyEvent.BUTTON1_MASK) != 0) {
            System.out.println("Button1 down");
        }
    }
});
我还应该指出,你可以

int ext = e.getModifiersEx();
if ((ext & KeyEvent.BUTTON1_DOWN_MASK) != 0) {
    System.out.println("Button1_down_mask");
}

正如我刚刚发现的,它会为其他鼠标按钮产生结果…

“我如何同时使用MouseListener和KeyListener?”你为什么要处理这两个问题?Swing通常使用键绑定而不是较低级别的
KeyListener
,而
ActionListener
有时是
MouseListener
的更好选择。所有这些都是为了支持什么?“我如何同时使用MouseListener和KeyListener?”你为什么要处理这两个问题?Swing通常使用键绑定而不是较低级别的
KeyListener
,而
ActionListener
有时是
MouseListener
的更好选择。所有这些都是为了支持什么?:O如果您有时间,您能解释一下您的代码吗?为什么应该是按位的!=0.. 顺便说一句,应该是
BUTTON1\u DOWN\u MASK
?@nachokk不,我试过
BUTTON1\u DOWN\u MASK
,但Java坚持使用过时的
BUTTON1\u DOWN
mods&KeyEvent.BUTTON1\u MASK!=0
它意味着什么?@nachokk我的位数学是…垃圾…但是…基本上,据我所知,它试图从修饰符中提取
BUTTON1\u MASK
的值,这样它要么返回16,要么返回0(当
BUTTON1\u MASK
被插入或被插入时返回16)…我不知道为什么,我只是复制粘贴;):O如果您有时间,您能解释一下您的代码吗?为什么应该是按位的!=0.. 顺便说一句,应该是
BUTTON1\u DOWN\u MASK
?@nachokk不,我试过
BUTTON1\u DOWN\u MASK
,但Java坚持使用过时的
BUTTON1\u DOWN
mods&KeyEvent.BUTTON1\u MASK!=0
它意味着什么?@nachokk我的位数学是…垃圾…但是…基本上,据我理解,它试图从修饰符中提取
BUTTON1\u MASK
的值,这样它要么返回16,要么返回0(当
BUTTON1\u MASK
被插入或被插入时返回16)…我不知道为什么,我只是复制粘贴;)