将虚拟键盘Android实现到PC

时间:2017-04-07 09:19:28

标签: java android sockets keyboard

我试图将我的Android手机用作PC的键盘。 这是通过使用套接字创建客户端/服务器连接的本地WiFi连接完成的。

我使用textwatcher来获取editText中字符的更改,然后将这些更改发送到服务器,然后服务器使用机器人类将其写入PC。这都是在Android手机上使用软键盘。 问题是当在editText中写入时,只发送一个字符。并由服务器拿起。

e.g。我输入了Android手机" a"并且将在服务器上模拟相应的按键。如果我然后键入另一个字符,它不会读取该字符,但如果我删除" a"然后键入它将发送该字符的另一个字符。

以下是代码的客户端,即Android应用程序。

public class MainActivity extends AppCompatActivity implements View.OnKeyListener {


private final static int portNumber = ****;
private final static String hostName = "192.168.0.8";
private final static String cTest = "Connection Test";
Socket socket = null;
private static String TAG = "test";




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new Thread() {
        @Override
        public void run() {


            try {
                Log.i(cTest, "attempting to connect");
                socket = new Socket(hostName, portNumber);
                Log.i(cTest, "Connected");

                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(getBaseContext(), "Connected Successfully", Toast.LENGTH_SHORT).show();
                    }
                });

                EditText editText = (EditText)findViewById(R.id.editText);
                editText.addTextChangedListener(editTextWatcher);

           } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.e(cTest, e.getMessage());
            }
        }

    }.start();
}


 private TextWatcher editTextWatcher = new TextWatcher() {

  private void sendTextToServer(String send) {

      BufferedWriter bw = null;

      String textChange = send;


      try {
          bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
          bw.write(textChange);
          bw.newLine();
          bw.flush();

      } catch (IOException e) {

      }
  }

    public void afterTextChanged(Editable s)
    {
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after)
    {
    }

    public void onTextChanged(CharSequence s, int start, int before, int count)
    {
            Log.d(TAG, s + " Character Read");

                String test = s.toString();
                this.sendTextToServer(test);
    }
};


@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {

    return false;
 }
}

以下代码是在eclipse中完成的服务器端代码。

public class ListenServerMain {

private static boolean ifConnected = true;
private final static int portNumber = ****;
public static String readIn;
private static Socket client = null;
private static ServerSocket server = null;
private static BufferedReader in = null;
private static Robot robot = null;


public static void main(String[] args) {
    // TODO Auto-generated method stub

        try {
                robot = new Robot();
                server = new ServerSocket(portNumber);


                System.out.println("Listening on port :****");
                client = server.accept(); //loops until a client server connection is established
                System.out.println("Client connected");

                //Receive a message from the client
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(client.getInputStream()));

                while(ifConnected)
                {

                        readIn = in.readLine();     //reads the input received

                        if(readIn.equalsIgnoreCase("A"))
                                {
                                    robot.keyPress(KeyEvent.VK_A);
                                    robot.keyRelease(KeyEvent.VK_A);

                                }
                        if(readIn.equalsIgnoreCase("B"))
                                {
                                    robot.keyPress(KeyEvent.VK_B);
                                    robot.keyRelease(KeyEvent.VK_B);
                                }
                        if(readIn.equalsIgnoreCase("C"))
                                {
                                    robot.keyPress(KeyEvent.VK_C);
                                    robot.keyRelease(KeyEvent.VK_C);
                                }
                        if(readIn.equalsIgnoreCase("BS"))
                        {
                            robot.keyPress(KeyEvent.VK_DELETE);
                            robot.keyRelease(KeyEvent.VK_DELETE);
                        }
                                }                               
                                System.out.println("Unable to read user Input");            //if it can't read the input from the Client
                                System.exit(-1);                                            //system exits


                System.out.println("Server Ended");
                System.exit(-1);

            }   
                catch (IOException e) {
                    System.out.println("Can't open the Socket");
                    System.exit(-1);                            
            }
                catch (AWTException e) {
                    System.out.println("Unable to create Robot");
                    System.exit(-1);                            
            }





    }
}

有什么方法可以成功阅读并发送在editText字段中输入的每个字符?

如果有必要进一步澄清,请告诉我。

谢谢

0 个答案:

没有答案