如何同时使用 MouseListener 和 KeyListener?
例如,如何做这样的事情
public void keyPressed( KeyEvent e){
// If the mouse button is clicked while any key is held down, print the key
}
最佳答案
使用 boolean 值来确定鼠标按钮是否被按下,然后在 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 变量。
关于java - MouseListener和KeyListener同时使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22057332/