File tree Expand file tree Collapse file tree 1 file changed +12
-2
lines changed
Expand file tree Collapse file tree 1 file changed +12
-2
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ public class KeyHandler implements KeyListener {
1919 private PaintPanel paintPanel ;
2020 private boolean inCommandMode ;
2121 Interpreter interpreter ;
22+ private int curIndex ;
2223
2324
2425 JTextField textField ;
@@ -85,6 +86,7 @@ public void keyTyped(KeyEvent e) {
8586 this .commandHistoryIter .next ();
8687
8788 commandHistoryIter .add (textField .getText ());
89+ curIndex ++;
8890
8991 executeCommand (textField .getText ().substring (PROMPT .length ()));
9092 exitCommandMode ();
@@ -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 (curIndex == commandHistoryIter .previousIndex ()){
110+ commandHistoryIter .previous ();
111+ }
112+ curIndex --;
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 (curIndex == commandHistoryIter .nextIndex ()){
118+ commandHistoryIter .next ();
119+ }
120+ curIndex ++;
111121 this .textField .setText (commandHistoryIter .next ());
112122 }
113123 }
You can’t perform that action at this time.
0 commit comments