Java linux 如何移动鼠标光标
Java linux how to move mouse cursor
我正在制作一个 3d 多平台应用程序,我想将鼠标光标始终设置为中间。 robot.mouseMove(x,y)
适用于 Windows,但适用于 Linux 它确实不起作用。
我有一个问题,是否有一种方法可以让我将鼠标光标设置在 Windows、Linux、Mac.
的特定位置
*编辑
这就是我得到的并且适用于 Windows.
public class Frame extends JFrame implements KeyListener, MouseMotionListener, MouseListener{
private static final long serialVersionUID = 4887525192006201710L;
private Frame self = this;
private JPanel buffer = new JPanel(){
public void paintComponent(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0,0, getWidth(), getHeight());
}
};
private Robot robot;
public Frame(String title){
super(title);
add(buffer);
setBounds(0,0, 640,480);
setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);
setUndecorated(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
try {
robot = new Robot();
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
addKeyListener(this);
addMouseMotionListener(this);
this.addComponentListener(new ComponentListener() {
public void componentResized(ComponentEvent e){
buffer.setBounds(0,0, getWidth(), getHeight());
}
public void componentHidden(ComponentEvent e){}
public void componentMoved(ComponentEvent e){}
public void componentShown(ComponentEvent e){}
});
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getKeyCode() == KeyEvent.VK_ENTER){
self.dispatchEvent(new WindowEvent(self, WindowEvent.WINDOW_CLOSING));
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("Mouse moved");
robot.mouseMove(getX()+getWidth()/2, getY()+getHeight()/2);
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
*解决方案
嗯,机器人工作正常。问题是没有问题(抱歉)。我以为机器人不工作是因为 VirtualBox 没有将我的鼠标移动到 Virtual Linux 屏幕的中间,但后来我发现它正在工作但是感谢 Enabled Mouse Integration我在 OS 里面没有看到原来的游标。
对于那些使用 运行 VirtualBox 并想要 禁用鼠标集成 快捷键 hostkey(right ctrl) + I
并且您的光标变为本机虚拟 OS 光标.
我正在制作一个 3d 多平台应用程序,我想将鼠标光标始终设置为中间。 robot.mouseMove(x,y)
适用于 Windows,但适用于 Linux 它确实不起作用。
我有一个问题,是否有一种方法可以让我将鼠标光标设置在 Windows、Linux、Mac.
的特定位置*编辑
这就是我得到的并且适用于 Windows.
public class Frame extends JFrame implements KeyListener, MouseMotionListener, MouseListener{
private static final long serialVersionUID = 4887525192006201710L;
private Frame self = this;
private JPanel buffer = new JPanel(){
public void paintComponent(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0,0, getWidth(), getHeight());
}
};
private Robot robot;
public Frame(String title){
super(title);
add(buffer);
setBounds(0,0, 640,480);
setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);
setUndecorated(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
try {
robot = new Robot();
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
addKeyListener(this);
addMouseMotionListener(this);
this.addComponentListener(new ComponentListener() {
public void componentResized(ComponentEvent e){
buffer.setBounds(0,0, getWidth(), getHeight());
}
public void componentHidden(ComponentEvent e){}
public void componentMoved(ComponentEvent e){}
public void componentShown(ComponentEvent e){}
});
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getKeyCode() == KeyEvent.VK_ENTER){
self.dispatchEvent(new WindowEvent(self, WindowEvent.WINDOW_CLOSING));
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("Mouse moved");
robot.mouseMove(getX()+getWidth()/2, getY()+getHeight()/2);
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
*解决方案
嗯,机器人工作正常。问题是没有问题(抱歉)。我以为机器人不工作是因为 VirtualBox 没有将我的鼠标移动到 Virtual Linux 屏幕的中间,但后来我发现它正在工作但是感谢 Enabled Mouse Integration我在 OS 里面没有看到原来的游标。
对于那些使用 运行 VirtualBox 并想要 禁用鼠标集成 快捷键 hostkey(right ctrl) + I
并且您的光标变为本机虚拟 OS 光标.