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

    Flags on onKeyDown?

    Scheduled Pinned Locked Moved Developers' Forum
    21 Posts 8 Posters 1.8k Views 8 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.
    • T Offline
      tomasz
      last edited by

      What would be the best way to read the CTRL key in Win and its corresponding key on MAC. I am writing about a case without creating a new tool.

      I would like to read a state of CTRL key when an user presses a toolbar icon.

      Tomasz

      Author of [Thea Render for SketchUp](http://www.thearender.com/sketchup)

      1 Reply Last reply Reply Quote 0
      • W Offline
        wikii
        last edited by

        if (flags & 4)==4
        shift=true
        end

        1 Reply Last reply Reply Quote 0
        • J Offline
          Jim
          last edited by

          Ah, so maybe that is what these constants are for?

          
          ["CONSTRAIN_MODIFIER_KEY", "CONSTRAIN_MODIFIER_MASK", "ALT_MODIFIER_KEY", "ALT_MODIFIER_MASK", "COPY_MODIFIER_KEY", "COPY_MODIFIER_MASK"]
          
          

          Hi

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

            @unknownuser said:

            .. I am writing about a case without creating a new tool.

            @jim said:

            ["CONSTRAIN_MODIFIER_KEY", "CONSTRAIN_MODIFIER_MASK", "ALT_MODIFIER_KEY", "ALT_MODIFIER_MASK", "COPY_MODIFIER_KEY", "COPY_MODIFIER_MASK"]

            Actually creating a tool in not a big deal, but I thought there is a way to omit that.
            In SU2KT there is no need for creating the tool and all necessary methods to handle it.
            I just need to read a state of CTRL\MENU key upon the start-up of the plugin.

            Author of [Thea Render for SketchUp](http://www.thearender.com/sketchup)

            1 Reply Last reply Reply Quote 0
            • J Offline
              Jim
              last edited by

              Can't be done, as far as I know. I agree it would be nice.

              Hi

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

                class SU2KT_key
                
                	def SU2KT_key;;key_pressed?
                		@key
                	end
                
                	def SU2KT_key;;activate
                		@key=false
                	end
                
                	def SU2KT_key;;onKeyDown(key, repeat, flags, view)
                		@key=(key==COPY_MODIFIER_KEY)
                		puts @key
                	 end
                
                 end
                
                
                keytool=SU2KT_key.new
                Sketchup.active_model.select_tool SU2KT_key
                sleep 1
                puts SU2KT_key.key_pressed?
                

                Why does it always give me 'false' on reload of the script? It looks that SU doesn't check the keyboard on creation of a new tool, otherwise I would get the @key value BEFORE sleep 1. Am I right?

                Thanks
                Tomasz

                Author of [Thea Render for SketchUp](http://www.thearender.com/sketchup)

                1 Reply Last reply Reply Quote 0
                • fredo6F Offline
                  fredo6
                  last edited by

                  @unknownuser said:

                  I just need to read a state of CTRL\MENU key upon the start-up of the plugin.

                  Did you try with the event OnMouseMove, which has a flags argument too?

                  1 Reply Last reply Reply Quote 0
                  • B Offline
                    BTM
                    last edited by

                    @unknownuser said:

                    @unknownuser said:

                    I just need to read a state of CTRL\MENU key upon the start-up of the plugin.

                    Did you try with the event OnMouseMove, which has a flags argument too?

                    … This is a bit off-topic, but on windows
                    ` class Flagtest

                    def onMouseMove(flags,x,y,view)
                    UI.messagebox flags
                    end
                    end

                    Sketchup.active_model.select_tool Flagtest.new`
                    will come up with 0 on mouse move, if no keys are pressed. on my mac, it's 256. Any idea why?

                    update:
                    Also, onMouseMove only activates when the mouse moves in the model area, so it wouldn't activate on startup of the plugin.

                    1 Reply Last reply Reply Quote 0
                    • Chris FullmerC Offline
                      Chris Fullmer
                      last edited by

                      ok, I am using the "flags" parameter of the onLButtonUp method to determine ctrl, alt key status. This is my code so far. It works, but I have a feeling it could be optimized. I really don't manipulate strings very well. I nearly turned it all into an array.

                      ` def onLButtonUp(flags, x, y, view)

                      flag_bin = flags.to_s(2)
                      flag_bin_to_add = 6 - flag_bin.length
                      zeros = "0"*flag_bin_to_add
                      adjusted_flag = (zeros + flag_bin).reverse

                      ctrlk = true if adjusted_flag[3,1] == "1"
                      altk = true if adjusted_flag[5,1] == "1"`

                      So I take the flag, turn it into binary as Jim showed. But a value less than 32 makes a binary with less than 6 digits in it. So if the user is holding alt, then a 6 digit binary is given. If they are holding ctrl, then only a 4 digit binary is given. So I am adding zeros to the binary string, then reversing it because I like the binary to be ordered according to the numerical representations for each key, lowest number key on the left, highest number key on the right.

                      Then I set my variables to be true if their binary digit is true, indicating that their key was pressed.

                      I hope that makes sense. Again, this seems to work great. But it feels klunky. Is there an easier way to parse the bit field?

                      Any optimization ideas?

                      Chris

                      ps - I tacked it onto this thread since this thread is what pointed me in the direction of how to work with the "flags" parameter.

                      Lately you've been tan, suspicious for the winter.
                      All my Plugins I've written

                      1 Reply Last reply Reply Quote 0
                      • thomthomT Offline
                        thomthom
                        last edited by

                        shift = (flags & CONSTRAIN_MODIFIER_MASK) == CONSTRAIN_MODIFIER_MASK
                        

                        Use the & for bitwise comparison.

                        Thomas Thomassen — SketchUp Monkey & Coding addict
                        List of my plugins and link to the CookieWare fund

                        1 Reply Last reply Reply Quote 0
                        • Chris FullmerC Offline
                          Chris Fullmer
                          last edited by

                          I don't quite understand what that means. What is bitwise?

                          Lately you've been tan, suspicious for the winter.
                          All my Plugins I've written

                          1 Reply Last reply Reply Quote 0
                          • thomthomT Offline
                            thomthom
                            last edited by

                            Link Preview Image
                            Bitwise operation - Wikipedia

                            favicon

                            (en.wikipedia.org)

                            Thomas Thomassen — SketchUp Monkey & Coding addict
                            List of my plugins and link to the CookieWare fund

                            1 Reply Last reply Reply Quote 0
                            • thomthomT Offline
                              thomthom
                              last edited by

                              Nice little list of Ruby's operators http://www.tutorialspoint.com/ruby/ruby_operators.htm

                              Thomas Thomassen — SketchUp Monkey & Coding addict
                              List of my plugins and link to the CookieWare fund

                              1 Reply Last reply Reply Quote 0
                              • M Offline
                                MartinRinehart
                                last edited by

                                @chris fullmer said:

                                flag_bin = flags.to_s(2) flag_bin_to_add = 6 - flag_bin.length zeros = "0"*flag_bin_to_add adjusted_flag = (zeros + flag_bin).reverse

                                Stick in a non-zero bit to the left of the flags.

                                Quickly: (flags | 128).to_s(2)[2..7].reverse

                                Slowly:

                                flags 
                                        # 00bbbbbb
                                flags | 128 
                                        # 10bbbbbb
                                (flags | 128).to_s(2)
                                        # 8-char string version
                                (flags | 128).to_s(2)[2..7] 
                                        # the 6 chars your really want
                                
                                

                                Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

                                1 Reply Last reply Reply Quote 0
                                • thomthomT Offline
                                  thomthom
                                  last edited by

                                  Converting to string is very inefficient.

                                  Thomas Thomassen — SketchUp Monkey & Coding addict
                                  List of my plugins and link to the CookieWare fund

                                  1 Reply Last reply Reply Quote 0
                                  • J Offline
                                    Jim
                                    last edited by

                                    @chris fullmer said:

                                    I don't quite understand what that means. What is bitwise?

                                    Think in binary, then compare them column for column. It's a logical, bit-to-bit comparison.

                                    
                                    5 in binary is 101
                                    6 in binary is 110
                                    
                                    5 & 6 is; logical AND each column (bit position)
                                    
                                        Columns
                                    ________________
                                    |  1  |  0  |  1   <- decimal 5
                                    | and | and | and
                                    |  1  |  1  |  0   <- decimal 6
                                    =================
                                       1     0     0 
                                    
                                    so 101 & 110 = 100 <- decimal 4
                                    
                                    

                                    The masks are constants, which are set to some convenient values:

                                    
                                    # I just made these up
                                    001 = CONSTRAIN_MASK
                                    010 = ALT_MASK
                                    100 = COPY_MASK
                                    
                                    

                                    So if the flag is decimal value 7:

                                    
                                    111  <- decimal 7
                                    
                                    

                                    In order to check the CONSTRAIN_MASK, you logical AND the CONSTRAIN_MASK with the flag value:

                                    
                                        111  <- flag value decimal 7
                                      & 001  <- contain mask constant
                                       ======
                                        001 <- CONSTRAIN_MASK set true
                                    
                                    or in real ruby as Thomas wrote;
                                    
                                    contrain_key_down = ((flags & CONSTRAIN_MODIFIER_MASK) == CONSTRAIN_MODIFIER_MASK)
                                    copy_key_down = ((flags & COPY_MODIFIER_MASK) == COPY_MODIFIER_MASK)
                                    alt_key_down = ((flags & ALT_MODIFIER_MASK) == ALT_MODIFIER_MASK)
                                    
                                    

                                    Although Adam B might know a clever way to decode them all in one elegant statement.

                                    Hi

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

                                    Advertisement