Java Robot Linux stimulate all key events

2019-08-11 03:07发布

I am trying to type all the possible keys from Keyboard, irrespective of languages or layout using Java Robot.

We have written a super class which has common methods to print characters KeyEvent.VK_A to KeyEvent.VK_Z, number KeyEvent.VK_0 to KeyEvent.VK_9 along with SHIFT key combination.

Now coming to specific part that is if we are having Italy or Turkish Keyboard which are having Unicode present or if any other special character. How can I stimulate the same keypress or keyrelease ? (as there is no valid KeyCode present for such characters)

Say we have ò,à,ù or *,§, etc

I am trying this on JDK 1.6. Here is Code snippet

protected void keyPressRelease(int keyCode){

    try{
        this.robot.keyPress(keyCode);
        this.robot.keyRelease(keyCode);
    }catch(java.lang.IllegalArgumentException e){
        logger.error("keyPressRelease() No key present "+KeyEvent.getKeyText(keyCode)+ " for Key Code :"+keyCode);
        logger.error("Error in typing ",e);
        this.errorBuilder.append("IllegalArgumentException keyPressRelease(), No key present "+KeyEvent.getKeyText(keyCode)+ " for Key Code :"+keyCode+"<br\\>");
    }catch(Exception e){
        logger.error("keyPressRelease() No key present "+KeyEvent.getKeyText(keyCode)+ " for Key Code :"+keyCode);
        logger.error("Error in typing ",e);
        this.errorBuilder.append("Exception keyPressRelease(), No key present "+KeyEvent.getKeyText(keyCode)+ " for Key Code :"+keyCode+"<br\\>");
    }
}//End of keyPressRelease()


protected void shiftKeyPress(int keyCode){

    this.robot.keyPress(KeyEvent.VK_SHIFT);

    try{

        this.robot.keyPress(keyCode);                    
        this.robot.keyRelease(keyCode);

    }catch(java.lang.IllegalArgumentException e){
        logger.error("shiftKeyPress() No key present "+KeyEvent.getKeyText(keyCode)+ " for Key Code :"+keyCode);
        this.errorBuilder.append("Exception shiftKeyPress(), No key present "+KeyEvent.getKeyText(keyCode)+ " for Key Code :"+keyCode+"<br\\>");
    }
    this.robot.keyRelease(KeyEvent.VK_SHIFT);

}//End of shiftKeyPress()



protected void typeLableName(int keyCode){
    String  labelName = KeyEvent.getKeyText(keyCode);
    type(labelName+" ");

    labelName = null;
}//End of typeLableName

//Windows 7 , normal keyboard, from Control panel changed the layout to Italy
private void checkSplCharacters(){

    this.keyPressRelease(KeyEvent.VK_ENTER);

    /*Start of  --> § Cedilla Small*/
    this.typeLableName(KeyEvent.VK_BACK_SLASH);
    this.keyPressRelease(KeyEvent.VK_BACK_SLASH);
    this.keyPressRelease(KeyEvent.VK_ENTER);

    this.typeLableName(KeyEvent.VK_BACK_SLASH);
    this.shiftKeyPress(KeyEvent.VK_BACK_SLASH);
    this.keyPressRelease(KeyEvent.VK_ENTER);
    /*End  of  --> § Cedilla Small*/        

    /*Start of  --> ì Grave Small*/
    this.typeLableName(KeyEvent.VK_PLUS);
    this.keyPressRelease(KeyEvent.VK_PLUS);     
    this.keyPressRelease(KeyEvent.VK_ENTER);

    this.typeLableName(KeyEvent.VK_PLUS);
    this.shiftKeyPress(KeyEvent.VK_PLUS);
    this.keyPressRelease(KeyEvent.VK_ENTER);
    /*End  of  --> ì Grave Small*/


}//End of checkSplCharacters

some of the points running in mind

  1. A dummy point, is it possible to know the position of the Keys say mapping in the format of rows & columns? If so which API will return the keyboards rows? & then I will try to run in loop for that row & that key.

  2. Read some places with Numpad + ALT key, also it seems this works on Windows & what if Numpad is not there ? Took a refernce from here

  3. Can we create our own customized mapping for same & is there a possible way to overwrite ?(any snippet for it)

Or any other possible way to type/ perform java robot for unicode/special characters.

Am trying some what below thing

Basically want to type ò,à,ù or *,§, using java Robot. So String a = "ò,à,ù or *,§"; //Unicode charactres try{ Robot robot = new Robot(); for(int i=0;i

     robot.keyPress(?);//What would be the equivalent key code for above Unicodes
    }

    }catch(Exception e){
    } 

0条回答
登录 后发表回答