Enabling Keyboard/Button Shortcut

Hi everyone,
I'm trying to add a Keyboard shortcut to a Button and can't seem to get it to work:
Below is what I have so far:

import wx, os

class TestKey(wx.Frame):
  def __init__(self, parent, id, title, position, size):
    wx.Frame.__init__(self, parent, id, title, position, size)

    d = wx.Button(self, -1, "Press Me")
    wx.EVT_BUTTON(self, d.GetId(), self.OnKeyDown)
    wx.EVT_KEY_DOWN(self,self.OnKeyDown)

  def OnKeyDown(self, event):
    key = event.KeyCode()
    if key == WXK_UP:
      print "Up Key Pressed"
    event.Skip()
    
class App(wx.App):
  def OnInit(self):
    
    frame = TestKey(wx.NULL, -1, "Key Press Test", wx.DefaultPosition,(200,200))
    self.SetTopWindow(frame)
    frame.Show(True)
    return True

if __name__ == "__main__":
  app = App(0)
  app.MainLoop()

If I press the "Up" key, nothing is printed.

I tried the following variations:

    wx.EVT_KEY_DOWN(parent,self.OnKeyDown)
    wx.EVT_KEY_DOWN(wx.Frame,self.OnKeyDown)
    wx.EVT_KEY_DOWN(d,self.OnKeyDown)

but "Up" key still isn't detecteed

What am I missing?

Thanks in advance.
-gohaku

gohaku wrote:

Hi everyone,
I'm trying to add a Keyboard shortcut to a Button and can't seem to get it to work:

Key/Char events are only delivered to the window that has the keyboard focus. If you want to have a shortcut key that is available when any window in the frame has the focus then use an accelerator key on a menu item (or use an wx.AcceleratorTable directly if you don't have a menu) and catch the event with EVT_MENU.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

I tried to get AcceleratorTable to work but can't:
import wx, os

class TestKey(wx.Frame):
         def __init__(self, parent, id, title, position, size):
                 wx.Frame.__init__(self, parent, id, title, position, size)
                 self.frame.SetAcceleratorTable(wxAcceleratorTable ([(wxACCEL_NORMAL, ord('P'), self.OnKeyDown)]) )
                 d = wx.Button(self, -1, "Press Me")
                 wx.EVT_BUTTON(self, d.GetId(), self.OnKeyDown)
                 wx.EVT_KEY_DOWN(self,self.OnKeyDown)

         def OnKeyDown(self, event):
                 key = event.KeyCode()
                 if key == WXK_UP:
                         print "Up Key Pressed"
                 event.Skip()

class App(wx.App):
         def OnInit(self):

                 frame = TestKey(wx.NULL, -1, "Key Press Test",wx.DefaultPosition,(200,200))
                 self.SetTopWindow(frame)
                 frame.Show(True)
                 return True

if __name__ == "__main__":
         app = App(0)
         app.MainLoop()

I get back the following:

Traceback (most recent call last):
   File "keydown.py", line 26, in ?
     app = App(0)
   File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/wxPython/wx.py", line 1951, in __init__
     _wxStart(self.OnInit)
   File "keydown.py", line 20, in OnInit
     frame = TestKey(wx.NULL, -1, "Key Press Test",wx.DefaultPosition,(200,200))
   File "keydown.py", line 6, in __init__
     self.frame.SetAcceleratorTable(wxAcceleratorTable ([(wxACCEL_NORMAL, ord('P'), self.OnKeyDown)]) )
AttributeError: TestKey instance has no attribute 'frame'

How do I add wxAcceleratorTable to my script?

Thanks again.
-gohaku

···

On Apr 26, 2004, at 4:10 PM, Robin Dunn wrote:

Key/Char events are only delivered to the window that has the keyboard focus. If you want to have a shortcut key that is available when any window in the frame has the focus then use an accelerator key on a menu item (or use an wx.AcceleratorTable directly if you don't have a menu) and catch the event with EVT_MENU.

gohaku wrote:

Key/Char events are only delivered to the window that has the keyboard focus. If you want to have a shortcut key that is available when any window in the frame has the focus then use an accelerator key on a menu item (or use an wx.AcceleratorTable directly if you don't have a menu) and catch the event with EVT_MENU.

I tried to get AcceleratorTable to work but can't:
import wx, os

class TestKey(wx.Frame):
        def __init__(self, parent, id, title, position, size):
                wx.Frame.__init__(self, parent, id, title, position, size)
                self.frame.SetAcceleratorTable(wxAcceleratorTable ([(wxACCEL_NORMAL, ord('P'), self.OnKeyDown)]) )
                d = wx.Button(self, -1, "Press Me")
                wx.EVT_BUTTON(self, d.GetId(), self.OnKeyDown)
                wx.EVT_KEY_DOWN(self,self.OnKeyDown)

        def OnKeyDown(self, event):
                key = event.KeyCode()
                if key == WXK_UP:
                        print "Up Key Pressed"
                event.Skip()

class App(wx.App):
        def OnInit(self):

                frame = TestKey(wx.NULL, -1, "Key Press Test",wx.DefaultPosition,(200,200))
                self.SetTopWindow(frame)
                frame.Show(True)
                return True

if __name__ == "__main__":
        app = App(0)
        app.MainLoop()

I get back the following:

Traceback (most recent call last):
  File "keydown.py", line 26, in ?
    app = App(0)
  File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/site-packages/wxPython/wx.py", line 1951, in __init__
    _wxStart(self.OnInit)
  File "keydown.py", line 20, in OnInit
    frame = TestKey(wx.NULL, -1, "Key Press Test",wx.DefaultPosition,(200,200))
  File "keydown.py", line 6, in __init__
    self.frame.SetAcceleratorTable(wxAcceleratorTable ([(wxACCEL_NORMAL, ord('P'), self.OnKeyDown)]) )
AttributeError: TestKey instance has no attribute 'frame'

How do I add wxAcceleratorTable to my script?

First, read the exception message. It's telling you that there is no self.frame, which shouldn't be surprising since you never assign anything to self.frame. Try using just self.SetAcceleratorTable.

Second, to get accelerator key events you need to bind EVT_MENU events to the ID specified in the accelerator table. See attached.

accel_test2.py (1.06 KB)

···

On Apr 26, 2004, at 4:10 PM, Robin Dunn wrote:

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!