Allow the up and down arrow keys to traverse the command history, like we do in Java.

git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@667 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Ray Greenwell
2008-10-14 21:25:37 +00:00
parent c0a22fdabb
commit fcf091b5c1
+29
View File
@@ -59,6 +59,7 @@ public class ChatControl extends HBox
addChild(_txt = new ChatInput());
_txt.addEventListener(FlexEvent.ENTER, sendChat, false, 0, true);
_txt.addEventListener(KeyboardEvent.KEY_UP, handleKeyUp);
_but = new CommandButton(sendButtonLabel, sendChat);
addChild(_but);
@@ -140,6 +141,22 @@ public class ChatControl extends HBox
// if there was no error, clear the entry area in prep for the next entry event
_txt.text = "";
_histidx = -1;
}
protected function scrollHistory (next :Boolean) :void
{
var size :int = _chatDtr.getCommandHistorySize();
if ((_histidx == -1) || (_histidx == size)) {
_curLine = _txt.text;
_histidx = size;
}
_histidx = (next) ? Math.min(_histidx + 1, size)
: Math.max(_histidx - 1, 0);
var text :String = (_histidx == size) ? _curLine : _chatDtr.getCommandHistory(_histidx);
_txt.text = text;
_txt.setSelection(text.length, text.length);
}
/**
@@ -150,6 +167,7 @@ public class ChatControl extends HBox
if (event.type == Event.ADDED_TO_STAGE) {
// set up any already-configured text
_txt.text = _curLine;
_histidx = -1;
// request focus
callLater(_txt.setFocus);
@@ -161,6 +179,14 @@ public class ChatControl extends HBox
}
}
protected function handleKeyUp (event :KeyboardEvent) :void
{
switch (event.keyCode) {
case Keyboard.UP: scrollHistory(false); break;
case Keyboard.DOWN: scrollHistory(true); break;
}
}
/** Our client-side context. */
protected var _ctx :CrowdContext;
@@ -170,6 +196,9 @@ public class ChatControl extends HBox
/** The place where the user may enter chat. */
protected var _txt :ChatInput;
/** The current index in the chat command history. */
protected var _histidx :int = -1;
/** The button for sending chat. */
protected var _but :CommandButton;