1 Reply Latest reply on Jun 21, 2018 1:32 PM by Deepak Gupta

    Making one Keyboard Shortcut context-sensitive to sketch, part, assembly or drawing (using Autohotkey)

    Kevin Chandler

      Context-sensitive keyboard shortcut executes different commands assigned to the currently active document type.

      Allows you to use one keyboard shortcut to run a set of commands specific to a sketch, part, assembly or drawing, whichever is current when the keyboard shortcut is pressed.

      This feature is made possible by www.autohotkey.com scripts.

       

      SCENARIO A:

      Keyboard shortcut automatically detects which is active: sketch; part; assembly or drawing, and executes only those commands assigned to that document type. Commands can be pulldown menu letters or the keystroke to a currently assigned SolidWorks keyboard shortcut.
      See SCENARIO A CAVEATS.

       

      SCENARIO B:

      Keyboard shortcut automatically detects if you're currently editing a sketch and executes the sketch-assigned commands, else the script sends the keystrokes associated with a non-sketch SolidWorks keyboard shortcut.
      This can also be a simplified implementation of SCENARIO A when the keyboard shortcut only needs to be shared between any two document types (assuming SCENARIO A caveats are satisfied).
      See SCENARIO B CAVEATS.

       

      If you have several of these  keyboard shortcut scripts, you can load all from one script using the #Include command (sample script is attached, paths must be edited to suit). This also allows either stopping or reloading all shortcut scripts at once.

      If you use the EXE files, then a bat/cmd file can be used to load all of your shortcuts at once.

       

      Two sample scripts (and their compiled EXE files) are attached for testing.


      A green square with an "A" icon will appear in your tray for each script (unless you do the #Include script, then it's just one icon).
      Hover over the icon to see its name.
      Right-click the icon and select Reload if the script has stopped working or Exit to stop the script.

       

      SCENARIO A CAVEATS:
      A1. Uses the window title to detect whether it's currently a part or an assembly by their file extension, so:
           A1.1 "Hide common file extensions" in Windows Explorer view options must be unchecked.

                A1.1.1 If not, see SCENARIO B.
           A1.2 Extension detection is case-sensitive, so consistent spelling is required.

                A1.2.1 If not, see SCENARIO B.
      A2. Uses the window title to detect whether it's currently a drawing by the presence of the phrase "- Sheet" (the SLDDRW extension isn't shown in the window title for drawings), so:
           A2.1 If you rename your drawing sheets and always have a consistent and unique bit of text to differentiate the current document type as being a drawing, the script can be modified to use this text instead of "- Sheet".
           A2.2 If you rename sheets and don't have a consistent/unique bit of text, see SCENARIO B.

      A3. Uses the window title to detect whether you're currently editing a sketch by the presence of the word "Sketch", so:

           A3.1 Similar to drawings caveat A2.1, "Sketch" must always be in the sketch name (or some other unique text).
           A3.2  If you rename sketches and don't have a consistent/unique bit of text, see SCENARIO C.

       

      SCENARIO B CAVEATS:
      B1. Caveat A3 still applies, else see SCENARIO C.

       

      SCENARIO C: Wait for SolidWorks to implement this.

       

      SCENARIO A IMPLEMENTATION:

      This is the script framework with Ctrl+Alt+M assigned to Mirror in a sketch, Fillet in a part, Component Mirror in an assembly or Balloon in a drawing.

      $!^m::

      ;

      SetTitleMatchMode, 2

      if WinActive("Sketch")

      {

        Send, !ttm{Enter}

        Return

      }

      ;

      else if WinActive(".SLDASM")

      {

        Send, !ir{Enter}

        Return

      }

      ;

      else if WinActive(".SLDPRT")

      {

        Send, !irf{Enter}

        Return

      }

      ;

      else if WinActive(" - Sheet")

      {

        Send, !ia{Enter}a{Enter}

        Return

      }

      Send, !^m

      Return

       

      Ctrl+Alt+M is the keystroke assignment and when pressed, the script reads the title of the active window.

      When there's a match, the Send command sends keystrokes as if you enter them. In this case, it's the Alt pulldown menu underlined letters, but you can also send the keystroke combination of an already existing SW keyboard shortcut (this is shown in SCENARIO B IMPLENTATION).

      After the matching keystrokes have been sent, "Return" exits the keystroke, but the script remains operational.

      The last Send is in case there's no match found, Ctrl+Alt+M is sent as the normal sequence in case it's used elsewhere.

      The "$" before "!^m at the beginning is to prevent this script from recursively calling itself.

      If you don't want to assign commands for a particular document type, comment out its else if code bunch with a semicolon at the beginning of each line.

       

      SCENARIO B IMPLEMENTATION:

      This is the script framework with Ctrl+Alt+M assigned to Mirror in a sketch, Fillet in a part,

      $!^m::

      ;

      SetTitleMatchMode, 2

      if WinActive("Sketch")

      {

        Send, !ttm{Enter}

        Return

      }

      Send, !^m

      Return

       

      This works as above except it only looks for a sketch (in this case, but it could one of the other document types too, if caveats are met).

      If you're not in a sketch, Ctrl+Alt+M is passed along to SW where there is an already assigned "regular" SW keyboard shortcut assigned.

       

      AHK codes for keystrokes:

      ! = Alt

      ^ = Ctrl

      + = Shift

      < or > = Assign Left/Right only of that key (example: >! is right Alt only)

       

      This was prompted by this Josh Brady post: Opinions... Context-sensitive shortcut keys?

       

      I hope you find these utilities useful.

      If you have any questions, please let me know.

       

      Cheers,

       

      Kevin