问题
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
回答1:
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
回答2:
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;
}
}
来源:https://stackoverflow.com/questions/1190813/flex-button-acting-like-a-backspace