Python api 模块,getFocusObject() 实例源码
我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用api.getFocusObject()。
def script_calculatorResult(self, gesture):
# To prevent double focus announcement, check where we are.
focus = api.getFocusObject()
navMenu = False
if isinstance(focus, UIA) and isinstance(focus.parent.parent, UIA) and focus.parent.parent.UIAElement.cachedAutomationID == "FlyoutNav":
navMenu = True
gesture.send()
# In redstone, calculator result keeps firing name change, so tell it to do so if and only if enter has been pressed.
self.shouldAnnounceResult = True
# Hack: only announce display text when an actual calculator button (usually equals button) is pressed.
# In redstone, pressing enter does not move focus to equals button.
if isinstance(focus, UIA):
if focus.UIAElement.cachedAutomationID == "CalculatorResults":
queueHandler.queueFunction(queueHandler.eventQueue, focus.reportFocus)
elif focus.UIAElement.cachedAutomationID != "NavButton":
# In newer releases, result is located on the same spot in the object hierarchy.
result = api.getForegroundObject().children[1].children[3]
if result.UIAElement.cachedAutomationID == "CalculatorResults" and not navMenu:
# And no, do not allow focus to move.
queueHandler.queueFunction(queueHandler.eventQueue, result.reportFocus)
def flushCurrentEntry():
global currentEntry, autoFlushTimer
if autoFlushTimer is not None:
autoFlushTimer.Stop()
autoFlushTimer = None
start, text = currentEntry
text = text.replace("\r\n", "\n")
text = text.replace("\r", "\n")
while True:
i = text.find("\n")
if i == -1:
break
if i > 0:
speech.speakText(text[:i])
if text[i:i + 2] == "\n\n":
speech.speakText("new paragraph")
text = text[i + 2:]
else:
speech.speakText("new line")
text = text[i + 1:]
if text != "":
speech.speakText(text)
braille.handler.handleCaretMove(api.getFocusObject())
currentEntry = None
requestWSRShowHideEvents()
def script_speakDebugLine(self, gesture):
gesture.send()
time.sleep(.025)
new = api.getFocusObject().makeTextInfo(textInfos.POSITION_CARET)
new.move(textInfos.UNIT_LINE, 0, endPoint = "start")
new.expand(textInfos.UNIT_LINE)
log.info(new.text)
speech.speakMessage(new.text)
def script_toggleCommandAnnouncement(self, gesture):
focus = api.getFocusObject()
if focus.windowClassName not in ("TWaveView", "TSoundForm"):
# Translators: Presented when command announcement toggle is unavailable.
ui.message(_("You need to be in sound window to toggle command announcement"))
else:
self.commandAnnouncement = not self.commandAnnouncement
# Handle the announcement of this script separately, since we need to speak even when false.
if self.commandAnnouncement:
# Translators: Presented when command announcement messages are turned on in Goldwave.
ui.message(_("command announcement on"))
else:
# Translators: Presented when command announcement messages are turned off in Goldwave.
ui.message(_("command announcement off"))
# Translators: Input help mode message for command announcement command in Goldwave.
def event_suggestionsClosed(self):
# Work around broken/odd controller for event implementation in Edge's address omnibar (don't even announce suggestion disappearance when focus moves).
if self.UIAElement.cachedAutomationID == "addressEditBox" and self != api.getFocusObject():
return
nvwave.playWaveFile(r"waves\suggestionsClosed.wav")
# Contacts search field in People app and other places.
# An ugly hack to prevent suggestion founds from repeating.
def event_UIA_elementSelected(self):
focusControllerFor=api.getFocusObject().controllerFor
if len(focusControllerFor)>0 and focusControllerFor[0].appModule is self.appModule and self.name:
speech.cancelSpeech()
api.setNavigatorObject(self)
self.reportFocus()
def event_caret(self) :
super(Edit, self).event_caret()
if self is api.getFocusObject() and not eventHandler.isPendingEvents('gainFocus'):
self.detectPossibleSelectionChange()
tx = self.makeTextInfo(textInfos.POSITION_SELECTION)
tx.collapse()
tx.expand(textInfos.UNIT_LINE)
if self.oldpos == tx._startOffset :
return
self.processLine(tx)
def get_terminate_button(self) :
if self.terminateButton != None : return
obj = api.getFocusObject()
while (obj.parent is not None) :
if (obj.role == controlTypes.ROLE_TABCONTROL) and (obj.name == 'Console') :
break
obj = obj.parent
if obj.name != "Console" : return
while obj.role is not controlTypes.ROLE_TOOLBAR :
obj = obj.firstChild
for i in xrange(1,obj.childCount) :
if obj.IAccessibleObject.accName(i) == "Terminate" :
self.terminateButton = obj.children[i-1]
return
def _get_script_hacky(self):
#Workaround until DB 1.1 when I fix NVDA to not need repeated scripts.
#Mostly based on scriptHandler.findScript, but no globalGestureMapness
focus = api.getFocusObject()
if not focus:
return None
ti = focus.treeInterceptor
if ti:
func = self._getScriptFromObject(ti)
if func and (not ti.passThrough or getattr(func,"ignoreTreeInterceptorPassThrough",False)):
return (func, ti)
# NVDAObject level.
func = self._getScriptFromObject(focus)
if func:
return (func, focus)
for obj in reversed(api.getFocusAncestors()):
func = self._getScriptFromObject(obj)
if func and getattr(func, 'canPropagate', False):
return (func, obj)
# Global commands.
func = self._getScriptFromObject(globalCommands.commands)
if func:
return (func, globalCommands.commands)