DEV Community

Cover image for Type in the Notepad using Java
Satvik
Satvik

Posted on

4 3

Type in the Notepad using Java

Let's make a java program that will open up notepad and type some text in it .

You will have to import 3 libraries which we would be using :

import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
Enter fullscreen mode Exit fullscreen mode

Create a class and add a main method , in this program we will have to throw 3 exceptions

  1. IOException
  2. InterruptedException
  3. AWTException

We will write this line to open up notepad

Runtime.getRuntime().exec("notepad.exe");
Enter fullscreen mode Exit fullscreen mode

We will use Thread.sleep to create a delay in out typewriter so that notepad opens up and out typewriter doesn't type anywhere else

Thread.sleep(2000);
Enter fullscreen mode Exit fullscreen mode

Then we will use the Robot Class to create the typewriter effect

r.keyPress(KeyEvent.VK_H);
Enter fullscreen mode Exit fullscreen mode

The line above display the letter H on the notepad .Like this we can print any letter to form words and sentences.

We can then use Thread.sleep to have a pause and create a natural typing effect

Thread.sleep(500); // 0.5 seconds
Enter fullscreen mode Exit fullscreen mode

Thread.sleep() takes argument in milliseconds

Using this you can display multiple letters in the notepad

import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.IOException;

public class notepadType{
public static void main (String[]args) throws IOException, InterruptedException, AWTException{
Runtime.getRuntime().exec("notepad.exe");

Thread.sleep(2000); // 2 seconds
Robot r=new Robot();
r.keyPress(KeyEvent.VK_H);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_E);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_L);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_L);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_O);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_SPACE); // displays a whitespace
Thread.sleep(500);
r.keyPress(KeyEvent.VK_W);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_O);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_R);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_L);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_D);
// hello world 
    }
}   
Enter fullscreen mode Exit fullscreen mode

Footnotes:

  1. Key Event Class
  2. java.awt
  3. IOException
  4. Runtime Class

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo πŸ“Šβœ¨

Top comments (0)

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay