Skip to main content
January 24, 2013
Answered

Ctrl up and down sensing

  • January 24, 2013
  • 2 replies
  • 2048 views

Hi

I want to set a public var to true when ctrl of shift are pressed, and then false when they are not pressed.  This is to tie in with some datagrid functions that react differntly when Ctrl or shift are down.

onKeyBoardDown() is sussessfully tracing when the keys are down, but onKeyboardUp is not when they are released. Why is that?  THANKS!

stage.addEventListener(KeyboardEvent.KEY_UP, onKeyBoardUp);

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyBoardDown);

private function onKeyBoardDown(event:KeyboardEvent):void

{

    if ( event.ctrlKey ) //if control key is down

    {

        trace ('CTRL on');

    }

    else if ( event.shiftKey )

    {

       

        trace ('SHIFT on');

    }

    else

    {

        trace('NONE!!!');

    }

   

}

private function onKeyBoardUp(event:KeyboardEvent):void

{

    if ( event.ctrlKey ) //if control key is up

    {

        trace ('CTRL off');

    }

    else if ( event.shiftKey )

    {

       

        trace ('SHIFT off');

    }

   

}

This topic has been closed for replies.
Correct answer sinious

To answer why your original code doesn't work, you're using KeyboardEvent to indicate the current state of the 2 keys in an "if/else" manner.

For example, when you release CTRL, event.ctrlKey is always going to be false (because it's no longer down of course). Same thing with releasing SHIFT if you release that, it will always be false.

Use the keyCode as moccamaximum mentioned but you should detect combinations by not limiting it to "if/else".

e.g.:

var ctrlIsDown:Boolean = false;

var shiftIsDown:Boolean = false;

function onKeyBoardDown(e:KeyboardEvent):void

{

    if ( e.keyCode == 17 )

    {

        trace ('CTRL on');

        // set state var for CTRL

               ctrlIsDown = true;

    }

    // don't use else here, separate logic

    if ( e.keyCode == 16 )

    {

        trace ('SHIFT on');

               // set state var for SHIFT

               shiftIsDown = true;

    }

}

function onKeyBoardUp(e:KeyboardEvent):void

{

    if ( e.keyCode == 17 )

    {

        trace ('CTRL off');

               // set state var for CTRL

               ctrlIsDown = false;

    }

    // dont use else here, separate logic

    if ( e.keyCode == 16 )

    {

        trace ('SHIFT off');

               // set state var for SHIFT

               shiftIsDown = false;

    }

    // make a function to check key states

    // if necessary here and do any adjustments

    // based on shift or control separately

}

keyCode has limits, beware. Alternate keyboards are known to change keyCode values to unexpected results. Change as necessary. Consider charCode as well as keyCode has other limits:

http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf64a29-7fdb.html#WS2db454920e96a9e51e63e3d11c0bf64a29-7fed

2 replies

sinious
siniousCorrect answer
Legend
January 24, 2013

To answer why your original code doesn't work, you're using KeyboardEvent to indicate the current state of the 2 keys in an "if/else" manner.

For example, when you release CTRL, event.ctrlKey is always going to be false (because it's no longer down of course). Same thing with releasing SHIFT if you release that, it will always be false.

Use the keyCode as moccamaximum mentioned but you should detect combinations by not limiting it to "if/else".

e.g.:

var ctrlIsDown:Boolean = false;

var shiftIsDown:Boolean = false;

function onKeyBoardDown(e:KeyboardEvent):void

{

    if ( e.keyCode == 17 )

    {

        trace ('CTRL on');

        // set state var for CTRL

               ctrlIsDown = true;

    }

    // don't use else here, separate logic

    if ( e.keyCode == 16 )

    {

        trace ('SHIFT on');

               // set state var for SHIFT

               shiftIsDown = true;

    }

}

function onKeyBoardUp(e:KeyboardEvent):void

{

    if ( e.keyCode == 17 )

    {

        trace ('CTRL off');

               // set state var for CTRL

               ctrlIsDown = false;

    }

    // dont use else here, separate logic

    if ( e.keyCode == 16 )

    {

        trace ('SHIFT off');

               // set state var for SHIFT

               shiftIsDown = false;

    }

    // make a function to check key states

    // if necessary here and do any adjustments

    // based on shift or control separately

}

keyCode has limits, beware. Alternate keyboards are known to change keyCode values to unexpected results. Change as necessary. Consider charCode as well as keyCode has other limits:

http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf64a29-7fdb.html#WS2db454920e96a9e51e63e3d11c0bf64a29-7fed

January 24, 2013

Thank you both.

Is there a reason that event.ctrlKey doesn't work on release that I'm misunderstanding?

Also, are you guys familiar with Macs ie are shift and ctrl used in the same as Windows for multi selection, or should I be setting up extra key codes to test on Macs as well?

Inspiring
January 24, 2013

I must admit, since I always used keyCode, I never thought to deeply about how the named "special" keys work differently from the "common". But sinious' explanation is really good.

Inspiring
January 24, 2013

It`s weird, I `ll give you that.

Workaround: use keyCode (tested under win 7 64bit)

stage.addEventListener(KeyboardEvent.KEY_UP, onKeyBoardUp);

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyBoardDown);

function onKeyBoardDown(e:KeyboardEvent):void

{

    //trace("Down:"+e.keyCode);

    if ( e.keyCode == 17 )

    {

        trace ('CTRL on');

    }

    else if ( e.keyCode == 16 )

    {

        trace ('SHIFT on');

    }

}

function onKeyBoardUp(e:KeyboardEvent):void

{

    //trace("UP:"+e.keyCode);

    if ( e.keyCode == 17 )

    {

        trace ('CTRL off');

    }

    else if ( e.keyCode == 16 )

    {

        trace ('SHIFT off');

    }

}