I need to开发者_如何学C fire an event by pressing CTRL+S.
I tried this but it won't work:
if(e.ctrlKey == true && e.keyCode == 81){
trace("CTRL+S")
}
How can it be done? Thanks for tips.
It is keyCode 83 for S (in case if you are making that mistake). Moreover, make sure you add your listener on KEY_UP event. Here is a working sample:
<?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">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:Panel title="Panel 3" width="100%" height="200" creationComplete="onCreationComplete()">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Script>
<![CDATA[
private function onCreationComplete():void{
this.addEventListener(MouseEvent.CLICK, clickHandler);
this.addEventListener(KeyboardEvent.KEY_UP,keyPressed);
}
private function clickHandler(event:MouseEvent):void {
stage.focus = this;
}
private function keyPressed(evt:KeyboardEvent):void{
if(evt.ctrlKey && evt.keyCode == 65)
trace("CTRL A is pressed");
if(evt.ctrlKey && evt.keyCode == 66)
trace("CTRL B is pressed");
if(evt.ctrlKey && evt.keyCode == 81)
trace("CTRL S is pressed");
}
]]>
</fx:Script>
</s:Panel>
</s:WindowedApplication>
M.D.'s answer looks very good; it shows how to add event listeners for the keyboard events.
But one thing he didn't mention: if you are running this as a Flash SWF in a browser, the CTRL+S shortcut may be intercepted and consumed by the browser and the Flash Player may never get it. CTRL+S is a pretty common shortcut for saving a page, so don't be surprised if you run into browser focus issues.
精彩评论