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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:03:08+00:00 2026-06-04T23:03:08+00:00

I am currently trying to implement a keylistener in my program so that it

  • 0

I am currently trying to implement a keylistener in my program so that it does an action when I pressed an arrow key, the object in my program either moves left or right.

Here is the moving method in my program

public void moveDirection(KeyEvent e)
    {
        int move = 0;
        int r = K.getRow();
        int c = K.getCol();
        if (e.getKeyCode() == 39) move = 1; //KeyEvent.VK_RIGHT
        if (e.getKeyCode() == 37) move = 2; //KeyEvent.VK_LEFT
        //if (e.getKeyCode() == KeyEvent.VK_DOWN) move = 3;

        switch (move)
        {
            case 1: if (inBound(r, c+1))
                        K.setLocation(r ,c+1); 
                    if (inBound(r, c-1) && frame2[r][c-1] == K)
                        frame2[K.getRow()][K.getCol()-1] = null; 
                    break; //move right 39
            case 2: K.setLocation(K.getRow(), K.getCol()-1); break; //move left 37
            //case 3: b.setLocation(b.getRow()+1, b.getCol()); break; //move down
            default: return;
        }        
        processBlockList();
    }

I am wondering how the program is supposed to read in (KeyEvent) e. I cannot really type in an arrowkey….

Please help!

edit: I also need to know what I need to add to my code so that my program waits about 700 milliseconds for a keyinput before moving on to another method

  • 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-06-04T23:03:11+00:00Added an answer on June 4, 2026 at 11:03 pm

    http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html
    Check this tutorial

    If it’s a UI based application , then ” I also need to know what I need to add to my code so that my program waits about 700 milliseconds for a keyinput before moving on to another method” you can use GlassPane or Timer class to fulfill the requirement.

    For key Event:

    public void keyPressed(KeyEvent e) {
    
        int key = e.getKeyCode();
    
        if (key == KeyEvent.VK_LEFT) {
            dx = -1;
        }
    
        if (key == KeyEvent.VK_RIGHT) {
            dx = 1;
        }
    
        if (key == KeyEvent.VK_UP) {
            dy = -1;
        }
    
        if (key == KeyEvent.VK_DOWN) {
            dy = 1;
        }
    }
    

    check this game example http://zetcode.com/tutorials/javagamestutorial/movingsprites/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to implement a socket server that enables the clients to send
I'm currently trying to implement a Table View on my iPhone app that grabs
I'm currently trying to implement a UITableView with a delegate and data source that
I'm currently trying to implement a feature in my app that shows tags for
I'm currently trying to implement something that combines reverse engineering and graph theory. Therefore
Hey guys, I'm currently trying to implement a function using C that takes in
I'm currently trying to implement an ExtendedListView that uses an Adapter. I'm implementing the
I am currently trying to implement the built-in Silverlight 3 validation against objects that
I am currently trying implement a QThread that contains a socket connection. The socket
Currently trying to implement a simple ping program to teach myself about network programming

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.