Setting up global keybindings without corresponding menu items

I’m trying to set up a global keyboard shortcut for my application. I know how to do this by associating the shortcut with a menu item, but I want to do it without a corresponding menu item. As per the instructions here (http://lists.wxwidgets.org/pipermail/wxpython-users/2003-August/021655.html), I should be able to do this with an accelerator table, but I can’t get it to work.

I’ve included a small, self-contained sample that attempts to bind an event handler with the Ctrl-L shortcut. Can anybody see what I’m doing wrong? Thanks in advance.

I am running wxPython 2.8.8.1 on Ubuntu 8.04.1 Hardy.

···

import wx

class MainWindow(wx.Frame):
def init(self, parent, id, title):
wx.Frame.init (self, parent, -1, title, size=(500,200))

    self.panel = wx.Panel(self, -1)

EVT_TEST_ID = wx.NewId()

wx.EVT_MENU(self, EVT_TEST_ID, self.OnTestEvent)

aTable = wx.AcceleratorTable([
    (wx.ACCEL_CTRL, ord('L'), EVT_TEST_ID)

])

self.SetAcceleratorTable(aTable)

self.Show(True)

def OnTestEvent(self, event):
    print "OnTestEvent fired"

app = wx.App()
frame = MainWindow(None, -1, “Test”)

app.MainLoop()

mercado mercado wrote:

I'm trying to set up a global keyboard shortcut for my application. I know how to do this by associating the shortcut with a menu item, but I want to do it without a corresponding menu item. As per the instructions here (http://lists.wxwidgets.org/pipermail/wxpython-users/2003-August/021655.html), I should be able to do this with an accelerator table, but I can't get it to work.

I've included a small, self-contained sample that attempts to bind an event handler with the Ctrl-L shortcut. Can anybody see what I'm doing wrong? Thanks in advance.

I am running wxPython 2.8.8.1 <http://2.8.8.1> on Ubuntu 8.04.1 Hardy.
---------------------------------------------------------------------
import wx

class MainWindow(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__ (self, parent, -1, title, size=(500,200))
        self.panel = wx.Panel(self, -1)

    EVT_TEST_ID = wx.NewId()

    wx.EVT_MENU(self, EVT_TEST_ID, self.OnTestEvent)

    aTable = wx.AcceleratorTable([
        (wx.ACCEL_CTRL, ord('L'), EVT_TEST_ID)
    ])

    self.SetAcceleratorTable(aTable)

    self.Show(True)

    def OnTestEvent(self, event):
        print "OnTestEvent fired"

app = wx.App()
frame = MainWindow(None, -1, "Test")
app.MainLoop()

Hmmm...this works on Windows XP, '2.8.7.1 (msw-unicode)', Python 2.5.2.

Try changing the event binding to this instead:

self.Bind(wx.EVT_MENU, self.OnTestEvent, id=EVT_TEST_ID)

That's the way I was told to do it...by Robin, I think.

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org

Thanks for the response Mike. Unfortunately, the alternative syntax doesn’t work for me either.

???

···

On Sat, Aug 2, 2008 at 1:15 PM, Mike Driscoll mike@pythonlibrary.org wrote:

Hmmm…this works on Windows XP, ‘2.8.7.1 (msw-unicode)’, Python 2.5.2.

Try changing the event binding to this instead:

self.Bind(wx.EVT_MENU, self.OnTestEvent, id=EVT_TEST_ID)

mercado mercado wrote:

···

On Sat, Aug 2, 2008 at 1:15 PM, Mike Driscoll <mike@pythonlibrary.org > <mailto:mike@pythonlibrary.org>> wrote:

    Hmmm...this works on Windows XP, '2.8.7.1 <http://2.8.7.1>
    (msw-unicode)', Python 2.5.2. <http://2.5.2.>

    Try changing the event binding to this instead:

    self.Bind(wx.EVT_MENU, self.OnTestEvent, id=EVT_TEST_ID)

Thanks for the response Mike. Unfortunately, the alternative syntax doesn't work for me either.

???

I loaded Ubuntu Hardy in my VM and confirmed that it does not work with your sample code. Weird. I did find a recipe in the wiki that might work for you:

http://wiki.wxpython.org/Using%20Multi-key%20Shortcuts

It's a little bit above my head, but it seems to be using an alternative technique. Maybe it can be modified to work for you?

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org

mercado mercado wrote:

I'm trying to set up a global keyboard shortcut for my application. I know how to do this by associating the shortcut with a menu item, but I want to do it without a corresponding menu item. As per the instructions here (http://lists.wxwidgets.org/pipermail/wxpython-users/2003-August/021655.html), I should be able to do this with an accelerator table, but I can't get it to work.

I've included a small, self-contained sample that attempts to bind an event handler with the Ctrl-L shortcut. Can anybody see what I'm doing wrong? Thanks in advance.

Try adding a widget to the frame that can have the keyboard focus. The focus needs to be on some widget within the frame for accelerators attached to the frame to be active.

···

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

I tried adding a button to the panel, and giving the button focus, and now the accelerator table is working as expected.

Thanks Mike and Robin, for your prompt and helpful responses.

···

On Mon, Aug 4, 2008 at 2:31 PM, Robin Dunn robin@alldunn.com wrote:

Try adding a widget to the frame that can have the keyboard focus. The focus needs to be on some widget within the frame for accelerators attached to the frame to be active.