Method: WinGui::Window#click

Defined in:
lib/win_gui/window.rb

#click(opts = {}) ⇒ Object

Emulates click of the control identified by opts (:id, :title, :class). Beware of keyboard shortcuts in button titles! So, use “&Yes” instead of just “Yes”. Returns screen coordinates of click point if successful, nil if control was not found

:id

integer control id (such as IDOK, IDCANCEL, etc)

:title

window title

:class

window class

:raise

raise this exception instead of returning nil if nothing found

:position/point/where

location where the click is to be applied - default :center

:mouse_button/button/which

mouse button which to click - default :right



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/win_gui/window.rb', line 114

def click(opts={})
  control = child(opts)
  if control
    left, top, right, bottom = control.get_window_rect

    where = opts[:point] || opts[:where] || opts[:position]
    point = case where
              when Array
                where # Explicit screen coords
              when :random
                [left + rand(right - left), top + rand(bottom - top)] # Random point within control window
              else
                [(left + right) / 2, (top + bottom) / 2] # Center of a control window
            end

    WinGui.set_cursor_pos *point

    button = opts[:mouse_button] || opts[:mouse] || opts[:which]
    down, up = (button == :right) ?
        [WinGui::MOUSEEVENTF_RIGHTDOWN, WinGui::MOUSEEVENTF_RIGHTUP] :
        [WinGui::MOUSEEVENTF_LEFTDOWN, WinGui::MOUSEEVENTF_LEFTUP]

    WinGui.mouse_event down, 0, 0, 0, 0
    WinGui.mouse_event up, 0, 0, 0, 0
    point
  else
    nil
  end
end