我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用ui.message()。
def event_liveRegionChange(self, obj, nextHandler): if isinstance(obj, UIA) and obj.name != self._nameChangeCache: automationID = obj.UIAElement.cachedAutomationID try: # Don't repeat the fact that update download/installation is in progress if progress bar beep is on. if ((automationID == "SystemSettings_MusUpdate_UpdateStatus_DescriptionTextBlock" and obj.previous.value <= "0") # For search progress bar, do not repeat it. or (automationID == "ProgressBar") # Do not announce "result not found" error unless have to. or (automationID == "NoResultsFoundTextBlock" and obj.parent.UIAElement.cachedAutomationID == "StatusTextPopup") # But announce individual update progress in build 16215 and later. or ("ApplicableUpdate" in automationID and automationID.endswith("_ContextDescriptionTextBlock"))): self._nameChangeCache = obj.name # Until the spacing problem is fixed for update label... if "ApplicableUpdate" in automationID and automationID.endswith("_ContextDescriptionTextBlock"): ui.message(" ".join([obj.parent.name, obj.name])) else: ui.message(obj.name) # And no, never allow double-speaking (an ugly hack). return except AttributeError: pass
def locateElement(self, automationID): # Foreground isn't reliable. fg = api.getForegroundObject() if fg.getChild(1).childCount > 0: screenContent = fg.getChild(1) else: screenContent = fg.getChild(2) # Thanks to My Peple in Fall Creators Update, screen content so far could actually be the title bar, and the actual foreground window is next door. # In other words, Skype window is embedded inside My People window. if screenContent.UIAElement.cachedAutomationID == "TitleBar": # The following traversal path may change in future builds. screenContent = screenContent.next.simpleLastChild.simpleFirstChild # Element placement (according to UIA changes from time to time. # Wish there is a more elegant way to do this... for element in screenContent.children: if isinstance(element, UIA) and element.UIAElement.cachedAutomationID == automationID: return element return None # Name change cache (yet again) # In some cases, Skype message fires name change, and a related element fires live region changed event.
def event_nameChange(self): #a nameChange event is fired by breakpoint UI control when the caret reaches a line with breakpoint, so, we rely on this to announce breakpoints global caretMovedToDifferentLine if not caretMovedToDifferentLine: #a nameChange event can be fired multiple times when moving by character within the same line, so, return if we already announced the break point for the current line return caretMovedToDifferentLine = False currentLineNum = _getCurLineNumber() BPLineNum = self._getLineNumber() if currentLineNum == 0 or BPLineNum == 0 \ or currentLineNum != BPLineNum: return if config.conf["visualStudio"]["beepOnBreakpoints"]: tones.beep(1000, 50) if not config.conf["visualStudio"]["announceBreakpoints"]: return message = _("breakpoint") state = re.search(REG_GET_BREAKPOINT_STATE, self.name) if state: message += " " message += state.group() ui.message(message)
def script_announce_scope(self, gesture): info=api.getReviewPosition().copy() info.expand(textInfos.UNIT_LINE) line_text = info.text if not line_text: ui.message(_("No text found")) return char, position = find_indent(line_text) if position == 0: ui.message(_("Not indented")) return for line in previous_lines(info): prev_line_text = line.text if not prev_line_text.strip(): continue new_char, new_position = find_indent(prev_line_text) if new_position < position: if new_position == 0 or new_char == char: ui.message(prev_line_text) return
def flashRightTextChanged(self, obj): text = obj.name if not text: return if self.lastFlashRightText == text: return self.lastFlashRightText = text mOff="Dragon\'s microphone is off;" mOn="Normal mode: You can dictate and use voice" mSleep="The microphone is asleep;" if mOn in text: ui.message("Dragon mic on") elif mOff in text: ui.message("Dragon mic off") elif mSleep in text: ui.message("Dragon sleeping")
def script_switch_flatMode(self,gesture) : val = config.conf['lambda']['brailleFlatMode'] = not config.conf['lambda']['brailleFlatMode'] #Translators: This determines whether to use API or DisplayMode to render the editor window on a braille display. It is a toggle (on/off) flatModeMessage = _("flat mode ") self.TextInfo = self._get_TextInfo() ui.message(flatModeMessage + ((lambda x: shMsg.GLB_ON if x else shMsg.GLB_OFF)(val))) braille.handler.mainBuffer.clear() braille.handler.handleGainFocus(self) #This script set the desired textInfo for braille, when flat mode is on, the LambdaEditorFlatTextInfo is used, otherwise the LambdaEditorTextInfo is set.
def message(self, text): import speech, braille braille.handler.message(text) if self.appModule.commandAnnouncement: speech.speakMessage(text) # A master function to obtain needed info from status bars. # #17.05: because this is prone to failure, insert debug messages if asked.
def script_dropStartMarker(self, gesture): gesture.send() # Translators: The start marker position for selecting parts of the audio track (example output: "Start: 0.00"). self.message(_("Start: {startMarkerPos}").format(startMarkerPos = self.getAudioSelectionParsed()[0]))
def script_dropFinishMarker(self, gesture): gesture.send() # Translators: The finish marker position for selecting parts of the audio track (example output: "Finish: 5.00"). self.message(_("Finish: {finishMarkerPos}").format(finishMarkerPos = self.getAudioSelectionParsed()[2]))
def script_playSelection(self, gesture): gesture.send() # Translators: Presented when selected audio is playing. self.message(_("Play selection"))
def script_selectAll(self, gesture): gesture.send() # Translators: Presented when all parts of the audio track is selected. self.message(_("Select All"))
def script_dropCueAtStartMarker(self, gesture): gesture.send() # Translators: Presented when audio cue is dropped at the start marker position. self.message(_("Cue dropped at start marker"))
def script_dropCueAtFinishMarker(self, gesture): gesture.send() # Translators: Presented when an audio cue is dropped at the finish marker position. self.message(_("Cue dropped at finish marker"))
def script_moveStartMarkerToNextCuePos(self, gesture): gesture.send() # Translators: Presented when the start marker is moved to the next cue position. self.message(_("Start marker at next cue"))
def script_moveStartMarkerToPrevCuePos(self, gesture): gesture.send() # Translators: Presented when the start marker is moved to the next cue position. self.message(_("Start marker at previous cue"))
def script_moveFinishMarkerToNextCuePos(self, gesture): gesture.send() # Translators: Presented when the finish marker is moved to the next cue position. self.message(_("Finish marker at next cue"))
def script_deleteSelection(self, gesture): gesture.send() # Translators: Presented when audio selection is deleted. self.message(_("deleted"))
def script_play(self, gesture): gesture.send() # Translators: Presented when a track is playing in Goldwave. self.message(_("play"))
def script_rewind(self, gesture): gesture.send() # Translators: Presented when a track is rewinding in Goldwave. self.message(_("rewind"))
def script_forward(self, gesture): gesture.send() # Translators: Presented when fast forwarding a track in Goldwave. self.message(_("fast forward"))
def script_pause(self, gesture): gesture.send() # Translators: Presented when pausing a track in Goldwave. self.message(_("pause"))
def script_startRecord(self, gesture): gesture.send() # Translators: Presented when starting recording in Goldwave. self.message(_("record")) # Audio position scripts: markers, selection duration.
def script_announceAudioPosition(self, gesture): # Shouldn't say anything unless in audio editing view. self.message(self.getAudioPos()) # Translators: Input help mode message for a Goldwave command.
def script_announceAudioSelection(self, gesture): # Parse this string to get individual info such as marker positions. audioSelectionParsed = self.getAudioSelectionParsed() if not audioSelectionParsed: # Translators: Presented when there is no audio selection summary available. self.message(_("Unable to obtain audio selection summary. Please close and reopen the audio track.")) else: # Translators: The audio selection summary message (example output: "0.00 to 1.00 (1.00)"). self.message(_("{audioSelectionStart} to {audioSelectionEnd} {audioSelectionLength}").format(audioSelectionStart = audioSelectionParsed[0], audioSelectionEnd = audioSelectionParsed[2], audioSelectionLength = audioSelectionParsed[3])) # Translators: Input help mode message for a Goldwave command.
def script_announceTrackLength(self, gesture): trackLengthSTR = self.getTrackLength() if not trackLengthSTR: # Translators: Presented when there is no track length information. self.message(_("Track length is unavailable. Please close and reopen the audio track.")) else: self.message(_("Track length: {trackLength}").format(trackLength = trackLengthSTR)) # Translators: Input help mode message for a Goldwave command.
def script_announceRemainingTime(self, gesture): audioPos = self.getAudioPos(raw=True) if not audioPos or " " in audioPos or not self.getTrackLength(): # Translators: An error message presented when remaining time cannot be anounced. ui.message(_("Cannot tell you remaining time for the current track")) else: if ":" in audioPos: ui.message(self.getRemainingTime(audioPos)) elif float(audioPos) == 0.0: ui.message(self.getTrackLength()) else: ui.message(self.getRemainingTime(audioPos)) # Audio channels and zoom level.
def script_announceZoomLevel(self, gesture): # Translators: Presented to indicate audio selection zoom level (example output: "Zoom level: 10.000"). self.message(_("Zoom level: {zoomLevel}").format(zoomLevel = self.getZoomLevel())) # Translators: Input help mode message for a Goldwave command.
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 script_ocrNavigatorObject(self, gesture): nav = api.getNavigatorObject() left, top, width, height = nav.location img = ImageGrab.grab(bbox=(left, top, left + width, top + height)) # Tesseract copes better if we convert to black and white... img = img.convert(mode='L') # and increase the size. img = img.resize((width * IMAGE_RESIZE_FACTOR, height * IMAGE_RESIZE_FACTOR), Image.BICUBIC) baseFile = os.path.join(tempfile.gettempdir(), "nvda_ocr") try: imgFile = baseFile + ".bmp" img.save(imgFile) ui.message(_("Running OCR")) lang = getConfig()['language'] # Hide the Tesseract window. si = subprocess.STARTUPINFO() si.dwFlags = subprocess.STARTF_USESHOWWINDOW si.wShowWindow = subprocess.SW_HIDE subprocess.check_call((TESSERACT_EXE, imgFile, baseFile, "-l", lang, "hocr"), startupinfo=si) finally: try: os.remove(imgFile) except OSError: pass try: hocrFile = baseFile + ".html" parser = HocrParser(file(hocrFile).read(), left, top) finally: try: os.remove(hocrFile) except OSError: pass # Let the user review the OCR output. nav.makeTextInfo = lambda position: OcrTextInfo(nav, position, parser) api.setReviewPosition(nav.makeTextInfo(textInfos.POSITION_FIRST)) ui.message(_("Done"))
def event_suggestionsOpened(self): super(SearchField, self).event_suggestionsOpened() # Announce number of items found (except in Start search box where the suggestions are selected as user types). # Oddly, Edge's address omnibar returns 0 for suggestion count when there are clearly suggestions (implementation differences). # Because inaccurate count could be announced (when users type, suggestion count changes), thus announce this if position info reporting is enabled. if config.conf["presentation"]["reportObjectPositionInformation"]: if self.UIAElement.cachedAutomationID == "TextBox" or self.UIAElement.cachedAutomationID == "SearchTextBox" and self.appModule.appName != "searchui": # Item count must be the last one spoken. suggestionsCount = self.controllerFor[0].childCount suggestionsMessage = "1 suggestion" if suggestionsCount == 1 else "%s suggestions"%suggestionsCount queueHandler.queueFunction(queueHandler.eventQueue, ui.message, suggestionsMessage)
def event_liveRegionChange(self, obj, nextHandler): if isinstance(obj, UIA): # Accessibility message alerts. # For some messages, system alert event is raised along with this event, so return. if obj.role == controlTypes.ROLE_ALERT and obj.UIAElement.cachedAutomationID == "a11y-announcements-message": return nextHandler() # For message alerts, system alert and live region changed are fired at the same time.
def event_UIA_systemAlert(self, obj, nextHandler): if isinstance(obj, UIA): if obj.role == controlTypes.ROLE_ALERT and obj.UIAElement.cachedAutomationID == "a11y-announcements-message": # Mick Curran says use last child in case a series of live texts are part of this control. ui.message(obj.lastChild.name) nextHandler()
def event_UIA_elementSelected(self, obj, nextHandler): # #7273: When this is fired on categories, the first emoji from the new category is selected but not announced. # Therefore, move the navigator object to that item if possible. # However, in recent builds, name change event is also fired. # For consistent experience, report the new category first by traversing through controls. speech.cancelSpeech() # And no, if running on build 17040 and if this is typing suggestion, do not announce candidate window changes, as it is duplicate announcement and is anoying. if obj.UIAElement.cachedAutomationID == "IME_Candidate_Window": return candidate = obj if obj.UIAElement.cachedClassName == "ListViewItem": # The difference between emoji panel and suggestions list is absence of categories/emoji separation. # If dealing with keyboard entry suggestions (build 17040 and later), return immediately. candidate = obj.parent.previous if candidate is None: ui.message(obj.name) nextHandler() return ui.message(candidate.name) obj = candidate.firstChild if obj is not None: api.setNavigatorObject(obj) obj.reportFocus() braille.handler.message(braille.getBrailleTextForProperties(name=obj.name, role=obj.role, positionInfo=obj.positionInfo)) else: # Translators: presented when there is no emoji when searching for one in Windows 10 Fall Creators Update and later. ui.message(_("No emoji")) nextHandler()
def script_previousLine(self, gesture): if self.curLine > 0: self.curLine -=1 ui.message(self.lines[self.curLine]) else: # Translators: Message presented when no more weather data is available for the current item. ui.message(_("No more weather data for this item.")) wx.Bell()
def event_nameChange(self, obj, nextHandler): if isinstance(obj, UIA): # #18: Announce street side changes as one uses the keyboard. if obj.UIAElement.cachedAutomationID == "StreetsideAddressTextBlock": ui.message(obj.name) nextHandler() # Yet again, live region change fires even if no text has changed.
def event_liveRegionChange(self, obj, nextHandler): if isinstance(obj, UIA): if obj.parent.UIAElement.cachedAutomationID == "MapControl" and obj.name != self.liveText: self.liveText = obj.name ui.message(obj.name) # And no, never call next handler.
def event_nameChange(self, obj, nextHandler): # NVDA, can you act as a mouthpiece for Cortana? if isinstance(obj, UIA) and isinstance(obj.next, UIA) and obj.next.UIAElement.cachedAutomationID != "SpeechButton" and obj.name != self.cortanaResponseCache: element = obj.UIAElement # There are two Cortana response lines. Usually line 2 is more reliable. # However, Redstone seems to favor line 1 better. # A specific automation ID is used for reminders and others. if element.cachedAutomationID in ("SpeechContentLabel", "WeSaidTextBlock", "GreetingLine1"): ui.message(obj.name) self.cortanaResponseCache = obj.name nextHandler()
def event_liveRegionChange(self, obj, nextHandler): if isinstance(obj, UIA) and obj.UIAElement.cachedAutomationID == "_progressText": if obj.name != self._appInstallProgress: self._appInstallProgress = obj.name # Don't forget to announce product title. productTitle = obj.parent.previous # Since March 2017 update, it is no longer the product name, but a progress button. if productTitle and productTitle.role == controlTypes.ROLE_BUTTON: # But since September 2017 update, the title is next door. possibleProductTitle = productTitle.parent.previous productTitle = productTitle.previous if possibleProductTitle is None else productTitle.parent.previous if productTitle and isinstance(productTitle, UIA) and productTitle.UIAElement.cachedAutomationID == "_productTitle": ui.message(" ".join([productTitle.name, obj.name])) return nextHandler()
def reportFocus(self): # Skype message/channel info and other extraneous text should not be announced. # Credit: Derek Riemer # But save the old name just in case it needs to be referred back to. self._message = self.name self.name = self.getShortenedMessage() super(SkypeMessage, self).reportFocus()
def script_showMessageLongDesc(self, gesture): ui.message(self._message) # Translators: the description for the showMessageLongDesc script on Skype for Windows 10.
def event_nameChange(self, obj, nextHandler): # In recent versions, live region change event is used instead, so don't announce messages with this method. if isinstance(obj, UIA): uiElement = obj.UIAElement if uiElement.cachedClassName == "TextBlock" and obj.next is not None: # Announce typing indicator (same as Skype for Desktop). nextElement = obj.next.UIAElement # Make sure to catch all possible UI placement changes between Skype UWP releases. if nextElement.cachedAutomationID in ("ChatEditBox", "ChatTranslationSettings"): # Translators: Presented when someone stops typing in Skype app (same as Skype for Desktop). ui.message(obj.name if obj.name != "" else _("Typing stopped")) nextHandler() # The live region changed event for messages has no automation ID whatsoever. # Unfortunately, Skype message fires name change, so be sure to perform one or the other.
def event_liveRegionChange(self, obj, nextHandler): if isinstance(obj, UIA): uiaElement = obj.UIAElement if not uiaElement.cachedAutomationID and uiaElement.cachedClassName == "TextBlock" and obj.name != self._skypeMessageCache: ui.message(getShortenedMessage(obj.name)) self._skypeMessageCache = obj.name return nextHandler()
def script_moveToArea(self, gesture): area = int(gesture.displayName.split("+")[-1]) # However, since March 2017, areas received keyboard shortcuts, such as Alt+2 for contacts list. if self.productVersion >= "11.13.133.0" and area < 4: gesture.send() return element = self.locateElement(self.moveTo[area][0]) if element is None: ui.message(self.moveTo[area][1]) return element.setFocus() return
def event_nameChange(self, obj, nextHandler): ui.message(obj.name) nextHandler()
def script_reportStatusLine(self, gesture): #it seems that the status bar is the last child of the forground object #so, get it from there obj = api.getForegroundObject().lastChild found=False if obj and obj.role == controlTypes.ROLE_STATUSBAR: text = api.getStatusBarText(obj) api.setNavigatorObject(obj) found=True else: info=api.getForegroundObject().flatReviewPosition if info: info.expand(textInfos.UNIT_STORY) info.collapse(True) info.expand(textInfos.UNIT_LINE) text=info.text info.collapse() api.setReviewPosition(info) found=True if not found: # Translators: Reported when there is no status line for the current program or window. ui.message(_("No status line found")) return if scriptHandler.getLastScriptRepeatCount()==0: ui.message(text) else: speech.speakSpelling(text) # Translators: Input help mode message for report status line text command.
def script_expand(self, gesture): if controlTypes.STATE_COLLAPSED in self.states: #translators: a message indecating that a tree view item in watch / locals /... tool window has been expanded ui.message(_("expanded")) gesture.send()
def script_collapse(self, gesture): if controlTypes.STATE_EXPANDED in self.states: #translators: a message indecating that a tree view item in watch / locals /... tool window has been collapsed ui.message(_("collapsed")) gesture.send()
def script_onSizeChange(self, gesture): gesture.send() #get the position from the status bar obj = api.getForegroundObject().lastChild text = obj.children[2].name width = re.match(REG_SPLIT_LOCATION_TEXT, text).group(3) hight = re.match(REG_SPLIT_LOCATION_TEXT, text).group(4) #translators: the width and the hight of a UI element in windows forms designer msg = _("width: %s hight: %s" %(width, hight)) ui.message(msg)
def script_breakpointToggle(self,gesture) : colors = self._hasBackground([RGB_BP]) if(colors[RGB_BP]) : ui.message(_("Breakpoint off")) else : ui.message(_("Breakpoint on")) gesture.send()
def script_errorReport(self,gesture) : gesture.send() colors = self._hasBackground([RGB_ERROR,RGB_WARN]) if(colors[RGB_ERROR]) : braille.handler.message(_("\t\t error %% error")) self.appModule.play_error() elif(colors[RGB_WARN]) : braille.handler.message(_("\t\t warning %% warning")) self.appModule.play_warning() globalCommands.commands.script_reportCurrentLine(gesture)