sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Detect Ctrl outside a Tool

    Scheduled Pinned Locked Moved Developers' Forum
    15 Posts 4 Posters 1.7k Views 4 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

      Is there a way to detect if the Ctrl or Shift keys are pressed when clicking a toolbar button?

      Hi

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

        I don't think so - not with the Ruby API.

        (But everytime I say "you can't", TBD comes up with an ingenious solution for Windows... πŸ˜‰ )

        1 Reply Last reply Reply Quote 0
        • C Offline
          CPhillips
          last edited by

          Sorry TBD. Beat you to it.

          
          require 'sketchup.rb'
          require 'Win32API' 
          
          $win32GetKeyStateFunc=Win32API.new("user32.dll", "GetKeyState", ['N'], 'N')
          KEY_LSHIFT=0xa0
          KEY_LCONTROL=0xa2
          
          def getKeyState(key)
              return (($win32GetKeyStateFunc.call(key)>>16)!=0)
          end
          #USAGE;
          #if(getKeyState(KEY_LCONTROL)) 
          #do something
          #end
          
          
          1 Reply Last reply Reply Quote 0
          • J Offline
            Jim
            last edited by

            Thanks, Chris.

            Todd,

            Is there is any Mac equivalent to Win32API?

            Hi

            1 Reply Last reply Reply Quote 0
            • C Offline
              CPhillips
              last edited by

              Ruby/DL does the same thing and works on Mac. But I don't know if there is a Mac equivalent to user32.dll.

              I do have a Mac dylib that reads keys and passes it to Ruby. Let me ask the author if it is ok to post it here.

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

                Strange!
                I get an "unknown" error when loading Getkeys.rb.
                Difficult to tell more, as whether by opening Sketchup or loading the file via the Ruby console, the error is desperatly "unknown".

                Otherwise, I would suggest that the community of Ruby developers tries to come up with a uniform solution to complement some missing functiosn of the Ruby API. If this DL programs works both on Windows and Mac, then it is really a good base. We could package it as a library, and a few of us in charge of updating it.

                Then, we could develop a few other functions that could be useful and probably require to go down to the System:

                • Move the mouse cursor to a given position
                • Intercept keys before Sketchup (when in a tool, all the short cuts like Space, 'L', etc..., are actually trapped by Sketchup, before they even reach the tool OnKeyDown method
                • Access to the main loop in order to be able to interrupt a lengthy process by typing a key
                  Fredo
                1 Reply Last reply Reply Quote 0
                • C Offline
                  CPhillips
                  last edited by

                  Here is some Mac code to read the keyboard. It was written by Kevin Willey and he gave me permission to post it here. I don't have a Mac so I can't test it.
                  Although Ruby/DL works on Windows and Mac this ruby code will only work on Mac.

                  
                  #Ruby code that will get the keyboard State on Mac.
                  #Written by Kevin Wiley
                  #requires RubyDL to load the GetKeys.dylib
                  require 'sketchup.rb'
                  require "dl"
                  require "dl/import"
                  
                  module GetKeys
                     extend DL;;Importable
                     dlload(Sketchup.find_support_file("GetKeys.dylib","plugins/"))
                     extern "BOOL TestForKeyDown(short)"
                  
                     def GetKeys.getKeyState(key)
                        puts(key)
                        if GetKeys.testForKeyDown(key) then
                           puts(key.to_s + "key is down")
                           return(true)
                        else
                           puts(key.to_s + "key is not down")
                           return(false)
                        end
                     end
                  
                  end
                  #USAGE
                  #GetKeys.getKeyState(0x03B) whenever you want to check for control key and
                  #GetKeys.getKeyState(0x038) whenever you want to check for shift
                  
                  

                  It uses Ruby/DL to load the Mac dylib (attached). Ruby/DL (http://ttsky.net/ruby/ruby-dl.html) does the same thing as Win32api.so. And it rocks. Not only is it cross platform but it is much easier to use and more powerful. It even allows a ruby function to be used a as a C callback.

                  I have packaged up the Windows and Mac 0SX Ruby/DL and attached it. The .so file is Win and the .bundle file is Mac. Place it in the plugins/ folder.


                  GetKeys.zip


                  RubyDL-Win32andMacOSX.zip

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

                    I have them all, and in the right place as specified in the Zip file.
                    I hope you find the issue, so that we can educate the approach.

                    1 Reply Last reply Reply Quote 0
                    • C Offline
                      CPhillips
                      last edited by

                      @unknownuser said:

                      Strange!
                      I get an "unknown" error when loading Getkeys.rb.
                      Difficult to tell more, as whether by opening Sketchup or loading the file via the Ruby console, the error is desperatly "unknown".

                      Otherwise, I would suggest that the community of Ruby developers tries to come up with a uniform solution to complement some missing functiosn of the Ruby API. If this DL programs works both on Windows and Mac, then it is really a good base. We could package it as a library, and a few of us in charge of updating it.

                      Then, we could develop a few other functions that could be useful and probably require to go down to the System:

                      • Move the mouse cursor to a given position
                      • Intercept keys before Sketchup (when in a tool, all the short cuts like Space, 'L', etc..., are actually trapped by Sketchup, before they even reach the tool OnKeyDown method
                      • Access to the main loop in order to be able to interrupt a lengthy process by typing a key
                        Fredo

                      First you are trying this on a Mac right? I wasn't clear in the orginial post that this code only Works on Mac because I dont have a windows version of GetKeys.dylib.

                      If you are running Mac that "Unknown" error happens when something goes wrong loading the either the DL.Bundle or the GetKeys.dylib. Make sure you have it like this.

                      plugins/getkeys.rb
                      plugins/getkeys.dylib
                      plugins/dl.bundle
                      plugins/dl/import.rb
                      etc.

                      I am sure Ruby/DL does work on both Mac and windows. I may not have packaged up the Ruby/dl quite right. The zip file I received had also had a "DL" file that was 0 bytes long.

                      I think the idea of creating a cross platform library is awesome! I would definitely contribute.

                      1 Reply Last reply Reply Quote 0
                      • C Offline
                        CPhillips
                        last edited by

                        @unknownuser said:

                        I have them all, and in the right place as specified in the Zip file.
                        I hope you find the issue, so that we can educate the approach.

                        Are you trying it on Mac or Windows?

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

                          I am running Windows XP on a regular laptop.
                          I am still running Sketchup 6.0.515, in case this does matter.

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

                            I would like to help out on this. I have both a Mac and a Windows machine and am set up for development on both, but I can't focus on this until after 3DBC.

                            1 Reply Last reply Reply Quote 0
                            • C Offline
                              CPhillips
                              last edited by

                              @unknownuser said:

                              I am running Windows XP on a regular laptop.
                              I am still running Sketchup 6.0.515, in case this does matter.

                              Ah. That is probably the problem. I didn't make it clear in my original post that this code is Mac only because I dont have a Win version of GetKeys.dylib. Use the first code I posted on Windows.

                              1 Reply Last reply Reply Quote 0
                              • C Offline
                                CPhillips
                                last edited by

                                @unknownuser said:

                                I would like to help out on this. I have both a Mac and a Windows machine and am set up for development on both, but I can't focus on this until after 3DBC.

                                Can you test the GetKeys code I posted on Mac? I want to make sure it works.

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

                                  Too bad, as I thought we had a method to provide a utility libraries based on operating system, but portable between PC and Mac.
                                  I guess this is possible, but we just need to package to offer the same interface, even with different code and binaries underneath

                                  Thanks anyway for this contribution. I'll try to have a closer look whenever I have some time left.

                                  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