I am making a 3d multi-platform application and I would like to set mouse cursor always to middle. robot.mouseMove(x,y)
works fine for Windows, but for Linux it really does not work.
I have a question if there is a method that would allow me to set mouse cursor at specific position for Windows, Linux, Mac.
*EDIT
This is what I got and works fine for 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.
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.