@@ -19,6 +19,7 @@ public class KeyHandler implements KeyListener {
1919 private PaintPanel paintPanel ;
2020 private boolean inCommandMode ;
2121 Interpreter interpreter ;
22+ private boolean isCurrentDirectionNext ;
2223
2324
2425 JTextField textField ;
@@ -81,9 +82,10 @@ public void keyTyped(KeyEvent e) {
8182
8283 //record command
8384
84- while (commandHistoryIter .hasNext ())
85+ while (commandHistoryIter .hasNext ()){
8586 this .commandHistoryIter .next ();
86-
87+ }
88+ isCurrentDirectionNext = true ;
8789 commandHistoryIter .add (textField .getText ());
8890
8991 executeCommand (textField .getText ().substring (PROMPT .length ()));
@@ -102,12 +104,20 @@ public void keyPressed(KeyEvent e) {
102104 exitCommandMode ();
103105
104106 //if it is up arrow, go back in history
105- } else if (e .getKeyCode () == KeyEvent .VK_UP ){
107+ } else if (e .getKeyCode () == KeyEvent .VK_UP ){
106108 if (commandHistoryIter .hasPrevious ()){
109+ if (isCurrentDirectionNext ){
110+ commandHistoryIter .previous ();
111+ }
112+ isCurrentDirectionNext = false ;
107113 this .textField .setText (commandHistoryIter .previous ());
108114 }
109- } else if (e .getKeyCode () == KeyEvent .VK_DOWN ){
115+ } else if (e .getKeyCode () == KeyEvent .VK_DOWN ){
110116 if (commandHistoryIter .hasNext ()){
117+ if (!isCurrentDirectionNext ){
118+ commandHistoryIter .next ();
119+ }
120+ isCurrentDirectionNext = true ;
111121 this .textField .setText (commandHistoryIter .next ());
112122 }
113123 }
0 commit comments