Tom
Tom

Reputation: 6707

TextField() - filter backspace key away with event listener

How to prevent a specific key such as backspace key functioning from TextField editable field, preventDefault does not seem to work:

public function handleEvents(evt:KeyboardEvent):void {
 if (evt.type == KeyboardEvent.KEY_UP) {
 if (evt.keyCode==8){
   evt.preventDefault () ;
  }
}

Upvotes: 2

Views: 1878

Answers (3)

tim
tim

Reputation: 11

What I have done is set focus to another temporary (and off-stage) text field and in my keydown handler return focus to the my text field for the keys I want to get through. Setting focus just for keys you want to filter out also works.

Upvotes: 1

grapefrukt
grapefrukt

Reputation: 27045

I recommend listening for the KEY_DOWN event if anything, but likely that won't work either. IIRC these type of events are a bit special and you can't really stop them. What I suspect you need to do is to store a copy of the text and whenever you detect a change you don't like just set it back to your stored version.

Upvotes: 2

Goran
Goran

Reputation: 6846

Try adding evt.stopImmediatePropagation()

Upvotes: 0

Related Questions