Java never ends an eclipse loop (sometimes)

Im totally puzzled by this, I have a switch statement inside a loop:

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"); } } } 

everything works fine, with the exception of 90-93, they make the program loop forever until I click on eclipse and it stops working. This is not an eclipse problem, because when I create it in .jar, I got the same result and not a server problem, because when I made a button that sent 90 send 7, it worked fine

It seems that only 90-93 causes an infinite loop, I already tried to change the numbers from 90 to about 200, but no luck

any enlightened guys?

+4
source share
2 answers

Sounds like this code:

 bot.keyPress(KeyEvent.VK_1); bot.keyRelease(KeyEvent.VK_1); 

hanging for some reason. Nothing to do with the switch statement.

0
source

Ok guys, I figured it out. You won’t believe it.

My servers is a virtual device for Android. I wanted to make a remote keyboard application, so I wrote a server to send each keystroke on my virtual keyboard to a client, which would then emulate my keystroke on my computer.

Problem? It worked. My client will emulate keypress on my computer, which will then emulate a keystroke on my Android emulator (if you press a key on the keyboard, it will press that key on the Android keyboard), which will send the client to press the key to press the key again, creating an endless loop .

This explains why when I focused on the eclipse window and not on the android window, the server stopped receiving numbers because the Android device stopped receiving keystrokes

Thank you for your help, although they really appreciated

0
source

Source: https://habr.com/ru/post/1435826/


All Articles