• Login
sketchucation logo sketchucation
  • Login
Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
🫛 Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

Key sequence in ruby

Scheduled Pinned Locked Moved Developers' Forum
10 Posts 3 Posters 1.6k 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.
  • P Offline
    Pixero
    last edited by 28 Nov 2007, 00:17

    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 28 Nov 2007, 00:18

      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
      • P Offline
        Pixero
        last edited by 28 Nov 2007, 00:18

        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 28 Nov 2007, 00:19

          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
          • P Offline
            Pixero
            last edited by 28 Nov 2007, 00:19

            Thanks I'll try that.

            1 Reply Last reply Reply Quote 0
            • T Offline
              todd burch
              last edited by 28 Nov 2007, 00:19

              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
              • P Offline
                Pixero
                last edited by 28 Nov 2007, 00:20

                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 28 Nov 2007, 00:20

                  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
                  • P Offline
                    Pixero
                    last edited by 28 Nov 2007, 00:21

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

                    1 Reply Last reply Reply Quote 0
                    • T Offline
                      todd burch
                      last edited by 28 Nov 2007, 00:21

                      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
                      1 / 1
                      • First post
                        5/10
                        Last post
                      Buy SketchPlus
                      Buy SUbD
                      Buy WrapR
                      Buy eBook
                      Buy Modelur
                      Buy Vertex Tools
                      Buy SketchCuisine
                      Buy FormFonts

                      Advertisement