代码之家  ›  专栏  ›  技术社区  ›  Supun Amarasinghe

机器人类粘贴到putty终端

  •  1
  • Supun Amarasinghe  · 技术社区  · 6 年前

    我正在尝试使用java程序将字符串粘贴到putty命令提示符中 Robot 班级。

    为了粘贴,我必须按 shift + insert 钥匙。什么我要做的是将字符串“HelloWorld”粘贴到putty命令提示符。下面是我的代码。

    package Auto;
    
    //Related imports
    
    public class Main {
    
        public static void main(String[] args) throws InterruptedException, AWTException {
    
            Runtime r = Runtime.getRuntime();
            Process p;
    
            String username = "ABC";
            String password = "DEF";
            String serverString = "100.0.0.0";
    
            String s = "C:\\Users\\ib\\Desktop\\Putty\\putty.exe -ssh -l " + username + " -pw " + password + " "
                    + serverString + "";
            try {
    
                p = r.exec(s);
    
                Thread.sleep(3000);
    
            } catch (Exception e) {
                System.out.println(e);
                e.printStackTrace();
    
            }
    
            String text = "HelloWorld"; 
            StringSelection stringSelection = new StringSelection(text); 
            Clipboard clipboard=Toolkit.getDefaultToolkit().getSystemClipboard();
            clipboard.setContents(stringSelection,stringSelection);
    
            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_SHIFT);
            Thread.sleep(150);
            robot.keyPress(KeyEvent.VK_INSERT); 
        }
    }
    

    此代码按 转移 按钮,但不按 插入 按钮。有人能指出我做错了什么吗?

    提前谢谢。

    0 回复  |  直到 6 年前