def killNatspeakOrDragonPad(self): unimacroutils.SetForegroundWindow(self.DragonPadHndle) mod, title, hndle = natlink.getCurrentModule() self.assert_(mod == self.DragonPadMod and hndle == self.DragonPadHndle, "could not get back to Natspeak/DragonPad window") actions.killWindow() mod, title, hndle = natlink.getCurrentModule() self.failIf(mod == self.DragonPadMod and hndle == self.DragonPadHndle, "did not kill Natspeak/DragonPad window")
def getAppWindow(self): """get application, try quick if application still exists if not, try to find (again) if cannot be found: try to start (if "shouldstartauto" is on) """ if self.app: wtext = win32gui.GetWindowText(self.app) if wtext: return # app still active self.__class__.app = None # lost the appl appWindows = mess.findTopWindows(wantedClass=self.windowclass, wantedText=self.windowcaption) if len(appWindows): if len(appWindows) > 1: print('warning, more appWindows active! %s' % appWindows) appHndle = appWindows[0] self.__class__.app = appHndle return # not found app: if not self.shouldstartauto: return D('starting the application %s' % self.apppath) meHndle = win32gui.GetForegroundWindow() result = os.startfile(self.apppath) if self.testcloseapp: # quick easy to start app probably sleepTime = 0.1 else: sleepTime = 0.5 # maybe a slow starting app for iTry in range(20): time.sleep(sleepTime) appWindows = mess.findTopWindows(wantedClass=self.windowclass, wantedText=self.windowcaption) if appWindows: break else: print('starting %s failed, dumping apps' % self.apppath) pprint.pprint(dumpTopWindows()) return if meHndle: # try to get focus back to originating window try: unimacroutils.SetForegroundWindow(meHndle) except: pass appHndle = appWindows[0] self.__class__.app = appHndle
def looseFocus(self, hndle=None): """return the focus to window with hndle, or saved handle """ controlHndle = win32gui.GetForegroundWindow() if hndle and hndle == self.app: raise ValueError( "hndle to loose focus to IS the application handle: %s" % hndle) if controlHndle == self.app: if hndle and hellonot(self.nonFocusHndles and self.nonFocusHndles[-1] == hndle): self.nonFocusHndles.append(hndle) else: if hndle: if hndle == controlHndle: return # OK if not (self.nonFocusHndles and self.nonFocusHndles[-1] == hndle): self.nonFocusHndles.append(hndle) print('loosing focus, hndle: %s' % self.app) # now do the work: if not self.nonFocusHndles: print('cannot loose focus, no stack') #if controlHndle == self.app: # action("SSK {alt+tab}") # self.wait(0.3) return while self.nonFocusHndles: hndle = self.nonFocusHndles.pop() try: print('loosing focus, to:%s (rest: %s)' % (hndle, self.nonFocusHndles)) unimacroutils.SetForegroundWindow(hndle) self.wait() self.nonFocusHndles.append(hndle) return except: print('exception loosing focus to %s, try previous on list' % hndle) continue raise Exception('could not loose focus an favour of %s' % hndle) else: if controlHndle == self.app: print('cannot loose focus, stack exhausted')
def acquireFocus(self): """get focus of special app, return previous handle if focus was already there, return None """ if not self.app: raise Exception('no application for aquiring focus') hndle = win32gui.GetForegroundWindow() if hndle == self.app: return self.saveFocus( ) # for next occurrence, mostly already done from gotBegin unimacroutils.SetForegroundWindow(self.app) for i in range(10): controlHndle = win32gui.GetForegroundWindow() if controlHndle == self.app: return hndle time.sleep(0.1) raise OSError('could not aquire focus for %s' % self.app)
def onTextChange(self, delStart, delEnd, newText, selStart, selEnd): if self.inOnTextChange: return self.inOnTextChange = 1 D('onTextChange: %s, %s, |%s|, %s, %s' % (delStart, delEnd, repr(newText), selStart, selEnd)) if not self.ctrl: D("do not have ctrl control") return dct = self.dictObj dct.setLock(1) getFocus = (selEnd - selStart > 1) # at start of call if newText: if self.lastSel == (delStart, delEnd): dct.setText(self.lastSelText, delStart, selEnd) # empty now else: print('window changed, update again') self.updateState() self.insertText(delStart, delEnd, newText) else: # loosing scratch that info: self.scratchinfo = [] if delStart < delEnd: print('selection to delete: %s, %s' % (delStart, delEnd)) if selStart < selEnd: self.setSelection(selStart, selEnd) # only set the window... else: self.insertText(selStart, selEnd, newText) #selStart, selEnd, newText = self.adjustAtEndOfText(selStart, selEnd, "") #if newText: #else: # self.setSelection(selStart,selEnd) # only set the window... dct.setLock(0) self.inOnTextChange = 0 if getFocus: unimacroutils.SetForegroundWindow(self.app)