Archive for April, 2009

Tutorial: Disabling Enter Key in TextArea

Objective

  • Learn how to disable the Enter key from working in a TextArea component.
  • View Demo

Files Required

Let’s Get Started!

Now I know what you are probably thinking; why would I want to disable the Enter key in a TextArea? Well I found one use case: The information that you want your user to enter can be quite lengthy and you want to display everything that the user had typed, but TextArea allows the Enter key to work. You wish it would operate like a TextInput component.

Let’s start by examining the init() function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * Initialise
 */
private function init ():void 
{
    // create the text area
    ta = new TextArea();
    ta.move(Main.PADDING, Main.PADDING);
    ta.setSize(stage.stageWidth - (Main.PADDING * 2), stage.stageHeight - (Main.PADDING * 2));
    addChild(ta);
    //
    // Add the event listeners
    ta.textField.addEventListener(TextEvent.TEXT_INPUT, ta_textInputHandler, false, 0, true);
}

After creating the TextArea, we have to listen to the TextEvent.TEXT_INPUT event to track whatever the user had just typed.

IMPORTANT NOTE: We listen to the event from the textField property of the TextArea instance, and not the TextArea instance itself. If you do otherwise, it will not work.

1
2
3
4
5
6
7
8
private function ta_textInputHandler (event:TextEvent):void
{
    // The char code for Enter is 10
    if (event.text.charCodeAt() == 10) 
    {
        event.preventDefault();
    }
}

In the event handler, we will track the text that is trying to get itself added to the textField. When the conditional is true, we simply prevent it from happening by using the preventDefault() method of event argument.

Final Note

For more information regarding preventDefault(), you can read the documentation. :)

AddThis Social Bookmark Button

Cheat Sheets For Developers

First off I need to apologise for being out of zone for the past months. Got overwhelmed at work, had to make some major decisions in life, and when I got some time to myself, I just needed the break. :P

Well I want to share something very important today, something that is very close to the hearts of all developers. Developers these days need to have different skill sets, and need to know them well. With the amount that we need to know, it’s not unusual if we forgot some API from some languages.

Enter cheat sheets. :D

Imagine how I felt when I found a site that are providing quality cheat sheets that are written by bestselling authors and leading experts. *whoohoo~!*

The information contained in these cheat sheets are reliable and relevant for today’s developers, giving us the edge that we all need on a tight schedule. I don’t think I have to sell them to you anymore; I bet you already know that they are good resources. ;)

Head over to RefCardz, get yourself registered with them, and finally grab their RSS feed.

Disclaimer: I’m not getting anything out of this from DZone. I simply think it is a great thing that they are doing for the community and thus, I do my part to spread the love. :)

AddThis Social Bookmark Button