KeyStroke/KeyPress in Java

Submitted by: 
Language: 
Visitors have accessed this post 676 times.

This tutorial will teach you how to create a program in java that has a KeyPress or KeyStroke.

So, now let's start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of keyStroke.java.

2. Import the following package library:

  1. import java.awt.event.*; // used to access the ActionEvent clas
  2. import javax.swing.*; //used to access the AbstractAction,Action,ActionMap,InputMap,JButton,JComponent,JFrame, and KeyStroke

3. We will initialize variables in our Main, variable frame as JFrame, button as JButton, and ACTION_KEY as String.

  1. String ACTION_KEY = "theAction";
  2. JFrame frame = new JFrame("KeyStroke Sample");
  3. JButton button = new JButton("Press the SpaceBar");

4. Create an ActionEvent to your button and will print "The spacebar button is pressed!". Have this code below:

  1. Action actionListener = new AbstractAction() {
  2. public void actionPerformed(ActionEvent actionEvent) {
  3. JButton source = (JButton) actionEvent.getSource();
  4. System.out.println("The spacebar button is pressed!");
  5. }
  6. };

5. To have a space keytroke used the KeyStroke class with the getKeyStroke class.

  1. KeyStroke space = KeyStroke.getKeyStroke(' ');

To provide an action on the button when pressing the spacebar, have this code below:

  1. InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
  2. inputMap.put(space, ACTION_KEY);
  3. ActionMap actionMap = button.getActionMap();
  4. actionMap.put(ACTION_KEY, actionListener);
  5. button.setActionMap(actionMap);

6. Lastly, add the button, set the size, visibility, and the close operation of the frame. Have this code below:

  1. frame.getContentPane().add(button);
  2. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  3. frame.setSize(400, 200);
  4. frame.setVisible(true);

Output:

output
Here's the full code of this tutorial:

  1. import java.awt.event.*; // used to access the ActionEvent clas
  2. import javax.swing.*; //used to access the AbstractAction,Action,ActionMap,InputMap,JButton,JComponent,JFrame, and KeyStroke
  3.  
  4. public class keyStroke {
  5. public static void main(String[] a) {
  6.  
  7. String ACTION_KEY = "theAction";
  8. JFrame frame = new JFrame("KeyStroke Sample");
  9. JButton button = new JButton("Press the SpaceBar");
  10.  
  11. Action actionListener = new AbstractAction() {
  12. public void actionPerformed(ActionEvent actionEvent) {
  13. JButton source = (JButton) actionEvent.getSource();
  14. System.out.println("The spacebar button is pressed!");
  15. }
  16. };
  17.  
  18. KeyStroke space = KeyStroke.getKeyStroke(' ');
  19. InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
  20. inputMap.put(space, ACTION_KEY);
  21. ActionMap actionMap = button.getActionMap();
  22. actionMap.put(ACTION_KEY, actionListener);
  23. button.setActionMap(actionMap);
  24. frame.getContentPane().add(button);
  25. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  26. frame.setSize(400, 200);
  27. frame.setVisible(true);
  28. }
  29. }

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.