捕获键盘事件CTRL + S键盘、事件、CTRL

2023-09-08 14:36:22 作者:谁伴我闯荡

我需要触发一个事件由pressing CTRL + S。

I need to 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.

推荐答案

这是关键code 83 S(的情况下,如果你犯那个错误)。此外,请确保您的KEY_UP事件添加监听器。以下是一个例子:

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>
 
精彩推荐
图片推荐