The default behavior for textfield, when up arrow is pressed is, the cursor goes to the beginning or first character. I would like to disable this and add custom behavior when up arrow is pressed. I am able to add the custom behavior but I am not able to stop the default behavior.
textfield的默认行为,按下向上箭头时,光标将转到开头或第一个字符。我想禁用此功能,并在按下向上箭头时添加自定义行为。我能够添加自定义行为,但我无法停止默认行为。
Can some body please let me know how can I fix my issue.
有些人可以告诉我如何解决我的问题。
thank you in advance. firemonk.
先感谢您。 firemonk。
2 个解决方案
#1
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
private function onKeyUp(event:KeyboardEvent):void {
if ( event.keyCode == 38 ) {
event.preventDefault();
}
}
]]>
</mx:Script>
<mx:TextInput text="Try me!" keyDown="onKeyUp(event);" />
</mx:WindowedApplication>
38 is an ASCII code of the up arrow.
38是向上箭头的ASCII码。
#2
I have tried the example you suggested but it does not seem to work. I have printed event. cancelable and got false. I guess it is not possible to over write default behavior of up arrow for TextField.
我已经尝试了你建议的例子,但似乎没有用。我有印刷的活动。可取消并且变得虚假。我想不可能为TextField覆盖向上箭头的默认行为。
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
private function onKeyUp(event:KeyboardEvent):void {
if ( event.keyCode == 38 ) {
trace(event.cancelable);
event.preventDefault();
event.stopImmediatePropagation();
}
}
]]>
</mx:Script>
<mx:TextInput text="Try me!" keyDown="onKeyUp(event);" id="tf"/>
</mx:Application>
#1
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
private function onKeyUp(event:KeyboardEvent):void {
if ( event.keyCode == 38 ) {
event.preventDefault();
}
}
]]>
</mx:Script>
<mx:TextInput text="Try me!" keyDown="onKeyUp(event);" />
</mx:WindowedApplication>
38 is an ASCII code of the up arrow.
38是向上箭头的ASCII码。
#2
I have tried the example you suggested but it does not seem to work. I have printed event. cancelable and got false. I guess it is not possible to over write default behavior of up arrow for TextField.
我已经尝试了你建议的例子,但似乎没有用。我有印刷的活动。可取消并且变得虚假。我想不可能为TextField覆盖向上箭头的默认行为。
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
private function onKeyUp(event:KeyboardEvent):void {
if ( event.keyCode == 38 ) {
trace(event.cancelable);
event.preventDefault();
event.stopImmediatePropagation();
}
}
]]>
</mx:Script>
<mx:TextInput text="Try me!" keyDown="onKeyUp(event);" id="tf"/>
</mx:Application>