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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:37:34+00:00 2026-05-31T15:37:34+00:00

The arrow keys for this application need to cause press and release events independent

  • 0

The arrow keys for this application need to cause press and release events independent of focus.
Setting onKeyRelease true causes a release event as expected but setting onKeyRelease false (code below) doesn’t seem to stop auto-repeat.
Is there a way to implement the key binding to trigger once when the arrow key is pressed and held?

Action right = new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        ...
    }
};
mainPanel.getInputMap(JPanel.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
        .put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "right");
mainPanel.getActionMap().put("right", right);
  • 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-31T15:37:35+00:00Added an answer on May 31, 2026 at 3:37 pm

    … could be good question, but please not clear everything, can you please elaborate …

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.*;
    import java.util.HashMap;
    import java.util.Map;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class MoveIcon extends JPanel {
    
        private static final long serialVersionUID = 1L;
        private static final String IMAGE_PATH = "http://duke.kenai.com/misc/Bullfight.jpg";
        private static final String IMAGE_PATH_PLAYER = "http://duke.kenai.com/iconSized/duke4.gif";
        public static final int STEP = 3;
        private static final int TIMER_DELAY = STEP * 8;
        private BufferedImage bkgrndImage = null;
        private BufferedImage playerImage = null;
        private Map<Direction, Boolean> directionMap = new HashMap<Direction, Boolean>();
        private int playerX = 0;
        private int playerY = 0;
    
        enum Direction {
    
            UP(KeyEvent.VK_UP, 0, -1), DOWN(KeyEvent.VK_DOWN, 0, 1),
            LEFT(KeyEvent.VK_LEFT, -1, 0), RIGHT(KeyEvent.VK_RIGHT, 1, 0);
            private int keyCode;
            private int xDirection;
            private int yDirection;
    
            private Direction(int keyCode, int xDirection, int yDirection) {
                this.keyCode = keyCode;
                this.xDirection = xDirection;
                this.yDirection = yDirection;
            }
    
            public int getKeyCode() {
                return keyCode;
            }
    
            public int getXDirection() {
                return xDirection;
            }
    
            public int getYDirection() {
                return yDirection;
            }
        }
    
        public MoveIcon() {
            try {
                URL bkgrdImageURL = new URL(IMAGE_PATH);
                URL playerImageURL = new URL(IMAGE_PATH_PLAYER);
                bkgrndImage = ImageIO.read(bkgrdImageURL);
                playerImage = ImageIO.read(playerImageURL);
                setPreferredSize(new Dimension(bkgrndImage.getWidth(), bkgrndImage.getHeight()));
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            for (Direction direction : Direction.values()) {
                directionMap.put(direction, false);
            }
            setKeyBindings();
            Timer timer = new Timer(TIMER_DELAY, new TimerListener());
            timer.start();
        }
    
        private void setKeyBindings() {
            InputMap inMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
            ActionMap actMap = getActionMap();
            for (final Direction direction : Direction.values()) {
                KeyStroke pressed = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, false);
                KeyStroke released = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, true);
                inMap.put(pressed, direction.toString() + "pressed");
                inMap.put(released, direction.toString() + "released");
                actMap.put(direction.toString() + "pressed", new AbstractAction() {
    
                    private static final long serialVersionUID = 1L;
    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        directionMap.put(direction, true);
                    }
                });
                actMap.put(direction.toString() + "released", new AbstractAction() {
    
                    private static final long serialVersionUID = 1L;
    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        directionMap.put(direction, false);
                    }
                });
            }
    
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (bkgrndImage != null) {
                g.drawImage(bkgrndImage, 0, 0, null);
            }
            if (playerImage != null) {
                g.drawImage(playerImage, playerX, playerY, null);
            }
        }
    
        private class TimerListener implements ActionListener {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean moved = false;
                for (Direction direction : Direction.values()) {
                    if (directionMap.get(direction)) {
                        playerX += STEP * direction.getXDirection();
                        playerY += STEP * direction.getYDirection();
                        moved = true;
                    }
                }
                if (moved) {
                    int x = playerX - 2 * STEP;
                    int y = playerY - 2 * STEP;
                    int w = playerImage.getWidth() + 4 * STEP;
                    int h = playerImage.getHeight() + 4 * STEP;
                    MoveIcon.this.repaint(x, y, w, h); // !! repaint just the player
                }
            }
        }
    
        private static void createAndShowUI() {
            JFrame frame = new JFrame("MoveIcon");
            frame.getContentPane().add(new MoveIcon());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    createAndShowUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to stop using the arrow keys in vim. But instead of setting
I want to disable the navigation on press of UP and Down arrow keys
I use this script to navigate with keyboard arrow keys. But the problem is
This is my code for a simple arrow sequencing application. It generates a random
This is my code.I am trying to make the ball move using arrow keys.
basicly i need my page to respond to left and right arrow keys. so
How do I get the left and right arrow keys to work in this
How to navigate the image using Keyboard arrow keys in C#. My 1st form
Can I somehow disable ListView items navigation through keyboard (arrow keys), and leave navigation
I'm trying to detect keyboard input and if the arrow keys are pressed, set

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.