Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

ScriptUI : Check if shiftKeyPressed when clicking a button ?

People's Champ ,
Nov 23, 2009 Nov 23, 2009

Hello,

I would like to know if I can control if the shift key is pressed when clicking on a button ?

I saw :

DrawState.shiftKeyPressed (Read Only)
Data Type: boolean
True if the Shift key is being pressed.


But it's connected to :

DrawState
Describes an input state at the time of the triggering   ScriptUIGraphics.onDraw()  event.
Contains properties that report whether the current control has the input focus, and the particular mouse button and keypress state. Passed in as argument to  ScriptUIGraphics.onDraw() .

But I can't see how to connect my button click event and the shiftkeypressed control :-S ?

Any hint ? TIA Loic

TOPICS
Scripting
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Nov 23, 2009 Nov 23, 2009

Hi Loic,

Here's a basic skeleton (only works in CS4):

uiElement.addEventListener( "keydown", handleShift );

function handleShift ( myEvent ) {

if ( myEvent.shiftKey ) { // Shift key pressed down

isShiftPressed = true;

}

else {

isShiftPressed = false;

}

}

Harbs

Translate
LEGEND ,
Nov 23, 2009 Nov 23, 2009

Hi Loic,

Here's a basic skeleton (only works in CS4):

uiElement.addEventListener( "keydown", handleShift );

function handleShift ( myEvent ) {

if ( myEvent.shiftKey ) { // Shift key pressed down

isShiftPressed = true;

}

else {

isShiftPressed = false;

}

}

Harbs

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Nov 23, 2009 Nov 23, 2009

Awesome Harbs,

Thanks for your help on that topic 😉

Bye Loic

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Nov 23, 2009 Nov 23, 2009

Oups too quick,

I am trying hard to make it run but it behaves weirdly (on my fault)

#targetengine session
rapidDlg = new Window('window',"btn test", undefined);
rapidDlg.btn = rapidDlg.add('button',undefined,'is shift pressed ?');

rapidDlg.btn.onClick = function(){
    pressed = rapidDlg.btn.addEventListener( "keydown", handleShift  );
    alert(pressed);
    rapidDlg.btn.removeEventListener( "keydown", handleShift  );
}

rapidDlg.show();

function handleShift ( myEvent ) {
     if ( myEvent.shiftKey ) { // Shift key pressed down
          isShiftPressed = true;
     }
     else {
          isShiftPressed = false;
     }
    return isShiftPressed;
}

alert(pressed) on the onClick callback sometimes throws false when true, sometimes sticks to true whatever shift is pressed or not...

I must do something wrong but can't see where :-S

Loic

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Nov 23, 2009 Nov 23, 2009

rapidDlg = new Window('window',"btn test", undefined);

rapidDlg.btn = rapidDlg.add('button',undefined,'is shift pressed ?');

rapidDlg.btn.addEventListener( "mousedown",function (myEvent) {

myEvent.target.text = myEvent.shiftKey ? "Yes" : "No";

});

rapidDlg.show();

Dirk

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 23, 2009 Nov 23, 2009

Hi Loic,

Here's a more useful example. (It's one of the components in my Script UI libarary which mimics the behavior of InDesign's Integer edit boxes.) A big chunk of the code is based on the work of Bob Stucky. To use it you just do:

myIntegerEditbox = new HarbsUI.IntegerEditBox(myContainer[,props]);

All the props values are optional, and you can specify smallNudge and largeNudge values like in the standard InDesign widgets.

     HarbsUI = {};
     
     HarbsUI.IntegerEditBox = function(container,props){//width,editValue,minimumValue,maximumValue,largeNudge,smallNudge
          var width = props.width || 50;
          this.value = props.editValue || 0;
          this.minimumValue = props.minimumValue || 0;
          this.maximumValue = props.maximumValue || 100;
          this.text = this.value;
          var editBox = container.add("edittext", undefined, this.value );
          this.largeNudge = props.largeNudge;
          //alert(this.largeNudge);
          this.smallNudge = props.smallNudge;
          var that =this;
          if(kAppVersion >=6){
               
               editBox.addEventListener( "keydown", handleNudge  );
          }
          editBox.preferredSize.width = width;
          editBox.widget = this;
          editBox.onChanging = function() {
               if ( this.text.length == 0 ) {
                    //alert(2);
                    this.text = this.widget.value;
                    this.widget.text = this.text;
               }
               else if ( isNaN( parseInt( this.text ) ) ) {
                    this.text = this.widget.text
               }
               else if ( parseInt( this.text ) > this.widget.maximumValue ) {
                    this.text = this.widget.maximumValue;
                    this.widget.text = this.text;
               }
               else if ( parseInt( this.text ) < this.widget.minimumValue ) {
                    this.text = this.widget.minimumValue;
                    this.widget.text = this.text;
               }
               else{
                    this.text=parseInt(this.text);
                    this.widget.text = this.text;
               }
          }
          function handleNudge ( myEvent ) {
               if ( myEvent.shiftKey ) { // then we're using the largeNudge
                    var myNudge = that.largeNudge;
               }
               else { // use the small nudge
                    var myNudge = that.smallNudge;
               }
               // handle the case of a blank entry in the field at the start
               if ( myEvent.target.text == "" ) {
                    var myNum = 0;
               }
               else {
                    var myNum = parseFloat( myEvent.target.text );
               }
               if(isNaN(myNum) ){myNum=0}
               switch( myEvent.keyName ) {
                    case "Down" : { // just fall through with myNudge set to it's negative value
                         myNudge = myNudge * -1;
                    }
                    case "Up" : {
                         //alert('up');
                         myEvent.target.text = higherMultiple( myNum, myNudge, that.maximumValue, that.minimumValue );
                         myEvent.target.nudged=true;
                         break;
                    }
               }
          }
     
     // this next function is to make sure the nudge feature increments in multiples of the nudge value, and handles the max/min value settings
          function higherMultiple ( myNum, myNudge, max, min ) {
          // The behavior of the InDesign's nudges is to go to the next multiple of the nudge value
          // for example, if the control's value is 7, the largeNudge is 5, a large nudge will make the value 10 (next multiple of the largeNudge).
               if ( myNudge < 0 ) { // if the nudge is negative, we're incrementing down, but in the loop below, we're always counting up
                    myNudge = myNudge * -1;
                    var myIncrement = -1;
               }
               else {
                    var myIncrement = 1;
               }
               // here we have to catch the case of the values being at max or min, we can't increment if that's the case
               if ( max != undefined && ( myNum + myIncrement ) >= max ) { // if there's a max value, and incrementing goes past it, return
                    return max;
               }
               else if ( min != undefined && ( myNum + myIncrement ) <= min ) { // if there's a min value, and decrementing goes past it, return
                    return min;
               }
               for ( var i = 0; i < myNudge; i++ ) {
                    myNum += myIncrement;
                    if ( myNum % myNudge == 0 ) { // if we're at a multiple of the nudge value, return the number
                         return myNum;
                    }
                    else if ( max && myNum >= max ) { // if there's a max value, and we're at it, return
                         return max;
                    }
                    else if ( min && myNum <= min ) { // if there's a min value, and we're at it, return
                         return min;
                    }
               }
               return myNum;
          }
          return editBox;
     }

Harbs

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Nov 23, 2009 Nov 23, 2009

Hi Harbs,

Thanks a lot for all your care and share,

The method you gave me works incredibly fine (mousedown). I will have a deeper look on your second proposal at rest 😉

A great thank you to you !

Loic

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Nov 23, 2009 Nov 23, 2009
LATEST

And A BIG BIG THANK YOU to Dick that I forget to thank

in the excitement 😉

Loic

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines