[英]Java linux how to move mouse cursor
I am making a 3d multi-platform application and I would like to set mouse cursor always to middle. 我正在制作3D多平台应用程序,我想将鼠标光标始终设置在中间。 robot.mouseMove(x,y)
works fine for Windows, but for Linux it really does not work. robot.mouseMove(x,y)
在Windows上工作正常,但在Linux上确实不起作用。
I have a question if there is a method that would allow me to set mouse cursor at specific position for Windows, Linux, Mac. 我有一个问题,是否有一种方法可以让我将鼠标光标设置在Windows,Linux和Mac的特定位置。
*EDIT *编辑
This is what I got and works fine for Windows. 这就是我得到的,并且在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
}
}
*SOLUTION *解
Well, robot works fine. 好吧,机器人工作正常。 Problem was that there was no problem (Sorry). 问题是没有问题(对不起)。 I thought robot is not working because VirtualBox was not moving my mouse to middle of the screen on Virtual Linux, but then I found out it is working but thanks to Enabled Mouse Integration I did not see original inside OS cursor. 我以为机器人无法正常工作,因为VirtualBox不能将鼠标移至Virtual Linux的屏幕中间,但是后来我发现它可以正常工作,但是由于启用了鼠标集成,所以我没有看到原始的OS光标。
For those what run VirtualBox and want to Disable Mouse Integration press key shortcut hostkey(right ctrl) + I
and your cursor become native Virtual OS cursor. 对于那些运行VirtualBox并要禁用鼠标集成的用户,请按快捷键hostkey(right ctrl) + I
,您的光标将变为本机Virtual OS光标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.