24 Sept 2012

More KeyEvents in java swings

/* key events in java with examples, More Key Events in java swings */

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 
java.net.ServerSocket server;

static java.net.Socket soc;
static java.io.ObjectInputStream in;
static int key;
static 
java.awt.Robot bot;

static boolean shift = false;

public static void main(String[] args) throws 
java.awt.AWTException

{
    bot = new java.awt.Robot();
    while(true)
    {
       try 

       {
            server = new 
java.net.ServerSocket(1111, 10);

            soc = server.accept();
            System.out.println("Accepted port");
            in = new 
java.io.ObjectInputStream(soc.getInputStream());

        } 
        catch (java.io.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 (java.io.IOException e) 
              {
                try {
                    e.toString();
                    in.close();
                    soc.close();
                    server.close();
                    break;
                } 

                catch (java.io.IOException e1) 
                {
                    e1.toString();
                }  
            }
        }
        System.out.println("PORT CLOSED");
    }
}
}

0 comments:

Post a Comment