简体   繁体   English

发送命令进行处理

[英]send commands to process

I wanna to create a programm which starts a Textfield. 我想创建一个启动Textfield的程序。 In the Textfield I can write commands that will start another process. 在文本字段中,我可以编写将启动另一个进程的命令。

Example: Textfield: "open firefox olaf" Then the programm start firefox. 示例:文本字段:“ open firefox olaf”然后程序将启动firefox。 Olaf is the Signal that the command ends. Olaf是命令结束的信号。

My programm compare the Textfield String array with a commands String array and if for example the word "open" equal in both string array the be run in a switch case method the following method "open". 我的程序将Textfield String数组与命令String数组进行比较,例如,如果单词“ open”在两个字符串数组中均相等,则可以在切换案例方法中运行以下方法“ open”。 thats works good. 多数民众赞成在很好。

My Problem: Now I wanna a command like this: Textfield: "type in firefox www.web.de olaf" The programm should focused on firefox and should typen in firefox "www.web.de", but I don't know how to do this. 我的问题:现在我想要一个这样的命令:文本字段:“键入firefox www.web.de olaf”该程序应专注于firefox,并键入firefox“ www.web.de”,但我不知道如何去做这个。 I don't wanna use the robot class with robot.keyPressed() 我不想将机器人类与robot.keyPressed()

Also I wanna to type in the textfield of the websites. 我也想输入网站的文本字段。

Why I do this? 我为什么要这样做? I just wanna learn java and use Voice speeches, so I dont need to type "open firefox olaf". 我只是想学习Java并使用语音语音,所以不需要输入“ open firefox olaf”。 I can just said it;) 我可以说;)

if I need to do with C++ it's okay 如果我需要使用C ++可以

import java.awt.BorderLayout;
import java.awt.Robot;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class KeyEventClass extends JFrame implements KeyListener, FocusListener {

    private static final long serialVersionUID = 1L;

    public KeyEventClass(){
        this.setLayout(new BorderLayout());
        JTextField field = new JTextField();
        field.addKeyListener(this);
        this.add(field, BorderLayout.CENTER);
        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);

        this.addWindowListener( new WindowAdapter() {
            public void windowActivated( WindowEvent e ){
                field.requestFocus();
                field.setText("");
            }
        }); 
    }


    String[] Commands = {"open"};  
    String[] Input = {"","","","","","","","","","","","","","",""} ;
    int i = 0;


    // Save the input in the String[] Input Variable but without VK_SPACE    
    public void keyPressed(KeyEvent e){ 

        if(i > 20) { System.out.println("Error Index over..");}
        if(e.getKeyChar()==KeyEvent.VK_SPACE){

            i++;

        }
        if( e.getKeyChar() != KeyEvent.VK_SPACE ) Input[i] += e.getKeyChar();

    }

    // Compare Input and Commands    
    public void keyReleased(KeyEvent e) {

        if( Input[i].compareTo("olaf")==0){ // olaf is a Signal of the end of the command line
            for(int k =0; k< Commands.length;k++){
                for(int z=0; z < Input.length;z++){

                    if(Input[z].compareTo(Commands[k])==0){

                        switch(k){
                        case 0: open(Input[z+1]);  //after start follow the name of the process 
                                System.out.println("case0 = open"); 
                                delete();
                                break;
                        }

                    }
                }
            }

        }
    }

    public void open(String process){

        try {
            Robot robot= new Robot();
            ProcessBuilder pb = new ProcessBuilder("cmd", "/c",
                    "C:\\Verknupfung\\"+process+".lnk");         //"C:\\Verknupfung\\" is a Path to start Process
            pb.start();

            robot.delay(1000);

            this.requestFocus();                                //set focus on KeyEventClass after start process

            } catch (Exception x) {System.out.println("Error to starts process:"+x);}   
    }

    public void delete(){
         for(int k=0;k< Input.length;k++) Input[k] = "" ;
         i=0;

    }

    public static void main(String[] args) {
        new KeyEventClass();
    }

    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void focusGained(FocusEvent arg0) {  
    }

    @Override
    public void focusLost(FocusEvent arg0) {
        // TODO Auto-generated method stub
    }

} 

You can use command line arguments to achieve that. 您可以使用命令行参数来实现。

./firefox www.stackoverflow.com ./firefox www.stackoverflow.com

so u can use the ProcessBuilder to pass it in : 因此您可以使用ProcessBuilder将其传递给:

String urlAddress = someJTextField.getText();
ProcessBuilder pb = new ProcessBuilder("/firefox", urlAddress);
pb.start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM