Reputation: 1197
I have a TextArea with undo/redo which is performed via ctrl+z, ctrl+y. My app is a mobile app with no cntrl on the soft keyboard so I want to make an undo and a redo button which simulates the keypresses. How do you simulate keys in Flex?
Upvotes: 0
Views: 1237
Reputation: 8834
You can dispatch any event you like including KeyboardEvents so to dispatch a KeyboardEvent that simulates ctrl-z (keycode 26) and ctrl-y (keycode 25) you could do something like the following:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600" creationComplete="creationCompleteHandler(event)">
<mx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function creationCompleteHandler(event:FlexEvent):void
{
this.addEventListener( KeyboardEvent.KEY_UP, keyHandler );
}
protected function clickHandler(event:MouseEvent):void
{
this.dispatchEvent( new KeyboardEvent( KeyboardEvent.KEY_UP, true, false, 26, 26, 0, true ) );
}
protected function keyHandler( e:KeyboardEvent ):void
{
trace(e.charCode, e.keyCode, e.ctrlKey);
}
]]>
</mx:Script>
<mx:Button id="undoButton" label="undo" click="clickHandler(event)" />
</mx:Application>
The line this.dispatchEvent( new KeyboardEvent( KeyboardEvent.KEY_UP, true, false, 26, 26, 0, true ) );
is the key (no pun intended) it says dispatch a KEY_UP event, the next two args are bubbles and cancelable, then 26 for both charCode and keyCode, 0 for keyLocation, and finally true for ctrlKey (to simulate it being down).
Upvotes: 1