Help with accelerator table

59 views
Skip to first unread message

Belaala Toufik

unread,
May 3, 2018, 1:22:50 PM5/3/18
to wxPython-users
Hello I need your help with this.
I want to control the three sliders objects in the attached file with accelertor table
for the volume slider I want to set the following shortcut .
f9 increase the volume level and f8 decrease the volume level
for the track slider I want to set the following shortcut .
ctrl + left arrow go backward and ctrl + right arrow go forward
and for the rate slider I want to set the following shortcut.
shift + ctrl + left arrow slow the rate and shift + ctrl + right arrow speed up .
It only works with the play button I set the shortcut to the space bar.
Any help please.

test_2.py

Dietmar Schwertberger

unread,
May 3, 2018, 1:55:43 PM5/3/18
to wxpytho...@googlegroups.com
On 5/3/2018 7:22 PM, Belaala Toufik wrote:
> It only works with the play button I set the shortcut to the space bar.
> Any help please.

I don't think that an accelerator table can create anything else than
menu or button commands.

You could implement a handler for EVT_CHAR_HOOK and then
programmatically increase/decrease the slider values.

The handler for EVT_CHAR_HOOK will be called before EVT_KEY_DOWN, so
don't forget to call event.Skip() for any key combination that you don't
handle.


Regards,

Dietmar


Dietmar Schwertberger

unread,
May 3, 2018, 2:01:00 PM5/3/18
to wxpytho...@googlegroups.com
P.S.: You need to bind the handler for the panel or frame, not the controls.

So e.g.

class MainPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self,parent)
        self.Bind(wx.EVT_CHAR_HOOK, self.on_char)


Belaala Toufik

unread,
May 4, 2018, 6:07:37 AM5/4/18
to wxPython-users
Hello .
Thanks for your reply can you give me full example please I don't understand wx very well.

Dietmar Schwertberger

unread,
May 4, 2018, 1:17:09 PM5/4/18
to wxpytho...@googlegroups.com
On 5/4/2018 12:07 PM, Belaala Toufik wrote:
Hello .
Thanks for your reply can you give me full example please I don't understand wx very well.

Indeed, it seems that there's no good example online.

Please find attached your updated example file to handle: "shift + ctrl + right arrow -> increase rate".

The relevant change:

class MainPanel(wx.Panel):
    def __init__(self, parent):
        ...
        self.Bind(wx.EVT_CHAR_HOOK, self.on_char)

    def on_char(self, event):
        if event.ShiftDown() and event.ControlDown() and event.GetKeyCode()==wx.WXK_RIGHT:
            # shift + ctrl + right arrow -> increase rate
            self.rate.SetValue( self.rate.GetValue() + 1 )
        else:
            # don't handle
            event.Skip()


Regards,
Dietmar
test_2.py

Tim Roberts

unread,
May 4, 2018, 1:37:55 PM5/4/18
to wxpytho...@googlegroups.com
Well, what have you tried?  You clearly have the example of the space
bar to show how this is done.  From that example, you ought to be able
to make up the rest of the accelerator table entries.

However, you have to think about what you're doing.  You cannot just
have your accelerator entries call the same handler as the sliders.  The
two events generate different event objects.  Your onVolume handler, for
example, fetches the new volume value from self.volume, but when your
accelerator key fires, that number will not have changed.  Your
accelerator key for F8 and F9 will need to fetch and adjust the current
volume value, like:

    newvol = self.currentVolume + 1
    if newvol < 100:
        self.currentVolume = newvol
        self.media.SetVolume( newvol / 100.0 )
        self.volume.SetValue( newvol )

You can either send the F8 and F9 events to different handlers, or use
the same handler and use event.GetInt() to notice which one you got.   
And since you'll be using those last three lines in several places, you
might consider adding a "def SetVolume( self, xxx )" method so you don't
repeat yourself.

None of what you've asked is very hard, but you shouldn't be asking us
to write it for you.  You have everything you need.

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

Tim Roberts

unread,
May 4, 2018, 1:41:59 PM5/4/18
to wxpytho...@googlegroups.com
Dietmar Schwertberger wrote:
> On 5/4/2018 12:07 PM, Belaala Toufik wrote:
>> Hello .
>> Thanks for your reply can you give me full example please I don't
>> understand wx very well.
>
> Indeed, it seems that there's no good example online.
>
> Please find attached your updated example file to handle: "shift +
> ctrl + right arrow -> increase rate".
>

Although your method can be made to work, it can also be done with an
accelerator table.  The accelerator entries end up firing wx.EVT_MENU
events, but those work perfectly well even if you don't actually have a
menu.

I tend to think of character hooks as a last resort, when more normal
methods of handling don't work.  I suppose it is personal preference.
Reply all
Reply to author
Forward
0 new messages