7 Replies Latest reply on Aug 10, 2012 8:20 AM by MattLeach

    Pass keystroke to script

    MattLeach

      I have a button on my layout to add a note into a second table. What i would like to do it change the way the button behanves based on a keystroke.

       

      For example, if i hold Ctrl and click the button, i would like to be able to capture that keystroke so the script can check for that key and perform additional steps.

       

      Currently, i have the button setup to pass a few parameters to the script:

       

      List ( Get ( AccountName ) ; Get ( ActiveModifierKeys ) )

       

      The first two steps of the called script separate the parameters into variables:

       

      Set Variable [$user; Value:GetValue ( Get ( ScriptParameter ) ; 1 )]

      Set Variable [$key; Value:GetValue ( Get ( ScriptParameter ) ; 2 )]

       

      It appears that no matter what key i press while clicking, the $key value stays null so im assuming im going about this incorrectly.. What would be the best approach for something like this to work?

        • 1. Re: Pass keystroke to script
          DrewTenenholz

          Matt --

           

          Just ran a quick test, and that should work just fine.  If you look carefully in the script debugger, you should be able to see:

           

          When you click the button, the debugger's call stack pane says something like

           

            (name of file - name of script )

           

          If you move one step along, the debugger should read:

           

          ScriptName (FileName , Parameter: )

           

          and you should be able to see both the account name and the value (at least a zero should be here for the modifier keys, not an empty value)

           

          and then the rest is up to you to get the parameter into script variables (which is fine BTW).

           

           

          Now, I have done something to my own laptop which re-maps the right-side option key on my Mac laptop to be read as the 'Enter' key.  When I hold that key down and click, I do get a zero (expected), but at least I get a value.  Unless you remapped your keyboard (or are using an external keyboard that is somehow not actually connected to the machine (low-battery?), this should work.

           

          By the way, I have a programming habit where I use a single

           

          Set Variable ( $ScriptParameter ; Get ( ScriptParameter ) )

           

          near the top of every script.  I then base all relevant script variables off of $ScriptParameter.  It's easier to type, easier to debug, and allows me to hang on to the original script parameter regardless of what crazy ideas I come up with for subsequent variables.

           

          -- Drew Tenenholz

          • 2. Re: Pass keystroke to script
            MattLeach

            I did a screen shot of the data viewer and the script debugged side by side.

             

            In this instance i held down the Ctrl key and clicked the button, the value for the modifier keys is 0.

             

            No matter what key i press while clicking the button, the result is 0

             

            parameter.png

            • 3. Re: Pass keystroke to script
              comment

              There is no need to send ActiveModifierKeys as part of the script parameter - you can get it directly inside the script. However, it still should work. Does the attached test file work for you?

              1 of 1 people found this helpful
              • 4. Re: Pass keystroke to script
                MattLeach

                Found the issue.

                 

                It appears that if the script debugger is open, the result is always 0.

                 

                If it is closed, it then works as expected. I never did run the script without those close which is why i was never able to get a result other than 0.

                1 of 1 people found this helpful
                • 5. Re: Pass keystroke to script
                  DrewTenenholz

                  Matt --

                   

                  Maybe this is just a silly timing issue. 

                   

                  I always get 0 when:

                  I hold down a key; I click the button; I let go of the key; I start stepping through the debugger.

                   

                  I get a value when:

                  I hold down a key; I click the button; ; I start stepping through the debugger; I let go of the key.

                   

                  Effectively, the value for Get(ActiveModifierKeys) is captured as the call stack moves from the button to the script, NOT when the button is clicked.

                   

                  -- Drew

                  • 6. Re: Pass keystroke to script
                    comment

                    MattLeach wrote:

                     

                    It appears that if the script debugger and/or the data viewer is open, the result is always 0.

                     

                    That's not what I see. But you must hold the modifier keys when you click 'Run' in the debugger.

                    • 7. Re: Pass keystroke to script
                      MattLeach

                      After further testing you are correct, i wasnt holding the key down when i clicked run in debugger.

                       

                      Thanks for the help guys!.