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 7072835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:51:26+00:00 2026-05-28T05:51:26+00:00

I m using the following loop , but its only typing the first charecter

  • 0

I m using the following loop , but its only typing the first charecter and the rest as numbers, any idea ?

import java.awt.*;

import javax.swing.KeyStroke;

public class test {

    public static void main(String[] args) throws AWTException
    {
        Robot r = new Robot();

        String s = "Face";

        for (int i = 0; i < s.length(); i++) 
        {
            char res = s.charAt(i);
            r.keyPress(res);
            r.keyRelease(res);  
            r.delay(1000);
        }           
    }
}

OUTPUT typing : F135

  • 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-28T05:51:26+00:00Added an answer on May 28, 2026 at 5:51 am

    The keyPress/Release methods need an int value that represents the character you want to type. These value are the key code for each character as determined by the KeyEvent.VK_??? variables.

    Try:

    import java.awt.*;
    import java.util.*;
    import java.lang.reflect.Field;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class RobotCharacter
    {
        public static void main(String[] args)
            throws Exception
        {
            JTextField textField = new JTextField(10);
    
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame.add( textField );
            frame.pack();
            frame.setLocationRelativeTo( null );
            frame.setVisible( true );
    
            Robot robot = new Robot();
            typeCharacter(robot, "a");
            typeCharacter(robot, "b");
            typeCharacter(robot, "C");
            typeCharacter(robot, "D");
         }
    
        public static void typeCharacter(Robot robot, String letter)
        {
            try
            {
                boolean upperCase = Character.isUpperCase( letter.charAt(0) );
                String variableName = "VK_" + letter.toUpperCase();
    
                Class clazz = KeyEvent.class;
                Field field = clazz.getField( variableName );
                int keyCode = field.getInt(null);
    
                robot.delay(1000);
    
                if (upperCase) robot.keyPress( KeyEvent.VK_SHIFT );
    
                robot.keyPress( keyCode );
                robot.keyRelease( keyCode );
    
                if (upperCase) robot.keyRelease( KeyEvent.VK_SHIFT );
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }
    }
    

    However, even this won’t work for all characters. For example on my keyboard the “%” is above the “5”. You can’t use VK_PERCENT. The key stroke needed is VK_5 along with a shift. There is no way to know the actual mapping of your keyboard to do this automatically.

    So a Robot is not a good way to do this.

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

Sidebar

Related Questions

I'm using the following to loop through all static methods in a class but
I'm using the following code to loop through a directory to print out the
I'm using the following syntax to loop through a list collection: For Each PropertyActor
I am using the following function to loop through a couple of open CDB
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
The following is a simple loop in C++. The timer is using QueryPerformanceCounter() and
I'm using Jquery mobile, so ignore some of the following overdone css, its not
I am using following PHP code to connect to MS Access database: $odb_conn =
I am using following storedprocedure set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO Alter PROCEDURE
I am using following code in rowdatabound function. Protected Sub gvwMileStone_RowDataBound(ByVal sender As System.Object,

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.