Skip to main content
Participant
December 20, 2009
Question

Can a script manually insert a delete (or backspace) into a text area?

  • December 20, 2009
  • 2 replies
  • 621 views

I'm new to action script, so this may have a simple answer, sorry if that's the case.

I have a little movie created with a simple purpose of trying to mimic a system like cell phones have for character input.

There are two text areas, one hidden off the screen that accepts the input from the user; a second text area in on screen and displays what the user had entered after some basic processing on the input.

The hidden text area is pretty basic with listener that is responsible for grabbing the input keys and then mangling them to be inserted into the displayed text area based on the cell phone text entry multi-tap rules. Example user press 2-2-2  and the hidden input accepts 222 and then appends 'C' to the display text area. That's working great, but I have a problem if the user wants to delete from the display text area. I can't find a way to insert a delete or backspace key into the display text area. I've tried using StringFromCharCode (8) // Key.BACKSPACE, but that doesn't work. That just displays one of those undefined glyphs. I figure that things like clear, and delete are handled somewhere higher in the input chain and that's where I'm having a problem.

I've looked, but I can't find a way to delete a single character from the text area. I was thinking if I could find the cursor location that I could just use some delete character method to get the job done, but I couldn't find how to do that.

Any ideas on how to pull off manual delete function in a text area?

This topic has been closed for replies.

2 replies

Inspiring
December 20, 2009

Do you actually want to insert a delete or do you actually want to do a delete? If you are seeing a strange character then you most likely have properly inserted the delete character into your string. I mean what do you expect a delete key to look like? My guess is that you expect it to perform its function.

Anyways my guess is that you need to listen in your code for the delete (and probably some other special characters).

var lo:Object=new Object()

lo.onKeyUp =function(){

switch ( Key.getCode()){

case Key.BACKSPACE :

trace ("do backspace stuff");

break;

case Key.ENTER :

trace ("do enter stuff");

break;

default :

trace("do regular stuff");

}

}

Key.addListener(lo)

lost_one2Author
Participant
December 20, 2009

Ned, I do really want to manually (by method of code) trigger a delete in a text area. The delete (or backspace) that I want added to the text area isn't directly entered by the user, it's the result of several key presses by the user. That's why I called it "manual" because it's done by the code, not by the user's direct input.

Think about how text entry works on most phones... if you want to enter a letter you have to cycle through all the letters on a single key until you get to the character desired. As I stated before, if you want a 'c' you need to press 2 button 3 times. This is basically what I'm trying to do, but I want to add the ability to delete a character too if the correct sequence in entered, for example a sequence of ### would equal a backspace.

Rothrock, I think you were thinking the same thing that a single key is used to do a backspace.

I was thinking that I could possibly insert key event into the event queue for the text area to process, but I wasn't sure about that.

Sorry, if this is confusing, I know it's not a straight forward design. I need onscreen and offscreen text areas with the onscreen one acting as a slave of the offscreen one.

The way the code currently works is hidden text area had a listner that responds to changes, and that handler processes the key entered and deletes that character from the hidden text area. After the full sequence of characters (say 2 button has been pressed 3 times) the handler then sends the resulting character into the display text area. This part of the code works great. I can add any character including some specials like tab and newline. However if I want the text area to delete a character I don't have a way to do that. That's why I tried to insert the backspace key (0x08 or ^H, for other oldschoolers), but using that as a char code or string from char code doesn't work.

I had also tried to create to set the focus to the display text area then create and dispatch a backspace keydown and keyup events to the visible text area and then return the focus back to the hidden textarea, but... no luck.

Ned Murphy
Legend
December 20, 2009

Whatever sequence of keys you decide on for representing a deletion shouldn't be any more difficult than what you are currently doing for recognizing other characters, though you probably need some first key that automatically triggers a flag that what's coming is not a character for text entry.  I'm assuming you have some form of timing out on entries since 2 2 2 could be a a a or a b or c.

Once you figure out how you want to configure detection of a deletion command, the action you take would be to rewrite the contents of the text area by taking its current text using string methods to modify it, such as extracting all but the last character as a substring and writing that into the text area in place of what's there.

Ned Murphy
Legend
December 20, 2009

Due to the title of your posting, I'm not really clear on what you're trying to accomplish... manual implies done by hand, not by code.  If it is to modify the text in the text area, then you can probably read a string from the text area and use string methods to operate on the string and then write it back into the text area, replacing what's there.