views:

344

answers:

2

Hi,

How would you make a button event call a function which acts like a backspace keyboard event delete.

I tried faking the dispatch keyboard event "keyup" "keydown" with keycode 8 and keynumber 8 without success.

No other way than doing it by hand with begin and end select index plus substr ?

I have a textinput i just want to add a button acting like a backspace.

Thanks

A: 

You can use the ASCII code 8 and translate it to a char or use the escape character '\b'

or you can manipulate the textFieldInstance object:

textFieldInstance.text = textFieldInstance.text.substr( 0, -1 );

see:

http://board.flashkit.com/board/showthread.php?t=246003

http://www.java2s.com/Code/Flash-Flex-ActionScript/String/InsertingSpecialWhitespaceCharactersBackspaceb.htm

http://www.wipeout44.com/brain_food/flash_actionscript_goodies.asp

http://www.ultrashock.com/forums/actionscript/deleting-chars-in-a-text-field-e-g-backspace-124649.html

e5
what if i have selected a 5char long word and try to play a backspace ? .text.substr( 0, -1 ); wont work.
coulix
should not simulating the user entering \b delete the selected word?
e5
A: 

You could try this:

function delSelected(textFieldInstance:TextField):void {
  var bIndex:int = textFieldInstance.selectionBeginIndex;
  var eIndex:int = textFieldInstance.selectionEndIndex;
  if (bIndex == eIndex) {
    textFieldInstance.text = textFieldInstance.text.substr(0,-1);
  } else {
    var a:String = textFieldInstance.text.substr(0,bIndex);
    var b:String = textFieldInstance.text.substr(eIndex);
    textFieldInstance.text = a+b;
  }
}
Phil Hayward