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.
    • J Offline
      Jim
      last edited by

      Can anyone explain to me the flags argument for the onKeyDown method. I had assumed it would have a certain, predictable value when the one of the modifier keys was being help down, but it doesn't make any sense to me.

      I am trying to avoid code like the following:

      
       if key == VK_SHIFT
         @shift = true
       end
       if key == VK_CONTROL
         @ctrl = true
       end
      
      

      Hi

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

        The flags are (as best as I could ever tell)

        1 = left mouse button down
        2 = right mouse button down
        4 = shift down
        8 = ctrl down
        16 = ?
        32 = alt down

        Then add them together to determine if the user has more than one key down. So shift + ctrl would give a flag of 12. Alt + ctrl gives a flag of 40. All of them down together give a flag of 47 (not including 16. I can't find 16, so I can't press it).

        Now, I don't know how to use that info in a script though. Sorry, I probably didn't help very much. Can you think of a good way to examine the flag 47 and decide what keys that means are being held down?

        Chris

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

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

          Well, it's a bit field. So using your information...

          
          47.to_s(2)
          101111
          
          

          Each position in the binary would represent a key state.

          
          1 - left mouse on
          0 - right mouse off
          1 - shift on
          1 - ctrl on
          1 - ?
          1 - alt on
          
          

          But my problem is the flag value does not change if I have the SHIFT key down or not, when I think it should. Maybe I'm making bad assumptions.

          Hi

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

            didn't know you could convert to binary that way. neat.

            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

              Oh, here's a thought. onKeyDown/Up use the "key" argument, while the others (onMouseMove, onRButtonDown, etc) use the "flag" argument. So with onkeyup, its not really a flag, but a keycode or keyID, whereas the other methods return a flag using the system I described above.

              Don't know how that changes things....

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

              1 Reply Last reply Reply Quote 0
              • 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
                                            • 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