How to set Focus to Mainwindow


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?


Thomas Thomassen may have that functionality in the SKUI library.



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?


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.


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

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


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 “” 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

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.

require "win32ole"

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

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


Ref: MSDN: Windows Script Host - AppActivate Method

Bring to Back?

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


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.

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

Bring to Back?

Thanks! I will test it.

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


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

Discussions & code samples over at the SketchUcation forums:

Re: Web dialogs stealing focus within my tool.

Blurring the webdialog on Mac


Solved! Simple and dirty :smile:

def FocusMainWindow()
  #create and close a dummie Webdialog
  dummieDlg ="dummie", true, "dummie", 0, 0, 10000, 10000, true)

Thanks @all

Bring to Back?

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