Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

key caracter from code java

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Map;

public class AsciiKeyTyper {

    private Map<Character,KeyStroke> strokeMap;
    private Robot robot;
    public AsciiKeyTyper() throws AWTException{
        robot=new Robot();
        //initialize a map from the input char to the keystroke,
        //using the fact that sometimes the KeyEvent key codes correspond to ASCII
        strokeMap=new HashMap<Character,KeyStroke>(){
                private static final long serialVersionUID = 1L;{
            put('
',new KeyStroke(KeyEvent.VK_ENTER,false));
            put('	',new KeyStroke(KeyEvent.VK_TAB,false));
            put('
',new KeyStroke(KeyEvent.VK_HOME,false));
            put(' ',new KeyStroke(KeyEvent.VK_SPACE,false));
            put('!',new KeyStroke(KeyEvent.VK_1,true));
            put('"',new KeyStroke(KeyEvent.VK_QUOTE,true));
            put('#',new KeyStroke(KeyEvent.VK_3,true));
            put('$',new KeyStroke(KeyEvent.VK_4,true));
            put('%',new KeyStroke(KeyEvent.VK_5,true));
            put('&',new KeyStroke(KeyEvent.VK_7,true));
            put(''',new KeyStroke(KeyEvent.VK_QUOTE,false));
            put('(',new KeyStroke(KeyEvent.VK_9,true));
            put(')',new KeyStroke(KeyEvent.VK_0,true));
            put('*',new KeyStroke(KeyEvent.VK_8,true));
            put('+',new KeyStroke(KeyEvent.VK_EQUALS,true));
            put(',',new KeyStroke(KeyEvent.VK_COMMA,false));
            put('-',new KeyStroke(KeyEvent.VK_MINUS,false));
            put('.',new KeyStroke(KeyEvent.VK_PERIOD,false));
            put('/',new KeyStroke(KeyEvent.VK_SLASH,false));
            for(int i=(int)'0';i<=(int)'9';i++){
                put((char)i,new KeyStroke(i,false));
            }
            put(':',new KeyStroke(KeyEvent.VK_SEMICOLON,true));
            put(';',new KeyStroke(KeyEvent.VK_SEMICOLON,false));
            put('<',new KeyStroke(KeyEvent.VK_COMMA,true));
            put('=',new KeyStroke(KeyEvent.VK_EQUALS,false));
            put('>',new KeyStroke(KeyEvent.VK_PERIOD,true));
            put('?',new KeyStroke(KeyEvent.VK_SLASH,true));
            put('@',new KeyStroke(KeyEvent.VK_2,true));
            for(int i=(int)'A';i<=(int)'Z';i++){
                put((char)i,new KeyStroke(i,true));
            }
            put('[',new KeyStroke(KeyEvent.VK_OPEN_BRACKET,false));
            put('',new KeyStroke(KeyEvent.VK_BACK_SLASH,false));
            put(']',new KeyStroke(KeyEvent.VK_CLOSE_BRACKET,false));
            put('^',new KeyStroke(KeyEvent.VK_6,true));
            put('_',new KeyStroke(KeyEvent.VK_MINUS,true));
            put('`',new KeyStroke(KeyEvent.VK_BACK_QUOTE,false));
            for(int i=(int)'A';i<=(int)'Z';i++){
                put((char)(i+((int)'a'-(int)'A')),new KeyStroke(i,false));
            }
            put('{',new KeyStroke(KeyEvent.VK_OPEN_BRACKET,true));
            put('|',new KeyStroke(KeyEvent.VK_BACK_SLASH,true));
            put('}',new KeyStroke(KeyEvent.VK_CLOSE_BRACKET,true));
            put('~',new KeyStroke(KeyEvent.VK_BACK_QUOTE,true));
        }};
    }
    public void typeKey(char key){
        try{
            strokeMap.get(key).type();
        }catch(NullPointerException ex){
            System.err.println("'"+key+"': no such key in mappings");
        }
    }
    private class KeyStroke{
        int code;
        boolean isShifted;
        public KeyStroke(int keyCode,boolean shift){
            code=keyCode;
            isShifted=shift;
        }
        public void type(){
            try{
                if (isShifted) {
                    robot.keyPress(KeyEvent.VK_SHIFT);
                }
                robot.keyPress(code);
                robot.keyRelease(code);
                if (isShifted) {
                    robot.keyRelease(KeyEvent.VK_SHIFT);
                }
                if(code==KeyEvent.VK_ENTER){
                    robot.keyPress(KeyEvent.VK_HOME);
                    robot.keyRelease(KeyEvent.VK_HOME);
                }

            }catch(IllegalArgumentException ex){
                String ch="";
                for(char key:strokeMap.keySet()){
                    if(strokeMap.get(key)==this){
                        ch=""+key;
                        break;
                    }
                }
                System.err.println("Key Code Not Recognized: '"+ch+"'->"+code);
            }
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: open cv for java 
Java :: What is builder tool 
Java :: Creategoogle maps marker with custom image/bitmap 
Java :: influx cli with docker container 
Java :: sum of no 
Java :: java get wrapper class for primitive 
Java :: Android number format thousands separator 
Java :: layout focus from recycleview not from top in fragment inside nestedScrollview in android xml 
Java :: Calculator repeat 
Java :: for loop optimized java 
Java :: java 8 lambda delete from list 
Java :: java scanner class time 
Java :: lombok error unnamed 
Java :: Start Text from top left android 
Java :: mysqld always running 
Java :: read only jtextfield 
Java :: os compatible java path separator 
Java :: ternario java 
Java :: java singleton with synchronized 
Java :: java code to implement hybrid interface 
Java :: javadoc generate docs for files in folder and subfolders 
Java :: how to create 2d arrazlist in java 
Java :: java lambda expressions qunado foi implantada 
Java :: factorial java recursion 
Java :: java load config file 
Java :: create a class 
Java :: how to move get selected from jfxtreetableview javafx 
Java :: substring in java 
Java :: Merging 2 sorted arrays (edge cases2) 
Java :: Caused by: java.lang.IllegalStateException: stream has already been operated upon or closed 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =