Actionscript Tips for Chat Clients

The Autoscrolling Chat Window: Most chat clients automatically scroll the content of the chat window when a new message arrives. This ensures that all of the new messages are immediately viewable. It would be nice if this feature was incorporated into your Flash client. I have a simple solution that uses the Flash MX textfield's onScroller event handler. Here is the code:

// This will automatically scroll the chat text
// down when new text arrives
chatText.onScroller = function () {
	while( chatText.scroll < chatText.maxscroll ) {
		chatText.scroll++;
	}
}

Send a Message with the Enter Key: Rather than having to use the mouse to click a button every time you want to send a message, it would be much easier to just hit Enter and be done with it. Fortunately, this is relatively easy to do using the new Key object and listeners. Here is the code:

// Listens for ENTER key to be pressed
enterListener = new Object();

// Called when a key is released enterListener.onKeyUp = function () { if( Key.getCode() == Key.ENTER ) { myJabber.sendMessageToRoom( inputText.text ); // Clear the text to make room for the next message inputText.text = ""; } }

// This ensures that we only listen for the ENTER // key when we are typing in the input box inputText.onSetFocus = function(oldFocus) { Key.addListener(enterListener); }

inputText.onKillFocus = function(newFocus) { Key.removeListener(enterListener); }