# How to set Focus to Mainwindow

**URL:** <https://forums.sketchup.com/t/how-to-set-focus-to-mainwindow/12383>\
**Category:** Ruby API\
**Created:** [June 23, 2015, 2:24pm UTC](https://forums.sketchup.com/t/how-to-set-focus-to-mainwindow/12383 "2015-06-23T14:24:28Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![gottselig\_v](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@gottselig\_v](https://forums.sketchup.com/u/gottselig_v)\
**Post date:** [June 23, 2015, 2:24pm UTC](https://forums.sketchup.com/t/how-to-set-focus-to-mainwindow/12383/1 "2015-06-23T14:24:28Z")

</div>

Hello,  
I have a UI::WebDialog. How can I set the focus on Sketchup MainWindow with a mouse click on a button in my WebDialog without closing the WebDialog?

---

<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 23, 2015, 4:14pm UTC](https://forums.sketchup.com/t/how-to-set-focus-to-mainwindow/12383/2 "2015-06-23T16:14:19Z")

</div>

> **.. in the SKUI library ?**
>
> Thomas Thomassen may have that functionality in the SKUI library.  
> [https://github.com/thomthom/SKUI](https://github.com/thomthom/SKUI)
> 
> @tt_su

* * *

## **UPDATE** - added with the 2021.1 release …

- 
## [`Sketchup::focus()`](https://ruby.sketchup.com/Sketchup.html#focus-class_method)
The [focus](https://ruby.sketchup.com/Sketchup.html#focus-class_method) method is used to focus the active model window.

This is API module method is cross-platform. The dummy dialog hack never worked on the Mac platform.

---

<div class="post-metadata">

**Author:** ![Aerilius](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Aerilius](https://forums.sketchup.com/u/Aerilius)\
**Post date:** [June 23, 2015, 5:00pm UTC](https://forums.sketchup.com/t/how-to-set-focus-to-mainwindow/12383/3 "2015-06-23T17:00:27Z")

</div>

There is no method in the API to do this.  
What is the use case (what does the button do)? Normally one does not create a button that does nothing more than move the focus, so if we knew in what context you want to do this, maybe there is a work-around or a better UX design?

---

<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 23, 2015, 5:13pm UTC](https://forums.sketchup.com/t/how-to-set-focus-to-mainwindow/12383/4 "2015-06-23T17:13:35Z")

</div>

> [@Aerilius](#):
>
> There is no method in the API to do this.

But it is a long-standing API feature request. Jim had this issue a long time ago with his button command panels (implemented as WebDialogs.)

I know of a PC workaround using either **`WIN32OLE`** or **`Win32API`** calls. But I’m at a loss for the Mac.

---

<div class="post-metadata">

**Author:** ![gottselig\_v](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@gottselig\_v](https://forums.sketchup.com/u/gottselig_v)\
**Post date:** [June 23, 2015, 5:40pm UTC](https://forums.sketchup.com/t/how-to-set-focus-to-mainwindow/12383/5 "2015-06-23T17:40:39Z")

</div>

> [@Aerilius](#):
>
> What is the use case (what does the button do)? Normally one does not create a button that does nothing more than move the focus

It starts a custom UI:Tool. I have to click in the MainWindow first for capture mousemove events.

> [@DanRathbun](#):
>
> I know of a PC workaround using either WIN32OLE or Win32API calls.

I will try it. This tool working on Windows only.  
API call would be fine.

---

<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 23, 2015, 6:14pm UTC](https://forums.sketchup.com/t/how-to-set-focus-to-mainwindow/12383/6 "2015-06-23T18:14:39Z")

</div>

No guarantees. English only. Window caption text may vary per language edition.

This code would be inside your class or module.  
The Ruby standard library must be availble with “win32ole.so” file loadable. (This is provided with SketchUp 2014+. It gets complicated with older SketchUp versions.)

The SketchUp application window caption may have changed over time, see Doc: [**`Sketchup::app_name`**](https://ruby.sketchup.com/Sketchup.html#app_name-class_method)

Could not find my old snippet, so I typed this out just a few minutes ago.

**EDIT** : **Tested and works on SU2015 Pro with “Untitled” and titled model file.**

**EDIT (2019-01-28): SketchUp 2016 and higher add a space and the year to the window caption, so the code below will need to be edited to sniff out the version and add the year to the caption string.**

```ruby

require "win32ole"

def activate_window( caption = get_caption() )
  #
  wsh = WIN32OLE.new("WScript.Shell")
  wsh.AppActivate caption
  #
end ###

def get_caption()
  #
  if Sketchup.active_model.path.empty? # new model
    caption = "Untitled - SketchUp"
  else
    filename = File.basename(Sketchup.active_model.path)
    if Sketchup.is_pro?
      caption = "#{filename} - SketchUp Pro"
    else
      caption = "#{filename} - SketchUp"
    end
  end
  #
end ###

```

💡

* * *

**Ref** : [MSDN: Windows Script Host - AppActivate Method](https://msdn.microsoft.com/en-us/library/wzcddbek%28v=vs.84%29.aspx)

---

<div class="post-metadata">

**Author:** ![Aerilius](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Aerilius](https://forums.sketchup.com/u/Aerilius)\
**Post date:** [June 23, 2015, 6:17pm UTC](https://forums.sketchup.com/t/how-to-set-focus-to-mainwindow/12383/7 "2015-06-23T18:17:44Z")

</div>

> [@gottselig\_v](#):
>
> I have to click in the MainWindow first for capture mousemove events.

Mousemove events should be captured just by hovering the main SketchUp window, independently from whether it has focus. If you mean keypresses, one can let the webdialog’s javascript listen for key events and pass them to Ruby.

Using external dependencies should not be the first means to solve a challenge/problem. Using only the official API, you can (somewhat) rely on that your extension will work in all installations of SketchUp on all platforms because SketchUp ships all you need. An external dependency (as well as unsupported methods) may fail due to anything in the environment outside of SketchUp, about which you take assumptions. It _can potentially_ give a support nightmare.

Edit: I don’t mean to steal a joy, just stay aware of the conditions under which your extension will work.  
Such assumptions can for example be string synthesis (like how the OS formats window titles), language (the above example can work on English systems) and typography (a hyphen **-** is different from a dash **–** ).

---

<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 23, 2015, 7:29pm UTC](https://forums.sketchup.com/t/how-to-set-focus-to-mainwindow/12383/8 "2015-06-23T19:29:11Z")

</div>

> [@DanRathbun](#):
>
> activate\_window

Fixed two things in the example:

Fixed the **`active_window()`** method name, was supposed to be named **`activate_window()`**.

In the second method, the application module is named **`Sketchup`** , not **`SketchUp`** , so fixed the call to the **`is_pro?`** module function.

> [@Aerilius](#):
>
> Such assumptions can for example be string synthesis (like how the OS formats window titles), language (the above example can work on English systems) and typography (a hyphen - is different from a dash –).

Which is exactly why we have begging for _for years_ for an API module function to set the SketchUp application window to have the focus, regardless of platform, language build, or SketchUp edition (Pro / Make.)

---

<div class="post-metadata">

**Author:** ![gottselig\_v](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@gottselig\_v](https://forums.sketchup.com/u/gottselig_v)\
**Post date:** [June 23, 2015, 7:39pm UTC](https://forums.sketchup.com/t/how-to-set-focus-to-mainwindow/12383/9 "2015-06-23T19:39:12Z")

</div>

> [@DanRathbun](#):
>
> This is not tested. Could not find my old snippet, so I typed this out just a few minutes ago.

Thanks! I will test it.

> [@Aerilius](#):
>
> Mousemove events should be captured just by hovering the main SketchUp window

Yes. But for Zoom with mouse Scrollwheel i need Focus on ManWindow.  
Thanks for Tipps and Code! I will test it and i have an other Idea.

Edit: My extension is for Windows only, because it calling a exe written in C++ and use Sketchup C++ API

---

<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 23, 2015, 8:05pm UTC](https://forums.sketchup.com/t/how-to-set-focus-to-mainwindow/12383/10 "2015-06-23T20:05:26Z")

</div>

> [@Aerilius](#):
>
> If you mean keypresses, one can let the webdialog’s javascript listen for key events and pass them to Ruby.

Thomas Thomassen has a function that does this in his TT\_Lib2 library. (But it use Win32API system calls that are not dependant upon correct language caption.):  
[TT::SketchUp::activate\_main\_window()](https://bitbucket.org/thomthom/tt-library-2/src/9ff3a5f08604ed477eb4b17fd63612bfc42832dd/TT_Lib2/sketchup.rb?at=Version%202.9#cl-88)

* * *

**Discussions & code samples over at the SketchUcation forums:**

[Re: Web dialogs stealing focus within my tool.](http://sketchucation.com/forums/viewtopic.php?f=180&t=57462#p522729)

[Blurring the webdialog on Mac](http://sketchucation.com/forums/viewtopic.php?f=180&t=49075#p441326)

---

<div class="post-metadata">

**Author:** ![gottselig\_v](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@gottselig\_v](https://forums.sketchup.com/u/gottselig_v)\
**Post date:** [June 24, 2015, 8:14am UTC](https://forums.sketchup.com/t/how-to-set-focus-to-mainwindow/12383/11 "2015-06-24T08:14:26Z")

</div>

Solved! Simple and dirty 😄

```
def FocusMainWindow()
  #create and close a dummie Webdialog
  dummieDlg = UI::WebDialog.new("dummie", true, "dummie", 0, 0, 10000, 10000, true)
  dummieDlg.show()
  dummieDlg.close()
end

```

Thanks @all

---

<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 24, 2015, 12:57pm UTC](https://forums.sketchup.com/t/how-to-set-focus-to-mainwindow/12383/12 "2015-06-24T12:57:06Z")

</div>

Nice idea. I am surprised at how many workarounds for one issue we can come up with.

👍

---

<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 25, 2021, 6:17am UTC](https://forums.sketchup.com/t/how-to-set-focus-to-mainwindow/12383/13 "2021-06-25T06:17:30Z")

</div>

## **UPDATE** - added with the 2021.1 release …

- 
## [`Sketchup::focus()`](https://ruby.sketchup.com/Sketchup.html#focus-class_method)
The [focus](https://ruby.sketchup.com/Sketchup.html#focus-class_method) method is used to focus the active model window.

This API module method is cross-platform. The dummy dialog hack never worked on the Mac platform.

---

<div class="post-metadata">

**Author:** ![indie3d](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/indie3d/32/187598_2.png) [@indie3d](https://forums.sketchup.com/u/indie3d)\
**Post date:** [July 9, 2021, 5:28pm UTC](https://forums.sketchup.com/t/how-to-set-focus-to-mainwindow/12383/14 "2021-07-09T17:28:29Z")

</div>

Very good news thanks!

Before I was using a solution which I found in this forum.

```ruby

def activate
  focus_sketchup_window
end

def html_properties
{
  dialog_title: 'Focus',
  resizable: false,
  width: 10,
  height: 10,
  left: 0,
  top: 0
  }
end

def focus_sketchup_window
  dlg = UI::HtmlDialog.new(html_properties)
  dlg.set_html('<html></html>')
  dlg.show
  dlg.close
  Sketchup.active_model.select_tool(nil)
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:** [July 9, 2021, 6:50pm UTC](https://forums.sketchup.com/t/how-to-set-focus-to-mainwindow/12383/15 "2021-07-09T18:50:08Z")

</div>

Yes, but as said, that (dummy dialog) hack never worked on Mac platform.

---

<div class="post-metadata">

**Author:** ![indie3d](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/indie3d/32/187598_2.png) [@indie3d](https://forums.sketchup.com/u/indie3d)\
**Post date:** [July 9, 2021, 11:19pm UTC](https://forums.sketchup.com/t/how-to-set-focus-to-mainwindow/12383/16 "2021-07-09T23:19:59Z")

</div>

The code I found works on my Mac Book Pro (15-inch 2019) and MacOS Big Sur with SketchUp 2021.

---

<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 9, 2021, 11:36pm UTC](https://forums.sketchup.com/t/how-to-set-focus-to-mainwindow/12383/17 "2021-07-09T23:36:13Z")

</div>

It has been said, that (on the Mac) if another window (rather than a document model window) has the focus, then the “hack” will just give the focus back to the previous window.
