# Tool Observer method onToolStateChanged invoke twice

**URL:** <https://forums.sketchup.com/t/tool-observer-method-ontoolstatechanged-invoke-twice/233243>\
**Category:** Ruby API\
**Created:** [June 20, 2023, 11:30am UTC](https://forums.sketchup.com/t/tool-observer-method-ontoolstatechanged-invoke-twice/233243 "2023-06-20T11:30:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RafaFiedo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/rafafiedo/32/198710_2.png) [@RafaFiedo](https://forums.sketchup.com/u/RafaFiedo)\
**Post date:** [June 20, 2023, 11:30am UTC](https://forums.sketchup.com/t/tool-observer-method-ontoolstatechanged-invoke-twice/233243/1 "2023-06-20T11:30:53Z")

</div>

I am using a simple observer for tools. Code below.

I noticed that when I move object method invoked 3 times.  
1 time when I grab the object  
2 times when I leave an object.

Unfortunately, the 2 times when I leave the object I have tool\_state == 0 so I have no idea how I can recognize that 'one time

My task is to run a function of everything when the user ‘leaves’ the grabbed object. Would be nice to avoid static/classes/global variables to do this. Any thoughts?

At first, I thought that I added observer twice, but the grabbing object invoke only once (tool\_state == 1)

```auto

class MyToolsObserver < Sketchup::ToolsObserver

  def onActiveToolChanged(tools, tool_name, tool_id)
    puts "onActiveToolChanged: #{tool_name}"
  end

  def onToolStateChanged(tools, tool_name, tool_id, tool_state)
    puts "onToolStateChanged: #{tool_id} - #{tool_name}:#{tool_state}"
 end

end

```

---

<div class="post-metadata">

**Author:** ![RafaFiedo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/rafafiedo/32/198710_2.png) [@RafaFiedo](https://forums.sketchup.com/u/RafaFiedo)\
**Post date:** [June 20, 2023, 12:46pm UTC](https://forums.sketchup.com/t/tool-observer-method-ontoolstatechanged-invoke-twice/233243/2 "2023-06-20T12:46:22Z")

</div>

I think I found a way by using the previous state tool:  
Used a object variable  
Maybe somebody will search that in future

Here is updated code:

```auto
class MyToolsObserver < Sketchup::ToolsObserver

  attr_accessor :last_state

  def onActiveToolChanged(tools, tool_name, tool_id)
    puts "onActiveToolChanged: #{tool_name}"
  end

  def onToolStateChanged(tools, tool_name, tool_id, tool_state)
    puts "onToolStateChanged: #{tool_id} - #{tool_name}:#{tool_state}"

    if tool_id == 21048 && tool_state == 0 && @last_state == 1
      logic_code()
      puts "INVOKE MOVE TOOL"
    end

    @last_state = tool_state
 end

end

```

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [June 20, 2023, 1:17pm UTC](https://forums.sketchup.com/t/tool-observer-method-ontoolstatechanged-invoke-twice/233243/3 "2023-06-20T13:17:42Z")

</div>

> [@RafaFiedo](#):
>
> I think I found a way by using the previous state tool:

Needing to keep track of state is quite common with the SketchUp API.

> [@RafaFiedo](#):
>
> Used a object variable

It is actually an **instance variable**.

> [@RafaFiedo](#):
>
> Unfortunately, the 2 times when I leave the object I have tool\_state == 0 so I have no idea how I can recognize that 'one time

Please read the open issue in the API Issue tracker:

- [Issue #858 - ToolsObserver.onToolStateChanged() callback description needs updating](https://github.com/SketchUp/api-issue-tracker/issues/858)

Also read about the v2016 Observer overhaul:

> **[Observers2016.pdf](https://assets.sketchup.com/files/ewh/Observers2016.pdf)**
>
> 93.72 KB

---

<div class="post-metadata">

**Author:** ![RafaFiedo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/rafafiedo/32/198710_2.png) [@RafaFiedo](https://forums.sketchup.com/u/RafaFiedo)\
**Post date:** [June 20, 2023, 1:36pm UTC](https://forums.sketchup.com/t/tool-observer-method-ontoolstatechanged-invoke-twice/233243/4 "2023-06-20T13:36:11Z")

</div>

thanks for all the extra info!

> Please read the open issue in the API Issue tracker:
> 
> - [Issue #858 - ToolsObserver.onToolStateChanged() callback description needs updating](https://github.com/SketchUp/api-issue-tracker/issues/858)

oh yea, new methods for observers would be nice!

---

<div class="post-metadata">

**Author:** ![Three\_H](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@Three\_H](https://forums.sketchup.com/u/Three_H)\
**Post date:** [April 21, 2025, 11:52am UTC](https://forums.sketchup.com/t/tool-observer-method-ontoolstatechanged-invoke-twice/233243/5 "2025-04-21T11:52:03Z")

</div>

So, how do I detect when a user has triggered a tool? I have a similar problem. I need to detect whether a user has triggered a move or zoom function. I have read the previous content. Can I refresh the queue status immediately by ending the previous function? Please help me.：（

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [April 21, 2025, 6:49pm UTC](https://forums.sketchup.com/t/tool-observer-method-ontoolstatechanged-invoke-twice/233243/6 "2025-04-21T18:49:57Z")

</div>

> [@Three\_H](#):
>
> So, how do I detect when a user has triggered a tool? I have a similar problem. I need to detect whether a user has triggered a move or zoom function.

Your only option is to use the `onActiveToolChanged` callback and test for the tool IDs when they equal that for the Move tool (`21048`) and the Zoom tool (`10509`).

> [@Three\_H](#):
>
> Can I refresh the queue status immediately by ending the previous function?

I do **not** know what a “queue status” is. This is not documented for the API.

What do you mean by “ending the previous function” ?

---

<div class="post-metadata">

**Author:** ![JackyM](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@JackyM](https://forums.sketchup.com/u/JackyM)\
**Post date:** [August 29, 2025, 3:58pm UTC](https://forums.sketchup.com/t/tool-observer-method-ontoolstatechanged-invoke-twice/233243/7 "2025-08-29T15:58:02Z")

</div>

Closed on August 29, 2025, 3:58pm UTC


