Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6972481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:58:25+00:00 2026-05-27T16:58:25+00:00

I write a method to simulate key press from KeyEvent, like below: private Robot

  • 0

I write a method to simulate key press from KeyEvent, like below:

private Robot robot(){
        if(robot==null){
            try {
                return new Robot();
            } catch (AWTException e) {
                throw new RuntimeException("Failed to create instance of Robot");
            }
        }else{
            return robot;
        }
    }

public void sendKeyEvent(KeyEvent evt) throws IOException {
        int type = evt.getID();
        if(type == KeyEvent.KEY_PRESSED){
            if(evt.isShiftDown()){
                robot().keyPress(KeyEvent.VK_SHIFT);
            }
            robot().keyPress(evt.getKeyChar());
        }else if(type == KeyEvent.KEY_RELEASED){
            robot().keyRelease(evt.getKeyChar());
            if(evt.isShiftDown()){
                robot().keyRelease(KeyEvent.VK_SHIFT);
            }
        }
    }

When this method received press ‘A’ key event, it could type ‘A’.

java.awt.event.KeyEvent[KEY_PRESSED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='A',modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_UNKNOWN]]

But the problem is when it received this KeyEvent(press ‘a’), it acturaly pressed “1”.

java.awt.event.KeyEvent[KEY_PRESSED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='a',keyLocation=KEY_LOCATION_UNKNOWN]]

Could anyone tell me what wrong with this method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:58:25+00:00Added an answer on May 27, 2026 at 4:58 pm

    It’s a bit tricky and confusing and you got confused.

    There are no ‘uppercase a’ and ‘lowercase a’ key events. There are just ‘A/a’ events and you can have or not a SHIFT modifier.

    It just happen to be that VK_A to VK_Z are identical to ASCII ‘A’ through ‘Z’ but not so for ‘a’ to ‘z’.

    When you’re re-sending the ‘a’ (ASCII 0x61, aka 97) that you got from getKeyChar(), you’re actually sending VK_NUMPAD1, which is why you get the ‘1’.

    The JavaDoc for getKeyChar says this:

    getKeyChar() Returns the character associated with the key in this
    event. For example, the KEY_TYPED event for shift + “a” returns the
    value for “A”

    So when you try with ‘A’, you get back VK_A and things work as you expect. But when you simply type ‘a’, you get 0x61 which is not what you want.

    As far as I can tell changing getKeyChar() to getKeyCode() would fix your problem.

    That said I wouldn’t go messing with KEY_PRESS/KEY_RELEASED. I’d simply intercept KEY_TYPED and “Robot” from there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

serial.write() method in pyserial seems to only send string data. I have arrays like
How do you write a method/message with multiple parameters? EDIT: Like multiple parameters for
Is it possible to write extension method in PowerShell? or to bolt a new
I'm trying to write a method like this: public static T Test<T>() { if
I would like to write a method in ruby that takes a class with
I'm stuck with this Java homework question: Write a new method, Refund, that simulates
I want to write method to send SMS on number and with text from
I wanted to write a method with an argument that defaults to a member
I'm wanting to write a method that I can use to initialise a Map.
I want to re-write a method that has way too many nested if statements.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.