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

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

Postby Anton_S » Wed Dec 21, 2011 11:43 pm

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

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.

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 ... 31(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 ... 33(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

require "MSPhysics/InputProc.rb"

class MyInputTool
    def initialize()

    def onKeyboardInput(key_number, key, state)
      puts "OnKeyboardInput: key_number #{key_number}, key #{key}, state #{state}"
      1 #remove the message

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

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

$myInputTool = MyInputTool.new

To hook the instance of the class we first need to select the hook, similar to selecting the tool:


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.
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.

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

MSPhysics::InputProc.unhook(input function name(s))

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:
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)
    InputProc Readme.rtf
    mouse input.rb

    win32/ version 1.4.8 - http://rubygems.org/gems/win32-api

Requirements: (Edited)
none :)

Edit: This Post is old and outdated. If you're interested in implementing Hook Procedure into your tool, use AMS Library.
Last edited by Anton_S on Sun Jul 06, 2014 12:34 pm, edited 3 times in total.

PluginStore Author
PluginStore Author

Re: [plugin] Updated hook Procedure

Postby Anton_S » Sat Dec 31, 2011 2:35 am

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

PluginStore Author
PluginStore Author

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

Postby mtriple » Sat Jul 05, 2014 11:52 am

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 :

Code: Select all
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
User avatar

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago

Ad Machine 


Return to Plugins

Who is online

Users browsing this forum: e2m44, FABRICIO_JOVINO, pushkarr06 and 9 guests

Visit our sponsors: