我正在开发一个Adobe AIR应用程序。我已经为我的项目中的所有按钮实现了单击事件。现在我想为聚焦按钮添加“ENTER”键盘事件。有没有简单的方法来实现它?请指教我..
答案 0 :(得分:5)
你可以做这样的事情:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
addedToStage="addEnterFeature()">
<fx:Script>
<![CDATA[
private function addEnterFeature() : void
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
myButton.addEventListener(MouseEvent.CLICK, onButtonClick);
}
private function onKeyDown(e : KeyboardEvent) : void
{
if(e.keyCode == Keyboard.ENTER && stage.focus is Button)
// Simulate a mouse click on current focused button
Button(stage.focus).dispatchEvent(new MouseEvent(MouseEvent.CLICK));
}
private function onButtonClick(e : Event) : void
{
trace("Button click handler");
}
]]>
</fx:Script>
<s:Button label="Test" id="myButton" />
</s:WindowedApplication>
如果您在应用中的每个按钮(具有焦点)上使用Enter键,它将模拟鼠标单击