gpt4 book ai didi

java - 如何通过Android套接字获取输入的字符到java服务器

转载 作者:行者123 更新时间:2023-12-02 05:03:54 24 4
gpt4 key购买 nike

我正在 Android 上创建一个远程 PC 应用程序。我编写了一段代码来获取按下的按键的 keyunicode,如下所示

AndroiClient.java

public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN){
int keyUniCode=event.getUnicodeChar(event.getMetaState());
if(keyUniCode!=0){
char charCode=(char) keyUniCode;
Log.i("Code:",""+keyUniCode);
SendMessage msg=new SendMessage(charCode);
msg.execute();
}
}
return super.dispatchKeyEvent(event);
}

并且使用 PrintWriter 对象在 java 服务器上接收 iti。

JavaServer.java

public class KeyListener {

private Robot robot;
private static Socket client;
private static ServerSocket server;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;
private static KeyListener keyboard;
public static void main(String... args) throws Exception {
keyboard = new KeyListener();
server=new ServerSocket(6666);
System.out.println("Waiting");
while(true){
client=server.accept();
System.out.println("Accepted");
inputStreamReader=new InputStreamReader(client.getInputStream());
bufferedReader=new BufferedReader(inputStreamReader);
message=bufferedReader.readLine();
System.out.println(message);
keyboard.type(message);
}
}

public KeyListener() throws AWTException {
this.robot = new Robot();
}

public KeyListener(Robot robot) {
this.robot = robot;
}

public void type(CharSequence characters) {
int length = characters.length();
for (int i = 0; i < length; i++) {
char character = characters.charAt(i);
type(character);
}
}

class MyException extends IllegalArgumentException{
public MyException(String message) {
super(message);
}
}

public void type(char character) {
switch (character) {
case 'a': doType(VK_A); break;
case 'b': doType(VK_B); break;
case 'c': doType(VK_C); break;
case 'd': doType(VK_D); break;
case 'e': doType(VK_E); break;
.
.
.
.//other cases
.
.
.
case '\n': doType(VK_ENTER); break;
.
.
.
.//other cases
.
default:
throw new MyException("Cannot type character " + character);
}
}

private void doType(int... keyCodes) {
doType(keyCodes, 0, keyCodes.length);
}

private void doType(int[] keyCodes, int offset, int length) {
if (length == 0) {
return;
}

robot.keyPress(keyCodes[offset]);
doType(keyCodes, offset + 1, length - 1);
robot.keyRelease(keyCodes[offset]);
}
}

现在我面临的问题是 Enter 和 Backspace 键在 android 中不会生成任何 Unicode。那么我怎样才能获得java的Enter和BacSpace按键

最佳答案

沿着这些思路应该有所帮助

public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN){
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
SendMessage msg = new SendMessage(some identifier for enter);
} else if (event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
SendMessage msg = new SendMessage(some identifier for delete);
} else {

int keyUniCode=event.getUnicodeChar(event.getMetaState());
if(keyUniCode!=0){
char charCode=(char) keyUniCode;
Log.i("Code:",""+keyUniCode);
SendMessage msg=new SendMessage(charCode);
msg.execute();
}
}
}
return super.dispatchKeyEvent(event);
}

关于java - 如何通过Android套接字获取输入的字符到java服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27987358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com