No internet connection
  1. Home
  2. How to

Wait For Key Press?

By Blake Bunzel @Blake_Bunzel
    2021-09-16 00:31:17.877Z

    Hello, I am putting together a Macro for EQ matching. I was wondering if there is a way to make the script wait for key presses (ie: space bar for play, space bar again for stop) to have the plugin i'm using listen for the match, then have SF finish the script.

    Thank you!

    • 16 replies
    1. Kitch Membery @Kitch2021-09-16 01:10:12.588Z

      Hi @Blake_Bunzel,

      Welcome to the forum :-)

      Can you describe the manual steps you would take to achieve what you are after?

      Generally, writing scripts that wait for user input, quickly becomes very advanced and/or unstable.
      But by rethinking the workflow so that the script does the work for you, may be a better approach.

      :-)

      1. BBlake Bunzel @Blake_Bunzel
          2021-09-16 01:20:04.672Z

          Hey Kitch!

          I have a script that utilizes Fab Filter's Pro Q 3 which a bunch of Mouse Click Relative to UI. It's currently split up into 2 macros:
          Macro 1. Sets the plugin to it's default state. Clicks through Pro Q3's menu items at the bottom to tell the plugin to listen to the sidechain. I then run program (hit space bar) to have the plugin listen to the side chain and adjust it's EQ curce. Once that is in the plugin, I will hit stop (space) bar.
          Macro 2. I then hit "Match", then "Finish", then click the gain scale a the bottom right, enter 30 to draw back from 100% to 30%.

          Essentially the only break between Macro 1 and Macro 2 is having to hit space bar to run program and again to stop program. I figure, if the script waits for these 2 space bar hits, it would then allow me to combine the Macros to one.

          1. Kitch Membery @Kitch2021-09-16 03:07:30.559Z

            Ahh I see...

            You could use the following code to wait for Pro Tools to stop playing;

            Found in @samuel_henriques post Wait for Pro Tools to stop playing or recording (is it the same?) #post-2 ;

            while (true) {
                if (sf.ui.proTools.isPlaying) {
            
                    sf.wait()
            
                    log("is playing")
                } else {
            
                    log("stoped")
                    break;
                }
            }
            

            Or you could use a more in-depth way that checks if Pro Tools is playing every 100 milliseconds while letting you know how to cancel the script while Pro Tools is playing.

            let currentTime = Date.now();
            let getTime = () => Date.now();
            let count = 0
            
            log("Is playing: To cancel script press Ctrl+Shift+Esc");
            
            while (sf.ui.proTools.isPlaying) {
            
                if (count < 0 || getTime() > currentTime + 5300) {
                    log("Is playing: To cancel script press Ctrl+Shift+Esc");
                    currentTime = Date.now();
                }
                sf.wait({ intervalMs: 100 });
            
                count++
            }
            
            log("Stoped");
            

            Simply add code to press play at the end of your first script and them place one of the above scripts between your first and second script. :-)

            Let me know if that makes sense, or if you need further explanation.

            Rock on!

            1. BBlake Bunzel @Blake_Bunzel
                2021-09-16 17:41:25.495Z

                Following back up here to report that the 2nd script worked perfectly!! Thank you Kitch! LOVING SF

                1. Kitch Membery @Kitch2021-09-16 19:17:33.743Z

                  Fantastic:. Rock on!

                2. In reply toKitch:

                  @Kitch Out of curiosity, what is count doing? In the while loop the script is checking if count is less than zero OR if 5300ms has passed (to keep the notification persistent). count is never less than zero, so what is it for?

                  1. It seems to run fine without the count variable.

                    1. In reply toChris_Shaw:
                      Kitch Membery @Kitch2021-09-16 22:08:20.949Z

                      Hi @Chris.

                      With out the count, the first time the script runs the log will only appear after 5300 milliseconds. the count is to make sure the log displays instantly the first time :-)

                      Rock on!

                      1. In reply toChris_Shaw:
                        Kitch Membery @Kitch2021-09-16 22:35:56.343Z

                        @Chris_Shaw,

                        I take that back... You are right, thanks for picking it up. I put the count there before I moved the 100ms wait to the end of the while loop. Great catch. :-)

                        New version;

                        let currentTime = Date.now();
                        let getTime = () => Date.now();
                        
                        log("Is playing: To cancel script press Ctrl+Shift+Esc");
                        
                        while (sf.ui.proTools.isPlaying) {
                        
                            if (getTime() > currentTime + 5300) {
                                log("Is playing: To cancel script press Ctrl+Shift+Esc");
                                currentTime = Date.now();
                            }
                            sf.wait({ intervalMs: 100 });
                        }
                        
                        log("Stoped");
                        

                        @Blake_Bunzel here is an updated version. :-)

                        Rock on!

                        1. Aah, I get it.
                          In the old version the if statement should have checked if count == 0 instead of count < 0. The new version is more concise. 👍

                          1. Kitch Membery @Kitch2021-09-16 23:11:47.517Z

                            Refactor, repeat, Story of my life! :-)

                            1. Mine as well…🤦‍♂️

                              1. Kitch Membery @Kitch2021-09-17 00:47:06.170Z

                                Hahahaha :-)

                          2. In reply toKitch:
                            BBlake Bunzel @Blake_Bunzel
                              2021-09-17 01:10:38.454Z

                              Thanks again!

                    2. B
                      In reply toBlake_Bunzel:
                      Blake Bunzel @Blake_Bunzel
                        2021-09-16 04:05:49.799Z

                        Thank you Kitch!! I'll give this a try and will circle back if I have any questions

                        1. A
                          In reply toBlake_Bunzel:
                          Alexander Gruzdev @Alexander_Gruzdev
                            2024-06-27 01:36:57.274Z

                            Hello Blake,
                            Would you be so kind to share your script for Fabfilter Q3 eq match process.
                            Appreciate any response. thank you.
                            AG