Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Robot Invalid KeyCode from Constant

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Robot Invalid KeyCode from Constant

    I am trying to create a Programm that pastes given text into a window that does not support ctrl v.
    This works except for certain symols, which the keyPress method of the Robot class rejects, although they are defined by the -according to the KeyEvent api- correct constants.
    This is an extacted segment of said code in a seperate eclipse project.
    public class Test { //NOSONAR
    	public static void main(String[] a) {
    		int i = KeyEvent.getExtendedKeyCodeForChar('[');
    		System.out.println(i); // prints 91
    		System.out.println(KeyEvent.VK_OPEN_BRACKET); //prints 91
     
    		try (final Scanner scanner = new Scanner(System.in);) {
    			final Robot bot = new Robot();
    			bot.setAutoDelay(30);
    		    // here's the problem
    			bot.keyPress(KeyEvent.VK_OPEN_BRACKET);
    			bot.keyRelease(KeyEvent.VK_OPEN_BRACKET);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    running this code throws a Invalid key code Exception for me
    Console Output:
    91
    91
    java.lang.IllegalArgumentException: Invalid key code
    at sun.awt.windows.WRobotPeer.keyPress(Native Method)
    at java.awt.Robot.keyPress(Unknown Source)
    at main.Test.main(Test.java:24)
    Last edited by zirrboy; March 5th, 2019 at 12:42 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,318
    Thanks
    53
    Thanked 2,392 Times in 2,346 Posts

    Default Re: Robot Invalid KeyCode from Constant

    throws a Invalid key code Exception
    Please copy the full text of the error message and paste it here.
    Also include the invalid value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Robot Invalid KeyCode from Constant

    This is an extacted segment of said code in a seperate eclipse project.
    Was that all the code?
    This works except for certain symols
    And has it been working before?
    "Tick, tack"

  4. #4
    Junior Member
    Join Date
    Mar 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Robot Invalid KeyCode from Constant

    The Input value is 91 which is stored in KV_OPEN_BRACKET.
    As for MrLowBot:
    this is more or less the entire Code as far as functionalty is concerned
    the main Programm has a .txt file Reader and loops through the characters, using a variable
    instead of the referenced constant. There is a mapping class too but it's Output is correct according to the KeyEvent api.
    And as far as i have been able to find out this is more or less limited to smbols that would require a key omination other than shift

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,318
    Thanks
    53
    Thanked 2,392 Times in 2,346 Posts

    Default Re: Robot Invalid KeyCode from Constant

    The code executes without an exception for me. It inserts a [ at the cursor's location in a document.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Mar 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Robot Invalid KeyCode from Constant

    After some experimenting, i found a semi-solution.
    changing your keybord layout to US will make the code run smoothly.
    So if you are experiencing the same Problem, look up how to change your
    Keyboard layout at least for the running time of the programm.
    Since Java will only give you keycodes based on US keylayouts, this is probably the
    cleanest solution, the other being finding the matching keycodes on your layout yourself
    and hardcoding a map.

Similar Threads

  1. Replies: 3
    Last Post: April 25th, 2013, 04:21 PM
  2. Doubt on a logic to store constant in file.
    By smilyface in forum Java Theory & Questions
    Replies: 3
    Last Post: August 24th, 2012, 06:38 AM
  3. Annotation on enum constant.
    By Doctor X in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 22nd, 2012, 09:43 AM
  4. Robot
    By imsuperman05 in forum Java Theory & Questions
    Replies: 19
    Last Post: December 26th, 2011, 12:08 PM
  5. Robot help.
    By mlan in forum AWT / Java Swing
    Replies: 1
    Last Post: February 14th, 2010, 03:26 PM