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

The Archive Base Latest Questions

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

I’m writing a simple program in Java which includes a KeyListener with the following

  • 0

I’m writing a simple program in Java which includes a KeyListener with the following overriding they KeyTyped method:

@Override
        public void keyTyped(KeyEvent e)
        {
            int key = e.getKeyCode();
            System.out.println("TEST");

            if (key == KeyEvent.VK_KP_LEFT || key == KeyEvent.VK_LEFT)
            {
                System.out.println("LEFT");
                //Call some function
            }
            else if (key == KeyEvent.VK_KP_RIGHT || key == KeyEvent.VK_RIGHT)
            {
                System.out.println("RIGHT");
                //Call some function
            }
        }

When I type anything other than the arrow keys (e.g. “a”), it prints TEST as it should. However, when I type a numpad arrowkey, it only prints TEST and when I type a standard arrow key it doesn’t print anything at all. Is this possibly because I’m on a laptop, or have I just made a silly mistake somewhere?

  • 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-28T16:22:52+00:00Added an answer on May 28, 2026 at 4:22 pm

    Yep, you’ll see the arrow keys respond to keyPressed and keyReleased, not keyTyped. My SSCCE:

    import java.awt.Dimension;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import javax.swing.*;
    
    public class ArrowTest extends JPanel {
       private static final int PREF_W = 400;
       private static final int PREF_H = PREF_W;
    
       public ArrowTest() {
          setFocusable(true);
          requestFocusInWindow();
    
          addKeyListener(new KeyAdapter() {
    
             @Override
             public void keyTyped(KeyEvent e) {
                myKeyEvt(e, "keyTyped");
             }
    
             @Override
             public void keyReleased(KeyEvent e) {
                myKeyEvt(e, "keyReleased");
             }
    
             @Override
             public void keyPressed(KeyEvent e) {
                myKeyEvt(e, "keyPressed");
             }
    
             private void myKeyEvt(KeyEvent e, String text) {
                int key = e.getKeyCode();
                System.out.println("TEST");
    
                if (key == KeyEvent.VK_KP_LEFT || key == KeyEvent.VK_LEFT)
                {
                    System.out.println(text + " LEFT");
                    //Call some function
                }
                else if (key == KeyEvent.VK_KP_RIGHT || key == KeyEvent.VK_RIGHT)
                {
                    System.out.println(text + " RIGHT");
                    //Call some function
                }
             }
    
    
          });
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       private static void createAndShowGui() {
          ArrowTest mainPanel = new ArrowTest();
    
          JFrame frame = new JFrame("ArrowTest");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    

    So to solve this, override keyPressed rather than keyTyped if you want to listen to arrow events.

    Or for an even better solution: use Key Bindings

    Edit
    My Key Bindings version:

    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class ArrowTest extends JPanel {
       private static final int PREF_W = 400;
       private static final int PREF_H = PREF_W;
    
       public ArrowTest() {
          ActionMap actionMap = getActionMap();
          int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
          InputMap inputMap = getInputMap(condition);
    
          for (Direction direction : Direction.values()) {
             inputMap.put(direction.getKeyStroke(), direction.getText());
             actionMap.put(direction.getText(), new MyArrowBinding(direction.getText()));
          }
       }
    
       private class MyArrowBinding extends AbstractAction {
          public MyArrowBinding(String text) {
             super(text);
             putValue(ACTION_COMMAND_KEY, text);
          }
    
          @Override
          public void actionPerformed(ActionEvent e) {
             String actionCommand = e.getActionCommand();
             System.out.println("Key Binding: " + actionCommand);
          }
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       private static void createAndShowGui() {
          ArrowTest mainPanel = new ArrowTest();
    
          JFrame frame = new JFrame("ArrowTest");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    enum Direction {
       UP("Up", KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0)),
       DOWN("Down", KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0)),
       LEFT("Left", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0)),
       RIGHT("Right", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0));
    
       Direction(String text, KeyStroke keyStroke) {
          this.text = text;
          this.keyStroke = keyStroke;
       }
       private String text;
       private KeyStroke keyStroke;
    
       public String getText() {
          return text;
       }
    
       public KeyStroke getKeyStroke() {
          return keyStroke;
       }
    
       @Override
       public String toString() {
          return text;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.