# Use the ruby code to simulate a mouse click on the sub-menu in menu

**URL:** <https://forums.sketchup.com/t/use-the-ruby-code-to-simulate-a-mouse-click-on-the-sub-menu-in-menu/71348>\
**Category:** Ruby API\
**Tags:** sketchup, ruby-api\
**Created:** [July 2, 2018, 2:34am UTC](https://forums.sketchup.com/t/use-the-ruby-code-to-simulate-a-mouse-click-on-the-sub-menu-in-menu/71348 "2018-07-02T02:34:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![haiquan](https://avatars.discourse-cdn.com/v4/letter/h/a5b964/32.png) [@haiquan](https://forums.sketchup.com/u/haiquan)\
**Post date:** [July 2, 2018, 2:34am UTC](https://forums.sketchup.com/t/use-the-ruby-code-to-simulate-a-mouse-click-on-the-sub-menu-in-menu/71348/1 "2018-07-02T02:34:02Z")

</div>

Hello, in the sketchup ruby API, can I use the ruby code to simulate a mouse click on the sub-menu in menu?  
thank you very much!

---

<div class="post-metadata">

**Author:** ![eneroth3](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/eneroth3/32/1222_2.png) [@eneroth3](https://forums.sketchup.com/u/eneroth3)\
**Post date:** [July 2, 2018, 6:23am UTC](https://forums.sketchup.com/t/use-the-ruby-code-to-simulate-a-mouse-click-on-the-sub-menu-in-menu/71348/2 "2018-07-02T06:23:48Z")

</div>

This can’t be done in Ruby alone. Maybe it can be done with OS specific libraries in other languages but I don’t think it is a good idea. Menu items can be moved or renamed and their names are language specific. What items are available, and thus their indices, also heavily depends on what is selected and has several combinations.

Rather than trying to target a part of the GUI I’d suggest to instead re-create the underlying action in Ruby.

---

<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:** [July 2, 2018, 7:26am UTC](https://forums.sketchup.com/t/use-the-ruby-code-to-simulate-a-mouse-click-on-the-sub-menu-in-menu/71348/3 "2018-07-02T07:26:44Z")

</div>

@haiquan What menu item do you want access to ?

---

<div class="post-metadata">

**Author:** ![ltc1996](https://avatars.discourse-cdn.com/v4/letter/l/858c86/32.png) [@ltc1996](https://forums.sketchup.com/u/ltc1996)\
**Post date:** [August 29, 2019, 6:18am UTC](https://forums.sketchup.com/t/use-the-ruby-code-to-simulate-a-mouse-click-on-the-sub-menu-in-menu/71348/4 "2019-08-29T06:18:55Z")

</div>

Such as ‘File - Open - choose .skp - save’. Instead of using code

```ruby
Sketchup.open_file(skp)
Sketchup.active_model.save

```

can Ruby simulate this:  
\*\* access to\*\* the menu ‘File’, then move to ‘Open’ → ‘Choose .skp’ → click Open → etc …  
Just for example, maybe he wants to slick different menu, but I think there are the same thing. Thx 🙂

---

<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:** [August 29, 2019, 2:17pm UTC](https://forums.sketchup.com/t/use-the-ruby-code-to-simulate-a-mouse-click-on-the-sub-menu-in-menu/71348/5 "2019-08-29T14:17:34Z")

</div>

I know how to do this on MS Windows …

[Search the Ruby API category on “Win32OLE SendKeys”](https://forums.sketchup.com/search?q=Win32ole%20SendKeys%20order%3Alatest_topic)

Here is one post of mine … [How to set API Export IFC Options? - #2 by DanRathbun](https://forums.sketchup.com/t/how-to-set-api-export-ifc-options/27018/2)

[For Mac search the forum for “cliclick”.](https://forums.sketchup.com/search?q=cliclick%20order%3Alatest_topic)

---

<div class="post-metadata">

**Author:** ![ltc1996](https://avatars.discourse-cdn.com/v4/letter/l/858c86/32.png) [@ltc1996](https://forums.sketchup.com/u/ltc1996)\
**Post date:** [September 3, 2019, 9:45am UTC](https://forums.sketchup.com/t/use-the-ruby-code-to-simulate-a-mouse-click-on-the-sub-menu-in-menu/71348/6 "2019-09-03T09:45:38Z")

</div>

Your code works in my su, and i understand how to send key to su.  
But when i assigned shortcut `Ctrl Alt Shift T` to `Thea Render/Create extendal model/proxy`, i can’t use this fun to change anything.

```ruby
require 'win32ole'
def test()
    shell = WIN32OLE::new("WScript.Shell")
    shell.SendKeys "^+%(T)"
end
# nothing happened...

```

I look up in [SendKeys Method](https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/8c6yea83(v=vs.84)) and i find that `Ctrl Alt Shift T` → “^+%(T)”, am i right?

Then i write this in su Ruby Panel:

```ruby
shell = WIN32OLE::new("WScript.Shell")
shell.SendKeys "^(O)"
shell.SendKeys "^(N)"

```

Since `Ctrl O` is to open select .skp window, etc…, but nothing happens. Im confused:

Did i code wrong, or i can’t send shortkey to su, or something else?

But thank you anyway !!!😀

---

<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:** [September 3, 2019, 4:56pm UTC](https://forums.sketchup.com/t/use-the-ruby-code-to-simulate-a-mouse-click-on-the-sub-menu-in-menu/71348/7 "2019-09-03T16:56:31Z")

</div>

> [@ltc1996](#):
>
> Then i write this in su Ruby Panel:

You **cannot** test SendKeys from the Ruby Console window because the console window will receive the keys instead of the SketchUp application window.

> [@ltc1996](#):
>
> I look up in [SendKeys Method](https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/8c6yea83(v=vs.84)) and i find that `Ctrl Alt Shift T` → “^+%(T)”, am i right?

Yes. (I think T should work the same as t.)

Try this …

```ruby
module LTC
  module TheaShortcut

    extend self
    require 'win32ole'

    def test()
      puts "In method 'test', sending keys CTRL+ALT+SHIFT+T ..."
      shell = WIN32OLE::new("WScript.Shell")
      shell.SendKeys "^+%(t)"
    end

    UI.add_context_menu_handler {|popup|
      popup.add_item("Test Thea Shortcut") { test() }
    }

  end
end

```

Then use the right-click context menu item to test from the main SketchUp window.

💡
