简体   繁体   中英

Java- Check for key presses not using KeyListener

I'm making a prototype for a videogame using Java (I plan on porting it to Unity later, but since I'm pretty comfortable with Java, I figure it'll be quicker to get an idea for the structure and basic components of my game in Java). Perhaps ironically, however, I have some doubts about how to do this thing in Java.

What is the best way to collect key presses? I know about KeyListeners and how to use them, but they don't seem ideal. I would like to be able to call a keyPress() method once every update cycle (at the beginning of the cycle, specifically) for precision reasons–I have information being updated each cycle and I want it to vary depending on which key is pressed–I realize that being even a couple of frames asynchronous isn't a huge deal, but immediacy of response is pretty important in making the game play well, I feel. I also don't want to be individually alerted every time a different key is pressed, but ideally, I'd like to generate a kind of list of which keys are pressed at any given moment (the game input consists of specific keystrokes, and so at any moment, I want to know what the active keystroke is). There's also other issues, regarding the timings when game events happen that make being synchronized to within 3 or 4 frames pretty important. It is less important that each cycle run fast than it is that I get the response to keys at the beginning of the cycle.

Is there way to do this? If there is, is it a good idea? Would it be too slow to expect any kind of consistent gameplay if I run this every update cycle (I'd expect it might even be faster or at least lighter than a listener)? Are there other places where this could wrong?

Thanks

first you should look at your gameplay. The use of events will not problem for you. And code disorder and event control make it difficult. There are many solutions to this. At first, did you used any frameworks (like the Libgdx)? if yes. this is handle, Otherwise you can go yourself.


One way to use "Message Handling". in this solution,You can write the code below.

public interface Telegram {
    public boolean handleMessage(final Telegram msg,Object yourObject );
}

public class MessageHandler {
    private static Map<String,List<Telegram>> telegramKeyMap = new HashMap<>();

    public static void addTelegram(String key,Telegram telegram){
        List<Telegram> telegramList = telegramKeyMap.getOrDefault(key, new ArrayList<Telegram>());
        telegramList.add(telegram);
    }

    public static void dispatchMessage(String key,Object addVal){
        List<Telegram> telegramList = telegramKeyMap.getOrDefault(key, new ArrayList<Telegram>());

        for (Telegram telegram : telegramList) {
            telegram.handleMessage(telegram,addVal);
        }
    }
}

public class Player implements Telegram {
    public Player() {

    }

    @Override
    public boolean handleMessage(Telegram msg, Object yourObject) {
        return false;
    }
}

in this code you can implement Telegram for any classes. then on the listeners call dispatchMessage. for input you can used any Objects(like Listener, keyCode,...) dispatcher fine any object with this key, them call handleMessage method. Now, each of your objects can handle their work.

second solution : Use "Event Management"
If the first option does not resolve your problem, then I'll take the second solution to the sample code. The message handler is used in most game engine engines, and it does increase the speed of the game, but it's opening up your hands sharply.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM