7 Replies Latest reply on Dec 7, 2012 11:32 PM by Sleepy Lion

    How to get the Key mode

    Sleepy Lion

      Hello

       

      Let me ask a question about key mode.

       

      I'm trying to catch the timing when both Shift-Key and Esc-Key are pressed.

       

      Using the Edge Animate's document.keydown  events , I can get the chance when the Shift-Key is pressed.

      At the same time I need to know the Esc-Key's mode , pressed? or released?.

      Does anyone know how to get the Ket mode?

       

      regards

        • 1. Re: How to get the Key mode
          heathrowe Most Valuable Participant

          Here is a sample to capture when the Shift + Esc keys are pressed. For my example I added the following to the keydown event handler of Stage.

           

          ///////// Copy to keydown handler ////////

          if (e.shiftKey && e.which ==27) {

          alert(e.which + " Shift + Esc was pressed");

          }

          ///////// End Copy ///////////////////////////////

           

          e.shiftKey can also be written as e.which ==16

           

          The operator && equates to and so the condition is only true when shift + esc keys are pressed.

           

          You get more charcodes from the following link, which has textfield samples that when you hit a key it will reveal the character code equivelent.

          http://www.javascriptkit.com/jsref/eventkeyboardmouse.shtml

           

           

          hth

          Darrell

          • 2. Re: How to get the Key mode
            Sleepy Lion Level 1

            Thank you very much heathrowe.

            It worked fine!

            Can I ask one more question about this? 

             

            The timing of users' press action varies.

            Someone press Shif-Key first and Esc-Key next and vice versa.

            ( It might be an event in a 0.1 second  )

             

            I think the sample code works when users press Shif-Key first and Esc-Key next.

            To cover the inverse pattern it is needed to know the condition that Esc-Key is down or not.

             

            Is it possible? 

            • 3. Re: How to get the Key mode
              joel_pau Level 5

              Darrel will answer your question.

              I add this link: because there is a live demo. See: Example/Demo and you type one letter.

              • 4. Re: How to get the Key mode
                heathrowe Most Valuable Participant

                Thanks  Joel for the jquery api reference link

                 

                Sleep Lion - change the event handler on keyup instead of keydown.

                 

                In other words add the following to the the keyup event handler of the element (example, Stage)

                 

                if (e.shiftKey && e.which ==27) {

                alert(e.which + " Shift + Esc was pressed");

                }

                 

                hth

                Darrell

                • 5. Re: How to get the Key mode
                  Sleepy Lion Level 1

                  Thank you very much heathrowe.

                  hmm, It seems to be the same thing.

                   

                  If users release [Esc-key] first and [Shift-key] next, this script works.

                  But it doesn't work in the contrary case.

                  To cover both cases, the system have to grasp the STATE of the keys, not EVENT.

                   

                   

                   

                   

                  I've made an example below .....

                  This script can hold the state of the keys.

                   

                   

                  [Note 1] This time I used [Esc-Key] and [z-key] ,  because the task manager opens

                               when [Shift-Key] and [Esc-key] is pressed in case of Chrome.

                   

                   

                  [Note 2] I used text-box named "Result" as an output tool instead of the alert tool,

                               because the variables (KeyFlagShift / KeyFlagZ) freeze using an alert tool

                              for some unknown reason.

                   

                   

                  //-----------------------------------------------------------

                  // ******  compositionReady  ******

                  sym.setVariable("KeyFlagShift" , false);

                  sym.setVariable("KeyFlagZ" , false);

                   

                   

                  //-----------------------------------------------------------

                  // ******  KeyUp  ******

                  switch(e.which){

                            case 16:

                                                sym.getComposition().getStage().setVariable("KeyFlagShift" , false);

                                                break;

                            case 90:

                                                sym.getComposition().getStage().setVariable("KeyFlagZ" , false);

                                                break;

                  }

                   

                   

                  //-----------------------------------------------------------

                  // ******  KeyDown  ******

                  switch(e.which){

                            case 16:

                                                sym.getComposition().getStage().setVariable("KeyFlagShift" , true);

                                                break;

                            case 90:

                                                sym.getComposition().getStage().setVariable("KeyFlagZ" , true);

                                                break;

                  }

                   

                   

                  var KeyS = sym.getComposition().getStage().getVariable("KeyFlagShift");

                  var KeyZ = sym.getComposition().getStage().getVariable("KeyFlagZ");

                  if(KeyS == true && KeyZ == true){

                            sym.getComposition().getStage().$("Result").text(" Shift + Esc was pressed");

                  }else{

                            sym.getComposition().getStage().$("Result").text(" -------");

                  }

                   

                   

                  //-----------------------------------------------------------

                  • 6. Re: How to get the Key mode
                    heathrowe Most Valuable Participant

                    This is not thorough, but may be on track. Unfortunately I am not that familiar with switch statements, and since you need to test for a condition in the keydown/keyup handlers, I find it easier to do this directly onto the compositionReady handler of Stage

                     

                    Example

                     

                    var esc_down = false,esc_key = 27,z_key = 90;

                    /

                    / chain event conditionals

                    $(document).keydown(function(e) {
                        if (e.which == esc_key) esc_down = true;
                    }).keyup(function(e) {
                        if (e.which == esc_key) esc_down = false;
                    });

                     

                    $(document).keydown(function(e) {
                        //e.which == 'Z'.charCodeAt(0)
                        if (esc_down && e.which == z_key) {
                            sym.$("Result").html("Esc+" +String.fromCharCode(e.which));
                            return false;
                        }
                       
                        if (!esc_down) {
                            sym.$("Result").html("-----");
                            return false;
                        }
                    });

                     

                    Darrell

                    • 7. Re: How to get the Key mode
                      Sleepy Lion Level 1

                      Thank you very much heathrowe.

                       

                      I don't have enough knowledge about "chain event", so I could not understand how your example works. sorry.

                      But only copied and pasted it into compositionReady and made it run it.

                      It seemed to catch the event of  "if (!esc_down)", but else didn't.

                       

                      Anyway, thank you for a lot of advices.