Action 脚本类中使用了 ctrl 和 space 键吗?

发布于 2024-10-25 23:10:09 字数 1543 浏览 4 评论 0原文

我的动作脚本类中有一个 switch 语句。我有一个 函数 onKeyDown(event: KeyboardEvent),其中包含一个 switch 语句。它控制不同的键。 但现在我想使用两个键 CTRLSPACE 键来处理 TAB 键执行的相同功能。 我想在我的 switch 语句中添加一个案例,首先我按 CTRL 键,然后按 SPACE 键,然后调用特定函数。

任何人都可以告诉我 switch 语句中如何使用两个键?

    private function onKeyDown(event: KeyboardEvent) : void{

        if (popUp.displayPopUp){
            switch (event.keyCode){
                case Keyboard.UP:
                case Keyboard.DOWN:
                case Keyboard.END:
                case Keyboard.HOME:
                case Keyboard.PAGE_UP:
                case Keyboard.PAGE_DOWN:
                    inputTxt.selectRange(text.length, text.length)
                    list.dispatchEvent(event)
                    break;
                case Keyboard.ENTER:
                    acceptCompletion();
                    break;
                case Keyboard.TAB:
                    if (requireSelection)
                        acceptCompletion();
                    else
                        popUp.displayPopUp = false
                    break;
                case Keyboard.ESCAPE:
                    popUp.displayPopUp = false
                    break;
                case Keyboard.CONTROL && Keyboard.SPACE:
                    if (requireSelection)
                        acceptCompletion();
                    else
                        popUp.displayPopUp = false
                    break;
            }
        }
    }  

I have a switch statement in my action script class.I have a function onKeyDown(event: KeyboardEvent) which contain a switch statement.which control the different keys.
But now I want to handle the same functionality which TAB key is doing using two keys CTRL and SPACE key.
I want add a case more in my switch statement first I pressed CTRL and then SPACE key then a a specific function is called.

Anyone can tell me how two keys used in my switch statement?

    private function onKeyDown(event: KeyboardEvent) : void{

        if (popUp.displayPopUp){
            switch (event.keyCode){
                case Keyboard.UP:
                case Keyboard.DOWN:
                case Keyboard.END:
                case Keyboard.HOME:
                case Keyboard.PAGE_UP:
                case Keyboard.PAGE_DOWN:
                    inputTxt.selectRange(text.length, text.length)
                    list.dispatchEvent(event)
                    break;
                case Keyboard.ENTER:
                    acceptCompletion();
                    break;
                case Keyboard.TAB:
                    if (requireSelection)
                        acceptCompletion();
                    else
                        popUp.displayPopUp = false
                    break;
                case Keyboard.ESCAPE:
                    popUp.displayPopUp = false
                    break;
                case Keyboard.CONTROL && Keyboard.SPACE:
                    if (requireSelection)
                        acceptCompletion();
                    else
                        popUp.displayPopUp = false
                    break;
            }
        }
    }  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睡美人的小仙女 2024-11-01 23:10:10

这是一个很常见的问题。

event.keyCode 只会为您提供最近按下的键的 keyCode。因此,您必须存储按键操作和按键释放操作。幸运的是,AS3 是一个非常好的数组实现,可以轻松完成此操作。

var _keys:Array;

addEventListener(KeyboardEvent.KEY_DOWN, keyDownEvent);
addEventListener(KeyboardEvent.KEY_UP, keyUpEvent);

function keyDownEvent(e:KeyboardEvent)
{
    // turn the key on
    _keys[e.keyCode] = true;

    // perform logic
    keyLogic()
}
function keyUpEvent(e:KeyboardEvent)
{
    // turn the key off
    _keys[e.keyCode] = false;
}
function keyLogic()
{
    // this is where the actual logic is
    if(_keys[Keyboard.SPACE] && _keys[Keyboard.CONTROL])
    {
        if (requireSelection)
            acceptCompletion();
        else
            popUp.displayPopUp = false
    }
}

这使您可以拥有一个整洁的小数组,它总是告诉您按下了哪些键。它还将程序逻辑与输入逻辑分开。双赢。

This is a pretty common problem.

event.keyCode is only going to give you the keyCode for the most recent key pressed. So you will have to store key presses and key releases. Fortunately, AS3 is a pretty sweet Array implementation that allows this to be done easily.

var _keys:Array;

addEventListener(KeyboardEvent.KEY_DOWN, keyDownEvent);
addEventListener(KeyboardEvent.KEY_UP, keyUpEvent);

function keyDownEvent(e:KeyboardEvent)
{
    // turn the key on
    _keys[e.keyCode] = true;

    // perform logic
    keyLogic()
}
function keyUpEvent(e:KeyboardEvent)
{
    // turn the key off
    _keys[e.keyCode] = false;
}
function keyLogic()
{
    // this is where the actual logic is
    if(_keys[Keyboard.SPACE] && _keys[Keyboard.CONTROL])
    {
        if (requireSelection)
            acceptCompletion();
        else
            popUp.displayPopUp = false
    }
}

This allows you to have a neat little array that always tells you what keys are down. It also separates your program logic from your input logic. Double win.

与君绝 2024-11-01 23:10:10

在您的情况下,您只需检查 键盘事件。但@DingoEatingFuzz 的答案将允许检查“空格”+“字母”等组合。

In your case you can just check ctrlKey property on KeyboardEvent. But @DingoEatingFuzz's answer will allow check combinations like 'space' + 'letter' and so on.

娇女薄笑 2024-11-01 23:10:10

案例键盘.SPACE:
if (事件.ctrlKey)
{

}
休息;

我认为这个方法很好......

case Keyboard.SPACE:
if (event.ctrlKey)
{

}
break;

i think this one approch is good........

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文