问题
I am working on an AS3 Flash game that initially takes a input from a barcode scanner. The data that it scans comes in as one long string using tabs to separate the data segments. The scanner acts as a keyboard and inputs the string into a hidden textfield so that I can grab the string and split it apart to get the data.
The input and everything works great. The issue that I am running into is that when the the textfield receives a tab character, instead of inserting the character into the textfield it highlights whatever is in the textfield. Then the next set of characters overwrite what was already in the textfield.
Is there any way around this? Is there some way to make the textfield accept the tab as a literal character? I cannot change the way the barcode delimits the data in the string.
Thanks for any help you can give.
回答1:
(Updated solution)
That is indeed preventabe. You can stop it with following text (assuming Text is the textfield).
Text.addEventListener(FocusEvent.KEY_FOCUS_CHANGE, TextKeyFocusChange);
private function TextKeyFocusChange(e:FocusEvent):void
{
e.preventDefault();
var txt:TextField = TextField(e.currentTarget);
txt.appendText("\t");
txt.setSelection(txt.length, txt.length);
}
回答2:
The following lets the user type a tab anywhere in the TextField (not just the end.)
someTextField.addEventListener(FocusEvent.KEY_FOCUS_CHANGE, TextKeyFocusChange);
private function TextKeyFocusChange(e:FocusEvent):void
{
e.preventDefault();
var txt:TextField = TextField(e.currentTarget);
txt.replaceText(txt.caretIndex, txt.caretIndex, "\t");
txt.setSelection(txt.caretIndex + 1, txt.caretIndex + 1);
}
Note: This solution is a little slower than using appendText and should only be used if tabs can be entered anywhere in the TextField, not just the end.
来源:https://stackoverflow.com/questions/690816/how-can-i-permit-tab-characters-in-a-textfield-in-flash