Accelerator tables and loops

Hi all,
I'm a lazy person, and prone to typing errors, so I made me a function on my class of wx.Frame:

  def addAccelerator(self, mods, key, action):
   newId = wx.NewId()
   self.Bind(wx.EVT_MENU, action, id = newId)
   self._acceleratorTable.append((mods, key, newId))

Then, just before I show the frame, I do:
self.SetAcceleratorTable(wx.AcceleratorTable(self._acceleratorTable))

All that works... What doesn't seem to work is this:

   for key in range(44, 94):
    for modifiers in [wx.ACCEL_NORMAL, wx.ACCEL_SHIFT, wx.ACCEL_CTRL|wx.ACCEL_SHIFT]:
     self.addAccelerator(modifiers, key, lambda event: self.onKey(modifiers, key))

What I'm trying to do is add accelerators for every key on the keyboard, then add in shift with those keys, and control shift. But when I run the program, and press a key, no matter which key I press, I get keycode 93, with modifiers 6 (wx.ACCEL_CTRL|wx.ACCEL_SHIFT).

So it seems as though the table is only acknowledging the last key entered, even though printing out the table gives millions of entries.

Any thoughts on why? Not sure if this is a wx thing, or a me not know enough python thing, but I thought I'd ask here.

Cheers,

Chris Norman wrote:

All that works... What doesn't seem to work is this:

   for key in range(44, 94):
    for modifiers in [wx.ACCEL_NORMAL, wx.ACCEL_SHIFT,
wx.ACCEL_CTRL|wx.ACCEL_SHIFT]:
     self.addAccelerator(modifiers, key, lambda event:
self.onKey(modifiers, key))

What I'm trying to do is add accelerators for every key on the keyboard,
then add in shift with those keys, and control shift.

This is SO not the right way to handle this. You're adding 150 entries
to the accelerator table. Why wouldn't you just use EVT_CHAR or
EVT_KEYUP/EVT_KEYDOWN and do the processing yourself?

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Chris Norman wrote:

All that works... What doesn't seem to work is this:

  for key in range(44, 94):
   for modifiers in [wx.ACCEL_NORMAL, wx.ACCEL_SHIFT,
wx.ACCEL_CTRL|wx.ACCEL_SHIFT]:
    self.addAccelerator(modifiers, key, lambda event:
self.onKey(modifiers, key))

What I'm trying to do is add accelerators for every key on the keyboard,
then add in shift with those keys, and control shift.

This is SO not the right way to handle this. You're adding 150 entries
to the accelerator table. Why wouldn't you just use EVT_CHAR or
EVT_KEYUP/EVT_KEYDOWN and do the processing yourself?

I chose accelerator tables so I didn’t have to add events to every control on the GUI.

Also, the program still seems to be as responsive before.

Finally, some of the keys weren’t getting caught by the wx.EVT_KEY_DOWN events, things like stuff involving the arrow keys. Honestly, I’m not sure if that was WX’s fault, my coding, or the screen reader I’m using with the computer, but I found accelerator tables worked better for me.

Cheers,

···

On 5 Nov 2014, at 18:12, Tim Roberts <timr@probo.com> wrote:

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Isn’t that just moving the processing from one place to another though? Is this a performance vs memory tradeoff here? What’s bad about 150 objects/entries to keep around in RAM, wouldn’t it be a lookup then, versus the various if statements you’d need to process on your own?

···

On Wednesday, November 5, 2014 10:12:18 AM UTC-8, Tim Roberts wrote:

Chris Norman wrote:

All that works… What doesn’t seem to work is this:

for key in range(44, 94):

for modifiers in [wx.ACCEL_NORMAL, wx.ACCEL_SHIFT,

wx.ACCEL_CTRL|wx.ACCEL_SHIFT]:

 self.addAccelerator(modifiers, key, lambda event:

self.onKey(modifiers, key))

What I’m trying to do is add accelerators for every key on the keyboard,
then add in shift with those keys, and control shift.

This is SO not the right way to handle this. You’re adding 150 entries

to the accelerator table. Why wouldn’t you just use EVT_CHAR or

EVT_KEYUP/EVT_KEYDOWN and do the processing yourself?

That’s a valid point, and I actually thought about that as I was
typing the comment. You’re quite right, it’s a philosophical issue,
not a technical issue.
I doubt that the accelerator table is implemented as a table
lookup. The typical app has no more than a dozen or so, so it’s
almost certainly a table with a “for” loop.
So, allow me to alter my first sentence to “Gosh, to me, personally,
this doesn’t feel like the right solution.”

···

Nathan McCorkle wrote:

    On Wednesday, November 5, 2014 10:12:18 AM UTC-8, Tim Roberts

wrote:

      This is SO not the right way to handle this.  You're adding

150 entries

      to the accelerator table.  Why wouldn't you just use EVT_CHAR

or

      EVT_KEYUP/EVT_KEYDOWN and do the processing yourself?
      Isn't that just moving the processing from one place to

another though? Is this a performance vs memory tradeoff here?
What’s bad about 150 objects/entries to keep around in RAM,
wouldn’t it be a lookup then, versus the various if statements
you’d need to process on your own?

-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com

That's a valid point, and I actually thought about that as I was

typing the comment. You’re quite right, it’s a philosophical issue,
not a technical issue.
I doubt that the accelerator table is implemented as a table
lookup. The typical app has no more than a dozen or so, so it’s
almost certainly a table with a “for” loop.
So, allow me to alter my first sentence to "Gosh, to me, personally,
this doesn’t feel like the right solution.”

So, just out of interest, how would you implement wx.EVT_KEY_DOWN to achieve the same results? I still want every key on the keyboard to either do wx.Bell(), or the actions indicated in my key definitions. And I want that to be true, no matter what control is focused.

Cheers,

···

Nathan McCorkle wrote:

    On Wednesday, November 5, 2014 10:12:18 AM UTC-8, Tim Roberts

wrote:

      This is SO not the right way to handle this.  You're adding

150 entries

      to the accelerator table.  Why wouldn't you just use EVT_CHAR

or

      EVT_KEYUP/EVT_KEYDOWN and do the processing yourself?
      Isn't that just moving the processing from one place to

another though? Is this a performance vs memory tradeoff here?
What’s bad about 150 objects/entries to keep around in RAM,
wouldn’t it be a lookup then, versus the various if statements
you’d need to process on your own?

-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com

I wrote some code that may or may not be relevant, but it seemed to effectively bind any Window object children. I was passing into my recursive function an event like EVT_KEY_DOWN, my Frame, and an event handler function… which was checking for the ESCAPE key press.

https://groups.google.com/d/msg/wxpython-users/Vt0hiGBsi_Q/xLiWxKUhHM4J

···

On Saturday, November 8, 2014 2:29:40 AM UTC-8, Chris Norman wrote:

So, just out of interest, how would you implement wx.EVT_KEY_DOWN to achieve the same results? I still want every key on the keyboard to either do wx.Bell(), or the actions indicated in my key definitions. And I want that to be true, no matter what control is focused.