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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:19:24+00:00 2026-06-07T10:19:24+00:00

While trying to create a simple game where a square is manipulated via the

  • 0

While trying to create a simple game where a square is manipulated via the keyboard keys, I have come across a small, rather irritating problem. I would like it to work so that when the opposite directional key is pressed, the character will stop; the character may move the other two directions while stopped in this situation.

This works perfectly with LEFT and RIGHT held down; the player may move UP or DOWN. If UP and DOWN are held down, however, the player will not move, nor will Java recognize that the LEFT or RIGHT keys were pressed.

import java.util.ArrayList;
import java.util.Random;

import org.lwjgl.*;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;

import static org.lwjgl.opengl.GL11.*;

public class Main {
    private Man p;
    private ArrayList<Integer> keysDown, keysUp;
    public Main() {
        try {
            Display.setDisplayMode(new DisplayMode(640, 480));
            Display.setTitle("LWJGLHelloWorld");
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
        }

        p = new Man(0, 0);
        keysDown = new ArrayList<>();
        keysUp = new ArrayList<>();

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 640, 480, 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);

        while (!Display.isCloseRequested()) {
            glClear(GL_COLOR_BUFFER_BIT);

            checkKeys();

            p.draw();

            Display.update();
            Display.sync(60);
        }

        Display.destroy();
    }

    public void checkKeys() {
        ArrayList<Integer> keys = new ArrayList<>();
        keys.add(Keyboard.KEY_A);
        keys.add(Keyboard.KEY_D);
        keys.add(Keyboard.KEY_W);
        keys.add(Keyboard.KEY_S);

        for (int key : keys) {
            if (Keyboard.isKeyDown(key)) 
                keysDown.add(key);
            else 
                keysUp.add(key);
        }

        keysDown.removeAll(keysUp);
        keysUp = new ArrayList<>();

        int speed = 4;
        int dx = 0;
        int dy = 0;
        if (keysDown.contains(keys.get(2))) {
            System.out.println("keyUP");
            dy -= speed;
        }
        if (keysDown.contains(keys.get(3))) {
            System.out.println("keyDOWN");
            dy += speed;
        }
        if (keysDown.contains(keys.get(0))) {
            System.out.println("keyLEFT");
            dx -= speed;
        }
        if (keysDown.contains(keys.get(1))) {
            System.out.println("keyRIGHT");
            dx += speed;
        }
        //if (keysDown.contains(keys.get(0)) && keysDown.contains(keys.get(1))) dx = 0;

        //if (keysDown.contains(keys.get(2)) && keysDown.contains(keys.get(3))) dy = 0;
        p.update(dx, dy);

    }

    public static void main(String[] args) {
        new Main();
    }

    class Man {
        public int x, y, w, h;
        public float cR, cG, cB;

        public Man(int x, int y) {
            this.x = x;
            this.y = y;
            w = 50;
            h = 50;

            Random rand = new Random();
            cR = rand.nextFloat();
            cG = rand.nextFloat();
            cB = rand.nextFloat();
        }

        public void draw() {
            glColor3f(cR, cG, cB);
            glRecti(x,  y,  x+w,  y+h);
        }

        public void update(int dx, int dy) {
            x += dx;
            y += dy;
        }
    }
}

That is the code that I am working with. In addition, I am unsure how to compile an executable jar that is using the lwjgl library in addition to slick-util.

EDIT:
I forgot to mention that with the current set-up: ASDW, it has problems. I also experienced these issues with JKLI and LEFT RIGHT UP DOWN. If you change it to keys in a line (ex: ASDF) it works as expected.

  • 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-07T10:19:26+00:00Added an answer on June 7, 2026 at 10:19 am

    You’re running in to a hardware limitation of your keyboard, which is described in the answer here.

    You may want to try using another keyboard, a ps/2 one if possible because they allow full n-key rollover.

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

Sidebar

Related Questions

I am getting strange errors while trying to create a simple database using isql
I have ran into an interesting issue while trying to create a more usable
So I'm trying to create a simple multi-threaded game engine for the game I
I am trying to make a simple game that goes across a TCP network.
As a small exercise I am trying to write a very small, simple game
I'm trying to create a simple display of progress for the user while actions
I find my self lost while trying to create an actually pretty easy SQL
I am having a trouble while trying to create an entity with a custom
How can I replace storyboard with XIB? While trying to create dynamically views and
I'm trying to create/update a location while saving a checkin in my Rails project,

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.