No internet connection
  1. Home
  2. How to

Templating If/Or/Else what am I doing wrong?

By Owen Granich-Young @Owen_Granich_Young
    2021-11-04 18:31:12.309Z

    @Kitch I found this script a long time back on the forum here :

    const modifierState = event.keyboardState
    
    if (modifierState.hasShift) {
    
        log("Shift is held down");
    
        //Add code here for when Shift is held down...
    
    } else if (modifierState.hasControl) {
    
        log("Control is held down");
        
        //Add code here for when Control is held down...
    
    } else if (modifierState.hasCommand) {
    
        log("Command is held down");
    
        //Add code here for when Command is held down...
    
    } else if (modifierState.hasAlt) {
    
        log("Option is held down");
    
        //Add code here for when Option is held down...
    
    } else {
    
        log("No modifiers are held down");
    
        //Add code here where no modifiers are held down///
    
    }
    

    With the goal of making it more accessible to us code Illiterate I'm working on making it into a template you can simply copy command ID's into so users can make their own multi-funtion buttons without needing to code.

    const modifierState = event.keyboardState
    const { holdControl, holdOption, holdShfit, holdCommand, noModifier } = event.props;
    
     if (modifierState.hasShift) {
    
        sf.soundflow.runCommand({
            commandId: holdShfit,
        });
    
    } else if (modifierState.hasControl) {
    
        sf.soundflow.runCommand({
            commandId: holdControl,
        });
    } else if (modifierState.hasCommand) {
    
        sf.soundflow.runCommand({
            commandId: holdCommand,
        });
    
    } else if (modifierState.hasAlt) {
    
        sf.soundflow.runCommand({
            commandId: holdOption,
        });
    
    } else {
    
        sf.soundflow.runCommand({
            commandId: noModifier,
        });
    
    }
    

    However for some reason the HOLD SHIFT modifer and the NO MODIFER are not working. All three of the }else if{ work but the if and the } else { are bugging out.

    What simple code am I getting totally wrong?

    Solved in post #15, click to view
    • 20 replies

    There are 20 replies. Estimated reading time: 8 minutes

    1. You might want to try a switch case instead.
      Additionally, instead of using hasAlt, hasControl. etc, try using event.keyboardState.asString. This way you'll be able to detect modifier combinations like shift-cmd. (I put a logging function to demonstrate.)

      const modifierState = event.keyboardState.asString
      log (modifierState)
      switch (true) {
          case (modifierState == "shift"):
              log("Shift is held down");
              break
          case (modifierState == "ctrl"):
              log("Control is held down");
              break
          case (modifierState == "cmd"):
              log("Command is held down");
              break
          case (modifierState == "alt"):
              log("Option is held down");
              break
          case (modifierState == ""):
              log("No Modifiers are held down");
              break
          case (modifierState == "alt+shift"): // two modifiers!
              log("Alt & Shift are held down");
              break
      };
      
      
      1. Sweet, I'll swap this out and try it!

        1. OH... MY .... GOD.... It was me all along. I had mispelled SHIFT and MODIFER in the SCRIPT vs the PROPERTIES.

          Your way gave me the same error Chris and it took me trying a diffrent verison to realize my mistake!

          User Fail. But I will use yours so people can have EVEN MORE modifer possiblites.

          Thanks!
          Owen

          1. glad I could help :)

            1. Kitch Membery @Kitch2021-11-04 21:21:52.645Z

              Ha! @Chris_Shaw beat me to it. Nice one Chris!.. And yes the Switch case is preferable for this scenario.

              Glad you two worked it out. :-)
              Rock on!

              1. BUT HAVE I TAKEN IT TOO FAR 😂

                Thanks again both of you, I'm going to publish 'Multi-Function Buttons for Dummys' to the store for all of us coding impaired.

                1. Kitch Membery @Kitch2021-11-04 21:44:32.644Z

                  Nice one @Owen_Granich_Young!

                  Sounds good! But be sure to read my advice in post #7

                  1. BUT HAVE I TAKEN IT TOO FAR 😂

                    Not really - there are 13 possible combos - you missed 2:
                    ctl-alt-cmd
                    shift-ctrl-alt-cmd

                    1. Kitch Membery @Kitch2021-11-04 22:59:48.230Z

                      BUT HAVE I TAKEN IT TOO FAR 😂

                      Hahahahaha no but @Chris_Shaw did!

                      1. In reply toChris_Shaw⬆:

                        Ha now I have to put those in later but also jeeez the quad might be a bit much for my fingers lol! Control option command is an important one tho!

                  2. Kitch Membery @Kitch2021-11-04 21:31:27.148Z

                    A side note on this, if you are going to be sharing it on the store is that reading modifier states should maybe be discouraged if the user is triggering the command presets via "Keyboard Triggers" as they may overlap with other SoundFlow command triggers (and SoundFlow has no way of knowing if there is a clash), they do however work quite well in conjunction with Stream Decks.

                    Let me know if that makes sense. I can give an example if needed.

                    Rock on!

                    1. I will make a note this is designed for streamdeck use, good point!

                      1. In reply toKitch⬆:
                        Chris Shaw @Chris_Shaw2021-11-04 21:59:35.630Z2021-11-04 22:24:51.731Z

                        Just to confirm what @Kitch says:
                        I tried to do something similar to this using keyboard triggers and the only modifiers a script like this will recognize are the ones assigned to the keyboard trigger. In order to recognize all available modifiers the user would have to assign multiple keyboard triggers (one for each modifier combo).

                        So I don't think that it would necessarily conflict with SF but rather it would be rather awkward / limiting.

                        So this script is best suited for decks or surfaces.

                        1. Yeah that all makes sense, tbh I only have built it thinking about streamlining my deck. And for friends who also want each button on their decks to be able to do more.

                          1. Kitch Membery @Kitch2021-11-04 22:11:05.826Z

                            Awesome!

                            1. The @raphaelsepulveda multi-click one should work fine with keybinding though, nifty alternative I also included in the package.

                              1. Kitch Membery @Kitch2021-11-04 22:18:01.339Z

                                Which one is that? Is there a link?

                    2. Kitch Membery @Kitch2021-11-04 22:17:28.796Z

                      A small refactor of the switch case;

                      const modifierState = event.keyboardState.asString
                      
                      switch (modifierState) {
                          case "shift":
                              log("Shift is held down");
                              break
                          case "ctrl":
                              log("Control is held down");
                              break
                          case "cmd":
                              log("Command is held down");
                              break
                          case "alt":
                              log("Option is held down");
                              break
                          case "":
                              log("No Modifiers are held down");
                              break
                          case "alt+shift": // two modifiers!
                              log("Alt & Shift are held down");
                              break
                      };
                      
                      Reply1 LikeSolution
                      1. of course!