sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Key sequence in ruby

    Scheduled Pinned Locked Moved Developers' Forum
    10 Posts 3 Posters 1.5k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • PixeroP Offline
      Pixero
      last edited by

      How do I use a sequence of keys in ruby?
      I've tried with:
      if (key == 18 && key == 38) # Alt and Up Arrow
      and:
      if (ALT_MODIFIER_KEY && key == 38)
      and:
      if (key == ALT_MODIFIER_KEY && key == 38)

      1 Reply Last reply Reply Quote 0
      • R Offline
        RickW
        last edited by

        In a tool, when you define onKeyDown, it has a bunch of parameters:
        def onKeyDown(key, repeat, flags, view)

        The "flags" parameter will be a numerical value representing various events or combinations of events. The values are bit-coded and can be added for combined events:
        1 - Left MB
        2 - Right MB
        4 - SHIFT key
        8 - CTRL key
        16 - ???
        32 - ALT key

        So, CTRL+SHIFT+ALT would be a flag value of 44 (8+4+32)

        In your example, then, you want to check if flags==32 and key==38

        RickW
        [www.smustard.com](http://www.smustard.com)

        1 Reply Last reply Reply Quote 0
        • PixeroP Offline
          Pixero
          last edited by

          Thanks for your help, but it still won't work for me.
          Here is what I've done (a bit shorter than the real code):

          def onKeyDown(key, repeat, flags, view)
           if (flags == 32 && key == 38) # Alt + Up
            dist = Geom;;Point3d.new [0, 0, 100]; 
           end #if
          
          # Now move it	
          tr = Geom;;Transformation.new (dist);
          Sketchup.active_model.entities.transform_entities(tr, e);		
              
          end # onKeyDown
          
          1 Reply Last reply Reply Quote 0
          • T Offline
            todd burch
            last edited by

            In my nudge script, I check for either the Shift Key or the Control key. I use the onKeyDown and onKeyUp methods to test for either key. When either key is pressed, I set a corresponding true toggle for that key. When it's raised, I set the toggle back to false.

            Finally, when I'm looking for the arrow keys, I test for true for either toggle and adjust the move distance accordingly. For instance, SHIFT+any arrow key is a 10X movement in distance.

            Todd

            1 Reply Last reply Reply Quote 0
            • PixeroP Offline
              Pixero
              last edited by

              Thanks I'll try that.

              1 Reply Last reply Reply Quote 0
              • T Offline
                todd burch
                last edited by

                Yeah, in the onKeyDown method, the keys are not OR'ed.

                You'll figure it out. Again, I recommend adding a "puts" statement as the first instruction in your methods to show the key code and you'll what see gets passed when.

                Todd

                1 Reply Last reply Reply Quote 0
                • PixeroP Offline
                  Pixero
                  last edited by

                  YES! That worked!!!!

                  Thanks a lot.

                  As a side note. Do you have the keycodes for Mac?
                  On my laptop (Windows) the keycodes are:
                  up = 38
                  left = 37
                  right = 39
                  down = 40
                  alt = 18
                  shift = 16
                  ctrl = 17

                  1 Reply Last reply Reply Quote 0
                  • T Offline
                    todd burch
                    last edited by

                    Ignore any logic you see - this is from an old copy of a script. They keycodes are good though, for both Panther and Tiger, and I suspect they will be the same for Leopard too.

                    BTW, in OS X "Tiger" 10.4.10, the RUBY_PLATFORM constant is "fat-darwin8.8.0". If you check for "darwin" that should be good enough, thru Tiger anyways.

                    Todd

                    
                    if !defined? KNUDGEOK then 
                      if RUBY_PLATFORM == "i386-mswin32" then                 # Win XP  
                        KALXMINUS = 37          # Arrow Left Key 
                        KARXPLUS  = 39          # Arrow Right Key 
                        KAUYPLUS  = 38          # Arrow Up Key 
                        KADYMINUS = 40          # Arrow Down Key 
                        KLTZDOWN  = 188         # (comma) Less Than Key 
                        KGTZUP    = 190         # (period) Greater Than Key 
                        KSHIFT    = 16          # Shift Key 
                        KCONTROL  = 17          # Control Key 
                        KNUDGEOK = true ; 
                      elsif RUBY_PLATFORM == "powerpc-darwin" then            # Mac OSX Panther 
                        KALXMINUS = 63234       # Arrow Left Key 
                        KARXPLUS  = 63235       # Arrow Right Key 
                        KAUYPLUS  = 63232       # Arrow Up Key 
                        KADYMINUS = 63233       # Arrow Down Key 
                        KLTZDOWN  = 44          # (comma) Less Than Key 
                        KGTZUP    = 46          # (period) Greater Than Key 
                        KSHIFT    = 131072      # Shift Key 
                        KCONTROL  = 262144      # Control Key 
                        KNUDGEOK = true ; 
                      else 
                        KPLATFORM = "?" 
                        end 
                      end ; 
                    
                    
                    1 Reply Last reply Reply Quote 0
                    • PixeroP Offline
                      Pixero
                      last edited by

                      Again, thanks.
                      The only one missing is the Alt key.

                      1 Reply Last reply Reply Quote 0
                      • T Offline
                        todd burch
                        last edited by

                        Oops.

                        Command = 1048576
                        Alt = 524288
                        Control = 262144

                        The Command key, also known as the "apple" key, shown as a ⌘ in the Doc, is the Mac equivalent to the PC's CONTROL key. The control key on a Mac isn't used that often.

                        The ALT key is also named the Option key.

                        Todd

                        1 Reply Last reply Reply Quote 0
                        • 1 / 1
                        • First post
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement