在 Python 中,使用 wxPython Phoenix 4.1.1 wx.html2 模块的 WebView,当页面加载时,我希望键盘焦点落在页面上的第一个元素上,这样我就可以从 Down 开始阅读页面使用 JAWS 或 NVDA 屏幕阅读器的箭头键。但我无法做到这一点,因为在页面加载时,屏幕阅读器键盘焦点总是跳转到页面中间的某个地方。我尝试使用在事件处理程序中调用<h1>
的函数将 JavaScript 的焦点手动设置到页面上的第一个元素,如下所示,但这根本没有效果。RunScript
wx.html2.EVT_WEBVIEW_LOADED
请注意,在此代码中,我使用此处描述的技巧在显示 WebView 组件后将屏幕阅读器焦点移动到 WebView 组件中。
class HelpHTMLDialog(wx.Dialog):
def __init__(self, title, parent = None):
super(HelpHTMLDialog, self).__init__(parent = parent, title = title)
self.addBrowser()
self.SetSize((900, 700))
self.Centre()
self.ShowModal()
# Adds the web browser to this dialog and binds the page load event.
def addBrowser(self):
vbox = wx.BoxSizer(wx.VERTICAL)
self.browser = wx.html2.WebView.New(self)
self.Bind(wx.html2.EVT_WEBVIEW_LOADED, self.onPageLoad, self.browser)
html = self.getHTML()
self.browser.SetPage(html, '')
vbox.Add(self.browser, 1, wx.EXPAND, 10)
self.SetSizer(vbox)
# Handles the WebView page load.
def onPageLoad(self, event):
# Clicks on the left upper corner of the page to move screen reader focus to the page
self.browser.SetFocus()
position = self.browser.GetPosition()
position = self.browser.ClientToScreen(position)
robot = wx.UIActionSimulator()
robot.MouseMove(position)
robot.MouseClick()
# Move the keyboard focus to the first <h1> heading on the page
self.browser.RunScript('''
var first = document.getElementsByTagName('h1')[0];
first.tabIndex = -1;
first.focus();
// I know this code runs after page load without a problem, because I tried to put alert('test') here, and it worked.
''')
# Gets the HTML of the page.
def getHTML(self):
html = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>Hello Python</h1>
<h2>First heading</h2>
<p>Here is some text just to demonstrate the use of wx.html2.WebView in Python.</p>
<ul>
<li>Firs item.</li>
<li>Second item.</li>
<li>Third item.</li>
<li>Fourth item.</li>
</ul>
<h2>Second heading</h2>
<p>Final paragraph is here just to make the page a bit longer.</p>
<script>
// I have also tried putting the JavaScript code for focusing the first <h1> element here instead, but with no success either.
</script>
</body>
</html>
'''
return html
那么,您对如何以我描述的方式在页面加载后有效控制屏幕阅读器键盘焦点有任何想法吗?
谢谢