Automatic Text Typing in Java

In this example, We are attaching simple program about, How to Create Automatic Text Typing in Java. The example were tested and shared in the post.

AutomaticText.java

package com.dineshkrish;
import java.awt.AWTException;
import java.awt.Desktop;
import java.awt.Robot;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
/**
* 
* @author Dinesh Krishnan
*
*/
public class AutomaticText {
// Duration to Start Typing the Text
private static final int DURATION = 5000;
public static void main(String[] args) {
// Defining the Scanner Object
Scanner scanner = new Scanner(System.in);
Robot robot = null;
try {
// Defining Robot Object
robot = new Robot();
System.out.println("Enter some text(without symbol and number) here");
// Reading the Input text from user
String name = scanner.nextLine();
if (name != null && !name.isEmpty()) {
// Converting the Lower Case
name = name.toLowerCase();
// Converting String to Character Array
char[] charArray = name.toCharArray();
try {
// Defining temp file object
File file = new File("temp.txt");
// Creating temp file
file.createNewFile();
// Opening the Text Editor with Temp file
Desktop.getDesktop().edit(file);
} catch (IOException e) {
e.printStackTrace();
}
// Starting duration
Thread.sleep(DURATION);
for (int i = 0; i < charArray.length; i++) {
// Typing Speed
Thread.sleep(250);
if (charArray[i] >= 97 && charArray[i] <= 123) {
robot.keyPress(charArray[i] - 32);
} else if (charArray[i] == 32) {
robot.keyPress(32);
}
}
} else {
System.out.println("Invalid Text Given");
System.exit(0);
}
} catch (AWTException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
// Closing the Resources
scanner.close();
}
}

Output

Enter some text(without symbol and number) here
dinesh krishnan

References

1. java.util Package JavaDocs
2. java.awt.Robot JavaDocs

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *