简体   繁体   English

Java侦听器-键入时鼠标左键未注册

[英]Java listeners - Mouse left-press not registering while typing

I am writing an application engine but I've found a problem in the event registering system. 我正在编写应用程序引擎,但是在事件注册系统中发现了问题。 It seems like when typing on the keyboard and pressing the mouse left button, the window doesn't register the mouse press. 似乎在键盘上键入并按鼠标左键时,该窗口没有记录鼠标的按下。 But when I press the right mouse button while typing on the keyboard it actually registers. 但是,当我在键盘上打字时按下鼠标右键时,它实际上会注册。

To somewhat summarize the code: 总结一下代码:

import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;

public class BasicEventTest implements KeyListener, MouseListener, MouseMotionListener {

    JFrame frame;

    /*********************************************** INPUT HANDLING ***********/
    public void keyPressed(KeyEvent k) {
        System.out.println(k.getKeyCode());
    }
    public void keyReleased(KeyEvent k) {}
    public void keyTyped(KeyEvent k) {}

    public void mousePressed(MouseEvent m) {
        System.out.println("MOUSEPRESS " + m.getButton());
    }
    public void mouseReleased(MouseEvent m) {}
    public void mouseMoved(MouseEvent m) {}
    public void mouseDragged(MouseEvent m) {}
    public void mouseEntered(MouseEvent m) {}
    public void mouseExited(MouseEvent m) {}
    public void mouseClicked(MouseEvent m) {}

    /**************************************************** CONSTRUCTOR *********/
    public BasicEventTest() {
        frame = new JFrame("BasicEventTest");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setPreferredSize(new Dimension(640, 480));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.createBufferStrategy(2);
        frame.setIgnoreRepaint(true);

        frame.addKeyListener(this);
        frame.addMouseListener(this);
        frame.addMouseMotionListener(this);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        BasicEventTest test = new BasicEventTest();
    }
}

SOLVED: Apparently in Windows 10, there's a setting in PC Settings->PC and devices->Mouse and touchpad, which controls the delay for registering clicks or moving the mouse after typing. 已解决:显然在Windows 10中,“ PC设置”->“ PC和设备”->“鼠标和触摸板”中有一个设置,该设置可控制注册单击或键入后移动鼠标的延迟。

The answer is on the comments: 答案在评论上:

Since the code is working as expected, you should probably check configurations at SO level 由于代码按预期运行,因此您应该检查SO级别的配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM