我正在尝试使用加速器表来捕获窗口中全局的按键,以便我可以触发一个方法作为响应。然而,加速器表似乎破坏了原本通过按键创建的事件,阻止我将其输入到文本控件中。有什么办法可以解决这种行为,或者有其他解决方案来解决我的问题吗?
编辑:这是这种情况的一个更好的示例:
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(-1,-1))
...
randomId = wx.NewId()
parent.Bind(wx.EVT_MENU, self.onKey, id=randomId)
accTable = wx.AcceleratorTable([(wx.ACCEL_NORMAL, 96, randomId)]) # 96 is the keycode for `
self.SetAcceleratorTable(accTable)
...
class ctrlpanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
...
grid = wx.GridBagSizer(hgap=7, vgap=7)
self.lblfreq = wx.StaticText(self, label="Base Frequency (hz): ")
grid.Add(self.lblfreq, pos=(0,0))
self.freq = wx.TextCtrl(self, value="0", size=(100, 20))
grid.Add(self.freq, pos=(0,1))
self.waveList = ['Sine', 'Semicircle', 'Saw', 'Square']
self.lblwave = wx.StaticText(self, label="Wave Shape: ")
grid.Add(self.lblwave, pos=(1,0))
self.wave = wx.Choice(self, choices=self.waveList)
self.wave.SetStringSelection('Sine')
grid.Add(self.wave, pos=(1,1))
self.SetSizerAndFit(grid)
使用此代码,加速器表可以完美工作,在焦点位于窗口的任何位置拾取我的按键,并激活 onKey 方法。但是,当焦点位于文本控件上时,我无法在文本控件中键入要拾取的字符,因为加速器表正在处理该事件,然后再将其发送到其他地方,或者阻止正常事件被触发或传播。我希望能够使事件仍然正常发生,因此在键入时文本将出现在文本控件中,即使这些键设置为通常在窗口中执行某些其他操作。我可以在这里使用任何 event.Skip() 类型的东西吗?
I am trying to use an accelerator table to catch a key press globally in the window so that I can fire a method in response. However, the accelerator table seems to destroy the event that would otherwise be created by pressing the key, preventing me from typing it into text controls. Is there any way to get around this behavior, or some other solution to my problem?
Edit: Here's a better example of the kind of situation:
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(-1,-1))
...
randomId = wx.NewId()
parent.Bind(wx.EVT_MENU, self.onKey, id=randomId)
accTable = wx.AcceleratorTable([(wx.ACCEL_NORMAL, 96, randomId)]) # 96 is the keycode for `
self.SetAcceleratorTable(accTable)
...
class ctrlpanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
...
grid = wx.GridBagSizer(hgap=7, vgap=7)
self.lblfreq = wx.StaticText(self, label="Base Frequency (hz): ")
grid.Add(self.lblfreq, pos=(0,0))
self.freq = wx.TextCtrl(self, value="0", size=(100, 20))
grid.Add(self.freq, pos=(0,1))
self.waveList = ['Sine', 'Semicircle', 'Saw', 'Square']
self.lblwave = wx.StaticText(self, label="Wave Shape: ")
grid.Add(self.lblwave, pos=(1,0))
self.wave = wx.Choice(self, choices=self.waveList)
self.wave.SetStringSelection('Sine')
grid.Add(self.wave, pos=(1,1))
self.SetSizerAndFit(grid)
With this code, the accelerator table works perfectly, picking up my key presses wherever the focus is on the window, and activating the onKey method. However, when the focus is on the text control, I can't type the character I'm picking up into the text control because the accelerator table is handling it the event before it goes anywhere else, or preventing the normal event from being fired or propagated. I want to be able to make the event still happen normally, so text will appear in the text control when typed, even if those keys are set to do some other action generally in the window. Is there any event.Skip() type thing I could use here?
发布评论
评论(1)
如果没有更多信息,例如操作系统、wx 版本或显示问题的小示例,很难说出问题是什么。但是,这里有一些关于该主题的示例文章可能会帮助您解决问题:
http://www.blog.pythonlibrary.org/2008/07/02/wxpython-working-with-menus-toolbars-and-accelerators/
http://www.blog.pythonlibrary.org/2010/12/02/wxpython-keyboard-shortcuts-accelerators/
It's hard to say what the issue is without more information, like OS, wx version or a small example that shows the problem. However, here are a couple example articles on the subject that might help you figure out the problem:
http://www.blog.pythonlibrary.org/2008/07/02/wxpython-working-with-menus-toolbars-and-accelerators/
http://www.blog.pythonlibrary.org/2010/12/02/wxpython-keyboard-shortcuts-accelerators/