我对此完全感到困惑,我在循环中有一个 switch 语句:
package com.example.project
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Keyserver {
static ServerSocket server;
static Socket soc;
static ObjectInputStream in;
static int key;
static Robot bot;
static boolean shift = false;
public static void main(String[] args) throws AWTException{
bot = new Robot();
while(true){
try {
server = new ServerSocket(4321, 10);
soc = server.accept();
System.out.println("Accepted port");
in = new ObjectInputStream(soc.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
while(soc.isConnected()){
try {
key = in.readInt();
System.out.println(key);
switch(key){
case(7):
bot.keyPress(KeyEvent.VK_0);
bot.keyRelease(KeyEvent.VK_0);
break;
case(8):
bot.keyPress(KeyEvent.VK_1);
bot.keyRelease(KeyEvent.VK_1);
break;
case(9):
bot.keyPress(KeyEvent.VK_2);
bot.keyRelease(KeyEvent.VK_2);
break;
case(10):
bot.keyPress(KeyEvent.VK_3);
bot.keyRelease(KeyEvent.VK_3);
break;
....
case(53):
bot.keyPress(KeyEvent.VK_Y);
bot.keyRelease(KeyEvent.VK_Y);
break;
case(54):
bot.keyPress(KeyEvent.VK_Z);
bot.keyRelease(KeyEvent.VK_Z);
break;
case(4):
bot.keyPress(KeyEvent.VK_BACK_SPACE);
bot.keyRelease(KeyEvent.VK_BACK_SPACE);
break;
case(62):
bot.keyPress(KeyEvent.VK_SPACE);
bot.keyRelease(KeyEvent.VK_SPACE);
break;
case(59):
if(!shift){
bot.keyPress(KeyEvent.VK_SHIFT);
}else{
bot.keyRelease(KeyEvent.VK_SHIFT);
}
shift = !shift;
break;
case(60):
if(!shift){
bot.keyPress(KeyEvent.VK_SHIFT);
}else{
bot.keyRelease(KeyEvent.VK_SHIFT);
}
shift = !shift;
break;
case(90):
bot.keyPress(KeyEvent.VK_1);
bot.keyRelease(KeyEvent.VK_1);
break;
case(91):
bot.keyPress(KeyEvent.VK_2);
bot.keyRelease(KeyEvent.VK_2);
break;
case(92):
bot.keyPress(KeyEvent.VK_0);
bot.keyRelease(KeyEvent.VK_0);
break;
case(93):
bot.keyPress(KeyEvent.VK_3);
bot.keyRelease(KeyEvent.VK_3);
break;
case(104):
bot.keyPress(KeyEvent.VK_ENTER);
bot.keyRelease(KeyEvent.VK_ENTER);
break;
case(105):
bot.keyPress(KeyEvent.VK_BACK_SPACE);
bot.keyRelease(KeyEvent.VK_BACK_SPACE);
break;
case(106):
bot.keyPress(KeyEvent.VK_MINUS);
bot.keyRelease(KeyEvent.VK_MINUS);
break;
case(107):
bot.keyPress(KeyEvent.VK_EQUALS);
bot.keyRelease(KeyEvent.VK_EQUALS);
break;
case(108):
bot.keyPress(KeyEvent.VK_TAB);
bot.keyRelease(KeyEvent.VK_TAB);
break;
case(109):
bot.keyPress(KeyEvent.VK_ESCAPE);
bot.keyRelease(KeyEvent.VK_ESCAPE);
break;
}
} catch (IOException e) {
try {
e.toString();
in.close();
soc.close();
server.close();
break;
} catch (IOException e1) {
e1.toString();
}
}
}
System.out.println("PORT CLOSED");
}
}
}
一切正常,除了 90-93,它们导致程序永远循环,直到我点击 eclipse 然后它停止。这不是日食问题,因为当我将它构建到 .jar 中时,我得到了相同的结果,这不是服务器问题,因为当我制作发送 90 发送 7 的按钮时,它工作正常
似乎只有 90-93 导致无限循环,我已经尝试将数字从 90 更改为 200 但没有运气
任何启蒙者?