2012-08-23 98 views
1

我正在創建一個簡單的控制檯應用程序,其中我可以使用鍵盤箭頭鍵作爲輸入,就像玩具的典型遙控器一樣。當我向上按下控制檯時,控制檯將打印輸出文本「UP」,或者如果我按下箭頭鍵,它將打印「向下」。Java控制檯鍵盤 - 自動接受輸入

我只想按一次箭頭鍵,即我不需要按回車接受我的輸入。我想要按下箭頭鍵自動接受輸入。

我已經嘗試了一些代碼,但這仍然沒有發生,我仍然需要按回車接受我的輸入。如果你有任何想法,我可以儘可能簡單地實現這一點,我真的很感激。

+2

請顯示代碼。 – adarshr

+0

看看這個問題。 http://stackoverflow.com/questions/9545388/how-can-i-detect-arrow-keys-in-java-console-not-in-gui – AlexR

+0

你說你已經嘗試過一些東西。請張貼代碼片段,也可以嘗試發佈您嘗試過的內容。 –

回答

0

此示例代碼將幫助您獲得左箭頭鍵事件。你可以參考這個,

import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 

import javax.swing.*; 

public class Test2 extends JPanel { 
    private static final int PREF_W = 400; 
    private static final int PREF_H = PREF_W; 
    private static final int TIMER_DELAY = 50; 
    private Timer leftKeyTimer = new Timer(TIMER_DELAY , new TimerListener()); 


    public Test2() { 
     int condition = JComponent.WHEN_IN_FOCUSED_WINDOW; 
     InputMap inputMap = getInputMap(condition); 
     ActionMap actionMap = getActionMap(); 

     String leftDownKey = "Left Down"; 
     String leftUpKey = "Left Up"; 
     KeyStroke leftDown = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT , 0, false); 
     KeyStroke leftUp = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT , 0, true); 
     inputMap.put(leftDown, leftDownKey); 
     inputMap.put(leftUp, leftUpKey); 

     actionMap.put(leftDownKey, new LeftKeyAction(false)); 
     actionMap.put(leftUpKey, new LeftKeyAction(true)); 
     leftKeyTimer.setActionCommand("Left Key"); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(PREF_W, PREF_H); 
    } 

    private class LeftKeyAction extends AbstractAction { 
     private boolean onKeyRelease; 

     public LeftKeyAction(boolean onKeyRelease) { 
     this.onKeyRelease = onKeyRelease; 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
     if (onKeyRelease) { 
      if (leftKeyTimer != null && leftKeyTimer.isRunning()) { 
       leftKeyTimer.stop(); 
      } 
     } else { 
      if (leftKeyTimer != null && !leftKeyTimer.isRunning()) { 
       leftKeyTimer.start(); 
      } 

     } 
     } 
    } 

    private class TimerListener implements ActionListener { 
     public void actionPerformed(ActionEvent actEvt) { 
     System.out.println(actEvt.getActionCommand()); 
     } 
    } 

    private static void createAndShowGui() { 
     Test2 mainPanel = new Test2(); 

     JFrame frame = new JFrame("Test2"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 
0

這實際上是一個令人驚訝的複雜的問題。 如果你想要一個真正的控制檯應用程序(沒有GUI元素),你必須犧牲可移植性。

大多數控制檯默認都是行緩衝的,所以Java在輸入按下之前不會得到任何輸入。大多數可以切換到字符模式,但沒有操作系統獨立的方式來做到這一點。欲瞭解更多信息,請參閱http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/