From 45bad8667cb674fee1a43f8fc558c5943d735ed2 Mon Sep 17 00:00:00 2001 From: "rygwdn@gmail.com" <> Date: Mon, 28 Feb 2011 22:00:47 -0400 Subject: [PATCH] try to ensure that the GVIM window is open before sending keys to it --- test.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/test.py b/test.py index 9512f96..948eac2 100755 --- a/test.py +++ b/test.py @@ -65,7 +65,7 @@ COMPL_ACCEPT = chr(25) if WIN: # import windows specific modules - import win32api, win32com, win32con, win32com.client + import win32com.client, win32gui shell = win32com.client.Dispatch("WScript.Shell") def focus_win(title=None): @@ -73,6 +73,12 @@ def focus_win(title=None): raise Exception("Failed to switch to GVim window") time.sleep(1) +def is_focused(title=None): + cur_title = win32gui.GetWindowText(win32gui.GetForegroundWindow()) + if (title or "- GVIM") in cur_title: + return True + return False + BRACES = re.compile("([}{])") WIN_ESCAPES = ["+", "^", "%", "~", "[", "]", "<", ">", "(", ")"] WIN_REPLACES = [ @@ -106,7 +112,14 @@ def send_win(keys, session): seq_o = seq seq = convert_keys(seq) - print "keys: '%s' -> '%s'" % (seq_o, seq.replace("~", "\n")) + + if not is_focused(): + time.sleep(2) + focus() + if not is_focused(): + # This is the only way I can find to stop test execution + raise KeyboardInterrupt("Failed to focus GVIM") + shell.SendKeys(seq) ################ end windows ################