How do I check if the user is pressing a key?

前端 未结 3 1585
盖世英雄少女心
盖世英雄少女心 2020-11-22 16:30

In java I have a program that needs to check continuously if a user is pressing a key. So In psuedocode, somthing like

if (isPressing(\"w\"))
{
 //do somthin         


        
相关标签:
3条回答
  • 2020-11-22 16:58

    You have to implement KeyListener,take a look here: http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyListener.html

    More details on how to use it: http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html

    0 讨论(0)
  • 2020-11-22 17:00

    In java you don't check if a key is pressed, instead you listen to KeyEvents. The right way to achieve your goal is to register a KeyEventDispatcher, and implement it to maintain the state of the desired key:

    import java.awt.KeyEventDispatcher;
    import java.awt.KeyboardFocusManager;
    import java.awt.event.KeyEvent;
    
    public class IsKeyPressed {
        private static volatile boolean wPressed = false;
        public static boolean isWPressed() {
            synchronized (IsKeyPressed.class) {
                return wPressed;
            }
        }
    
        public static void main(String[] args) {
            KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
    
                @Override
                public boolean dispatchKeyEvent(KeyEvent ke) {
                    synchronized (IsKeyPressed.class) {
                        switch (ke.getID()) {
                        case KeyEvent.KEY_PRESSED:
                            if (ke.getKeyCode() == KeyEvent.VK_W) {
                                wPressed = true;
                            }
                            break;
    
                        case KeyEvent.KEY_RELEASED:
                            if (ke.getKeyCode() == KeyEvent.VK_W) {
                                wPressed = false;
                            }
                            break;
                        }
                        return false;
                    }
                }
            });
        }
    }
    

    Then you can always use:

    if (IsKeyPressed.isWPressed()) {
        // do your thing.
    }
    

    You can, of course, use same method to implement isPressing("<some key>") with a map of keys and their state wrapped inside IsKeyPressed.

    0 讨论(0)
  • 2020-11-22 17:13

    Try this:

    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    
    public class Main {
    
        public static void main(String[] argv) throws Exception {
    
        JTextField textField = new JTextField();
    
        textField.addKeyListener(new Keychecker());
    
        JFrame jframe = new JFrame();
    
        jframe.add(textField);
    
        jframe.setSize(400, 350);
    
        jframe.setVisible(true);
    
    }
    
    class Keychecker extends KeyAdapter {
    
        @Override
        public void keyPressed(KeyEvent event) {
    
            char ch = event.getKeyChar();
    
            System.out.println(event.getKeyChar());
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题