TextField() - 使用事件监听器过滤掉退格键
如何防止 TextField 可编辑字段中的特定键(例如退格键)起作用,preventDefault 似乎不起作用:
public function handleEvents(evt:KeyboardEvent):void {
if (evt.type == KeyboardEvent.KEY_UP) {
if (evt.keyCode==8){
evt.preventDefault () ;
}
}
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 () ;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议监听 KEY_DOWN 事件(如果有的话),但这可能也不起作用。 IIRC 这类事件有点特殊,你无法真正阻止它们。
我怀疑您需要做的是存储文本的副本,每当您检测到您不喜欢的更改时,只需将其设置回您存储的版本即可。
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.
我所做的是将焦点设置到另一个临时(和后台)文本字段,并在我的 keydown 处理程序中将焦点返回到我想要通过的键的文本字段。 仅为您想要过滤掉的键设置焦点也可以。
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.
尝试添加 evt.stopImmediatePropagation()
Try adding evt.stopImmediatePropagation()