如何让我的程序在另一个程序中编写?

时间:2014-12-12 23:21:09

标签: java swing

我正在制作一个宏程序。当CTRL模式打开时,程序会在用户单击时注册鼠标坐标。按下“CTRL”时会激活CTRL模式,再次按下时会取消激活。如果取消激活,程序会询问“执行操作?”如果响应为是,则程序单击已注册的坐标。

如果用户在CTRL模式下写了一些内容,我现在正试图让程序写入。这可能吗?如果是这样,怎么样?

编辑:我的解释很差,所以我决定添加一个例子。

实施例

我激活了CTRL模式。然后单击记事本并写上“hello”。然后我停用CTRL并运行操作。程序会点击记事本并写上“你好”。

我认为你不需要这个,但这里是代码:

public class Gui extends JFrame {
private JPanel mousePanel;

private JLabel statusBar;
private JLabel keyBar;

public boolean ctrl;

List<Integer> xList = new ArrayList<Integer>();
List<Integer> yList = new ArrayList<Integer>();
List<KeyEvent> charTyped = new ArrayList<KeyEvent>(); 

public int[] x;
public int[] y;

public Gui() {
    super("Program");
    mousePanel = new JPanel();
    mousePanel.setBackground(Color.WHITE);
    add(mousePanel, BorderLayout.CENTER);
    statusBar = new JLabel("No events");
    keyBar = new JLabel("No key events");
    add(keyBar, BorderLayout.NORTH);;
    add(statusBar, BorderLayout.SOUTH);

    HandlerClass handler = new HandlerClass();
    mousePanel.addMouseListener(handler);
    mousePanel.addMouseMotionListener(handler);
    this.addKeyListener(handler);
}


public void Click(int x, int y) throws AWTException {
    Robot bot = new Robot();
    bot.mouseMove(x, y);
    bot.mousePress(InputEvent.BUTTON1_MASK);
    bot.mouseRelease(InputEvent.BUTTON1_MASK);
}

private class HandlerClass implements MouseListener, MouseMotionListener, KeyListener {

    //Mouse Listener
    public void mouseClicked(MouseEvent event) {
        statusBar.setText(String.format("Clicked at %d, %d", event.getX(), event.getY()));
        if(ctrl) {
            xList.add(MouseInfo.getPointerInfo().getLocation().x);
            yList.add(MouseInfo.getPointerInfo().getLocation().y);
        }
    }

    public void mousePressed(MouseEvent event) {
        statusBar.setText(String.format("You are pressing the mouse at %d, %d", event.getX(), event.getY()));
    }

    public void mouseReleased(MouseEvent event) {
        statusBar.setText(String.format("Released at %d, %d", event.getX(), event.getY()));
    }

    public void mouseEntered(MouseEvent event) {
        statusBar.setText(String.format("Mouse entered at %d, %d", event.getX(), event.getY()));
        mousePanel.setBackground(Color.RED);
    }

    public void mouseExited(MouseEvent event) {
        statusBar.setText(String.format("Mouse exited at %d, %d", event.getX(), event.getY()));
        mousePanel.setBackground(Color.WHITE);
    }

    //Mouse Motion
    public void mouseDragged(MouseEvent event) {
        statusBar.setText(String.format("Dragging mouse at %d, %d", event.getX(), event.getY()));
    }

    public void mouseMoved(MouseEvent event) {
        statusBar.setText(String.format("Moving mouse at %d, %d", event.getX(), event.getY()));
    }

    //Key Listener
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == e.VK_CONTROL && !(ctrl)){
            keyBar.setText("CTRL ON");
            ctrl = true;
        }
        else if(e.getKeyCode() == e.VK_CONTROL && ctrl) {
            keyBar.setText("CTRL OFF");
            ctrl = false;
             if(JOptionPane.showOptionDialog(null, "Perform actions?", "", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null) == JOptionPane.YES_OPTION) {
                 int index = 0;
                 for(int actionX : xList) {
                     try {
                        Click(actionX, yList.get(index));
                    } catch (AWTException e1) {
                        e1.printStackTrace();
                    }
                     index++;
                     try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                 }
             }
        }
    }

    public void keyReleased(KeyEvent e) {

    }

    public void keyTyped(KeyEvent e) {

    }
}
}

1 个答案:

答案 0 :(得分:1)

在看到您的最新修改时,this is probably what you're looking for

我删除了帖子中的其他内容,因为我不断收到旧帖的评论。