lib/win32/autogui/window.rb in win32-autogui-0.2.1 vs lib/win32/autogui/window.rb in win32-autogui-0.3.0
- old
+ new
@@ -48,10 +48,12 @@
# Wrapper for window
#
class Window
include Windows::Window # instance methods from windows-pr gem
include Windows::Window::Message # PostMessage and constants
+ include Autogui::Logging
+ include Autogui::Input
attr_reader :handle
def initialize(handle)
@handle = handle
@@ -80,11 +82,11 @@
#
def close(options={})
PostMessage(handle, WM_SYSCOMMAND, SC_CLOSE, 0)
wait_for_close(options) if (options[:wait_for_close] == true)
end
-
+
# Wait for the window to close
#
# @param [Hash] options
# @option options [Boolean] :timeout (5) timeout in seconds
#
@@ -94,11 +96,11 @@
sleep 0.05 until 0 == IsWindow(handle)
end
end
# @return [String] the ANSI Windows ClassName
- #
+ #
def window_class
buffer = "\0" * 255
length = GetClassNameA(handle, buffer, buffer.length)
length == 0 ? '' : buffer[0..length - 1]
end
@@ -123,16 +125,34 @@
def is_window?
(handle != 0) && (IsWindow(handle) != 0)
end
# Brings the window into the foreground and activates it.
- # Keyboard input is directed to the window, and various visual cues
+ # Keyboard input is directed to the window, and various visual cues
# are changed for the user.
#
+ # A process can set the foreground window only if one of the following conditions is true:
+ #
+ # * The process is the foreground process.
+ # * The process was started by the foreground process.
+ # * The process received the last input event.
+ # * There is no foreground process.
+ # * The foreground process is being debugged.
+ # * The foreground is not locked.
+ # * The foreground lock time-out has expired.
+ # * No menus are active.
+ #
# @return [Number] nonzero number if sucessful, nil or zero if failed
#
def set_focus
- SetForegroundWindow(handle) if is_window?
+ if is_window?
+ # if current process was the last to receive input, we can be sure that
+ # SetForegroundWindow will be allowed. Send the shift key to whatever has
+ # the focus now. This allows IRB to set_focus.
+ keystroke(VK_SHIFT)
+ ret = SetForegroundWindow(handle)
+ logger.warn("SetForegroundWindow failed") if ret == 0
+ end
end
# The identifier (pid) of the process that created the window
#
# @return [Integer] process id if the window exists, otherwise nil