机器人类鼠标滚轮不工作(robot class mouseWheel not working)

2019-10-17 22:15发布

我一直在与模拟鼠标事件与一个在机器人类,并且一切都很好,直到我试图使用鼠标滚轮功能滚动。 我只是有这个简单的一行:

  Robot robot = new Robot();
  robot.mouseWheel(-100);

我一直在尝试这样的变化很长一段时间,程序运行时,什么都不做,然后正常终止。 可有人阐明了这是为什么?

谢谢!

Answer 1:

你的程序可能无法工作,因为无论有什么可scroll up的GUI到您的鼠标指针休息。 或者,你还没有休息您的鼠标指针移动到适当的GUI上,你可以看到滚动效果。 下面是简单的程序来实现这一目标。 我希望这将是对你有帮助:

import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.awt.*;

public class MouseScrollRobot extends JFrame
{

    JTextArea ta;
    boolean scrolledAway = false;
    Robot robot;
    boolean started = false;
    public void createAndShowGUI() 
    {
        setTitle("Robot Demonstration");
        JPanel panel = new JPanel();
        ta = new JTextArea();   
        StringBuilder sBuilder = new StringBuilder();
        try
        {
            robot = new Robot();
            BufferedReader bfr = new BufferedReader(new FileReader("MouseScrollRobot.java"));
            String line = null ;
            while ((line = bfr.readLine()) !=null)
            {
                sBuilder.append(line+"\n");
            }
        }
        catch (Exception ex){ex.printStackTrace();}
        ta.setText(sBuilder.toString());
        JScrollPane jsp = new JScrollPane(ta);
        final Timer timer = new Timer(100, new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent evt)
            {
                try
                {
                    robot.mouseMove((getLocationOnScreen().x + getWidth()) / 2 , (getLocationOnScreen().y + getHeight()) / 2);//Move mouse pointer to the Component which you want to scroll
                    ta.requestFocus();
                    robot.setAutoDelay(100);
                    if (!scrolledAway)
                    {
                        setTitle("Scrolling up");
                        robot.mouseWheel(-40);
                    }
                    else
                    {
                        setTitle("Scrolling Down");
                        robot.mouseWheel(40);
                    }
                    scrolledAway = !scrolledAway;
                    setTitle("Scrolled");
                }catch (Exception ex){ex.printStackTrace();}
            }
        });
        timer.setRepeats(true);
        timer.start();
        getContentPane().add(jsp);
        setSize(500,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
  }

  public static void main(String args[]) 
  {
    SwingUtilities.invokeLater( new Runnable()
    {
        @Override
        public void run()
        {
            MouseScrollRobot msr = new MouseScrollRobot();
            msr.createAndShowGUI();
        }
    });
  }
}


Answer 2:

这对我来说工作得很好...

import java.awt.AWTException;
import java.awt.Robot;

public class TestRobotScroll {

    public static void main(String[] args) {
        try {
            Robot bot = new Robot();
            bot.setAutoDelay(100);
            Thread.sleep(2000);
            System.out.println("++");
            bot.mouseWheel(25);
            Thread.sleep(2000);
            System.out.println("--");
            bot.mouseWheel(-25);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}

我有它在编辑器中滚动,滚动浏览器...



文章来源: robot class mouseWheel not working