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

    [plugin] Updated (Dec. 30, 2011) hook Procedure

    Scheduled Pinned Locked Moved Plugins
    3 Posts 2 Posters 4.8k Views 2 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.
    • A Offline
      Anton_S
      last edited by Anton_S

      First, if you have downloaded HookProc plugin, then delete MSPhysics folder, myInputProc.rb file, and rbconfig.rb - if its version is higher, than sketchup's ruby core version.

      Mouse and Keyboard Input Procedure

      Introduction:
      Input Procedure allows the user to monitor and make decistions for mouse and keyboard input messages that are sent to sketchup thread. For instance, lets say you want to write a plugin that would use key 'a' to do some task.You can do that by getting input in onKeyDown method. Though there is one thing: Key 'a' is a shortcut key to an arc tool - when you press key 'a' your tool deactivates, and the arc tool activates. How can you make it so that key 'a' doesn't interfere with arc tool? - Well there are two answers I know of:

      1st Answer: Go to shortcuts menu and remove key 'a' from being a shortcut to some tool.
      2nd Answer: Use hook procedure, simple as I know of.

      1st answer is not good! If you wanted to set shortcuts back, you would have take some time for doing that, and if you wanted to upload a plugin to internet, so that others could use, before activating your plugin they would also have to remove shortcuts, and after deactivating, set tool shortcuts back. I think that's just a waste of time and no one would want to do that, so cross the first answer off and we're left with 2nd.

      So anyways, what is Hook Procedure?
      "A hook is a point in the system message-handling mechanism where an application can install a subroutine to monitor the message traffic in the system and process certain types of messages before they reach the target window procedure."

      Its not only capable of monetering messages, its also capable to prevent them to be sent to the window procedure, and this is what we wan't! We could prevent key 'a' from intefering with sketchup window, and still get its input.

      Usage:
      The usage is a bit similar to sketchup tool.
      First, you'll need to write a class. The class should have some or all of the following methods:

      ` onKeyboardInput(key_number, key, state)`
      

      key_number - a virtual key code to the virtual key
      key - virtual key
      state - the state of the key:

      0 - on key down
      1 - key is down
      (-1) - on key up
      The return value should be:

      0 - let the message interfere with sketchup - process the message
      1 - prevent the message from intefering with sketchup
      Reference to virtual key codes: http://msdn.microsoft.com/en-us/library/dd375731(v=vs.85.aspx
      The assigned virtual keys are in the VirtualKeyCodes.rb file.

      ` onMouseInput(msg_number, msg, x, y)`
      

      msg_number - the number to the constant of the message
      msg - the message
      x, y - real cursor position
      The return value should be:

      0 - process the message
      1 - prevent the message from intefering with sketchup
      Reference to mouse messages: http://msdn.microsoft.com/en-us/library/windows/desktop/ms645533(v=vs.85.aspx
      The assigned message names are in the mouse input.rb file

      ` onMouseWheelRotation(d, x, y)`
      

      d - rotation direction: 1 - forward, (-1) - backward
      x, y - real cursor position
      The return value should be:

      0 - process the message
      1 - prevent the message from intefering with sketchup

      Example:
      ` require "MSPhysics/InputProc.rb"

      class MyInputTool

      def initialize()
      end

      def onKeyboardInput(key_number, key, state)
      

      puts "OnKeyboardInput: key_number #{key_number}, key #{key}, state #{state}"
      1 #remove the message
      end

      def onMouseInput(msg_number, msg, x, y)
      

      puts "OnMouseInput: msg_number #{msg_number}, msg #{msg}, cursor position (#{x}, #{y})"
      0 #process the message
      end

      def onMouseWheelRotation(d, x, y)
      

      puts "OnMouseWheelRotate: direction #{d}, cursor position (#{x}, #{y})"
      1 #remove the message
      end
      end

      $myInputTool = MyInputTool.new`
      To hook the instance of the class we first need to select the hook, similar to selecting the tool:

      MSPhysics::InputProc.select_hook($myInputTool)

      Now, to hook it (Or turn it on):

      ` MSPhysics::InputProc.hook(input function name(s))`
      You can hook one or multiple of procedures in one time.
      Example:
      	` MSPhysics::InputProc.hook("onMouseInput")`
      				or/and multiple procedures
      	` MSPhysics::InputProc.hook("onMouseInput", "onKeyboardInput", "onMouseWheelRotation")`
      
      You don't have to write them exactly the same, you can make it lowercased or capitalized and whatever... Ex: ("ONKeyboardinput", "onmousewheelrotation")
      
      You can also hook all:
      The function will automatically find the hook input methods that are in your instance class and hook them.
      ` MSPhysics::InputProc.hook_all()`
      

      Now, to unhook it (Or turn it off):

      ` MSPhysics::InputProc.unhook(input function name(s))
      MSPhysics::InputProc.unhook_all()`
      		or
      ` MSPhysics::InputProc.select_hook(nil or other tool)`
      When you select a different hook or nil, it will automatically unhook all and then select a different tool.
      If you select the same tool, it will just return, so don't get bothered by selecting it multiple times.
      

      You can also hook the procedures, that are already hooked. It will just return, so don't get bothered by hooking same procedures multiple times.
      The same thing is with the unhook, hook_all, and unhook_all.
      The errors and other stuff will be reported to the Ruby Console

      To get the status of procedures, call the method status:
      MSPhysics::InputProc.status()
      It will return the hash of all three input proceduers with their status. (true - on, false - off)

      If it finds an error in your script, it will report it in the Ruby Console and unhook the procedure in which the error occured.
      You can toggle between hooking and unhooking messages as many times as want to.

      The example is in the InputProcExample.rb file.

      Sketchup Version:
      Tested and works in SU7 and SU8

      Operating System:
      Windows - XP/Vista/7

      Footprint: (Edited)
      InputProcExample.rb
      MSPhysics/

      %(#0000FF)[core.rb
      	InputProc.rb
      	InputProc Readme.rtf
      	mouse input.rb
      	VirtualKeyCodes.rb]
      	%(#FF0000)[win32/]%(#00FF00)[version 1.4.8 - [http://rubygems.org/gems/win32-api](http://rubygems.org/gems/win32-api)]
      

      ruby18/

      api.so
      ruby19/

      api.so
      api.rb

      Requirements: (Edited)
      none 😄


      Edit: This Post is old and outdated. If you're interested in implementing Hook Procedure into your tool, use AMS Library.

      1 Reply Last reply Reply Quote 0
      • A Offline
        Anton_S
        last edited by

        Its very useful for making games in SU.
        You can use it in sketchy physics for windows...

        1 Reply Last reply Reply Quote 0
        • mtripleM Offline
          mtriple
          last edited by

          im trying to fix sketchy physics for sketchup 32 2014 based on 64 os , i think i did good so far but the plugin still dont work, the tools appear , but Errors comes up :::
          i think its something with misunderstanding of os type :

          Error Loading File sketchyphysics.rb
          Error; #<RuntimeError; can't find the symbol `init'>
          C;/Program Files (x86)/SketchUp/SketchUp 2014/Tools/SketchyPhysics3/dl/import.rb;126;in `symbol'
          C;/Program Files (x86)/SketchUp/SketchUp 2014/Tools/SketchyPhysics3/dl/import.rb;145;in `import'
          C;/Program Files (x86)/SketchUp/SketchUp 2014/Tools/SketchyPhysics3/dl/import.rb;61;in `extern'
          C;/Program Files (x86)/SketchUp/SketchUp 2014/Tools/SketchyPhysics3/newton.rb;95;in `<module;NewtonServer>'
          C;/Program Files (x86)/SketchUp/SketchUp 2014/Tools/SketchyPhysics3/newton.rb;84;in `<top (required)>'
          C;/Program Files (x86)/SketchUp/SketchUp 2014/Tools/sketchyphysics.rb;27;in `load'
          C;/Program Files (x86)/SketchUp/SketchUp 2014/Tools/sketchyphysics.rb;27;in `LoadSketchyPhysics3'
          C;/Program Files (x86)/SketchUp/SketchUp 2014/Tools/sketchyphysics.rb;62;in `doLoad'
          C;/Program Files (x86)/SketchUp/SketchUp 2014/Tools/sketchyphysics.rb;138;in `<top (required)>'
          

          hope u can help me

          compospray workin 2014

          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