Skip to main content
Inspiring
July 22, 2013
Pregunta

keyboard Event: How can I only capture the modifier keys?

  • July 22, 2013
  • 1 respuesta
  • 3703 visualizaciones

Hey guys,

I know that i can use event.shiftKey, event.ctrlKey, event.altKey and event.metaKey to check whether a certain modifier key is pressed or not. But these properties will return undefined if the keyIndentifier is for modifier key.

But what I want to do is only capture the modifier keys

For example,

var w = new Window("dialog","keyboard");

var textInput = w.add("edittext");

textInput.preferredSize = [100,30];

textInput.active = true;

textInput.addEventListener ("keydown", function (k){sayHi(k)});

function sayHi (k)

{

    if (k.keyName == "Alt")

    {

        textInput.text = "Hello "

     } 

}

w.show();

I created a dialog with a edit text field, what I want to do is change the text to "Hello" when the "Alt" key is pressed, but my code doesn't work.

So, how can I only capture the modifier keys without need to press other keys or click the mouse at the same time?

Este tema ha sido cerrado para respuestas.

1 respuesta

Alan_AEDScripts
Inspiring
July 22, 2013

Keyboard state object works well independent of event listeners...

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

                    function shiftState() {

                                                  var myKeyState = ScriptUI.environment.keyboardState;

                                                  if(myKeyState.shiftKey == true) {return 1;}

                                                  else {return 0;}

                                        } //close.

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

if(shiftState()==1){//do it}

Alan_AEDScripts
Inspiring
July 22, 2013

I think you also need to have the event listener trigger within an onChange / changing callback for the edit text.

Also check out the UI manual as you can use a boolean property to check ALT

From manual...

//=======================================================

altKey Boolean When true, the ALT key was active. Value is undefined if the

keyIdentifier is for a modifier key.

//=======================================================

Alan_AEDScripts
Inspiring
July 22, 2013

var w = new Window("dialog","keyboard");

var textInput = w.add("edittext");

textInput.preferredSize = [100,30];

//textInput.active = true;

//textInput.addEventListener("keydown", function (k){sayHi(k)});

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

function shiftState() {

          var myKeyState = ScriptUI.environment.keyboardState;

          if(myKeyState.shiftKey == true) {return 1;}

                                                                        else {return 0;}

} //close.

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

textInput.onChanging=function(){

          //You have to hit shift plus another key here..

          if(shiftState()==1){this.text = "The Shift Key..."}

 

          }

textInput.onActivate=function(){

          if(shiftState()==1){this.text = "first click on activate"}

 

          }

w.show();