I am toying with the idea of adding some 'right-click' popup menu
functionality to my app.
If I do, I will want to allow use of the 'menu' key on the keyboard (don't
know the correct term, but usually at bottom right between Alt and Ctrl) as
an alternative.
I have found that on my laptop running Windows 2003 Server, this key returns
399, which has a constant of WXK_WINDOWS_MENU, but on my FC5 server, it
returns 309, which has a constant of WXK_MENU.
It is no problem to test for 'evt.KeyCode() in
(wx.WXK_MENU,wx.WXK_WINDOWS_MENU)', but I just wondered if this is the
recommended method or if there is a better way.
Seems pretty straightforward to me, I would go with it unless/until you
find something better - which is only really possible if there is some
sort of 'menu button has been pressed' event binding built into wxPython
(which I doubt).
- Josiah
···
"Frank Millman" <frank@chagford.com> wrote:
It is no problem to test for 'evt.KeyCode() in
(wx.WXK_MENU,wx.WXK_WINDOWS_MENU)', but I just wondered if this is the
recommended method or if there is a better way.
It is no problem to test for 'evt.KeyCode() in
(wx.WXK_MENU,wx.WXK_WINDOWS_MENU)', but I just wondered if this is the
recommended method or if there is a better way.
Seems pretty straightforward to me, I would go with it unless/until you
find something better - which is only really possible if there is some
sort of 'menu button has been pressed' event binding built into wxPython
(which I doubt).
EVT_CONTEXT_MENU should do it, at least on Windows. It will be sent for either the menu key, or an unhandled right-click.
--
Robin Dunn
Software Craftsman http://wxPython.org Java give you jitters? Relax with wxPython!
Josiah Carlson wrote:
>> It is no problem to test for 'evt.KeyCode() in
>> (wx.WXK_MENU,wx.WXK_WINDOWS_MENU)', but I just wondered if this is
>> the recommended method or if there is a better way.
>
> Seems pretty straightforward to me, I would go with it unless/until
> you find something better - which is only really possible
if there is
> some sort of 'menu button has been pressed' event binding
built into
> wxPython (which I doubt).
EVT_CONTEXT_MENU should do it, at least on Windows. It will
be sent for either the menu key, or an unhandled right-click.
Good news and bad news.
Good news - EVT_CONTEXT_MENU responds to both menu key and right-click on
both MSW and GTK2, using a TextCtrl for testing.
Getting the position seems a bit awkward. The event has a GetPosition()
method. If the menu key is pressed it returns (-1,-1). if the mouse is
right-clicked it returns the position relative to the entire screen, not the
active frame. You can calculate the desired position regardless, as the
event is bound to a particular control, and you should know the position of
the control, but then you are ignoring GetPosition(). There is also a
SetPosition() method. I read the docs, but I did not understand how you are
supposed to use it.
Bad news - I actually want to trigger the event from a TreeCtrl or
TreeListCtrl, by clicking/pressing on a given tree item, detecting the event
and detecting which item triggered it. I tried binding EVT_CONTEXT_MENU to
the TreeCtrl, but it does not respond to the event at all.
In the meantime I will continue checking for EVT_RIGHT_UP and 'evt.KeyCode()
in (wx.WXK_MENU,wx.WXK_WINDOWS_MENU)', which I think will do the job.
It is no problem to test for 'evt.KeyCode() in (wx.WXK_MENU,wx.WXK_WINDOWS_MENU)', but I just wondered if this is the recommended method or if there is a better way.
Seems pretty straightforward to me, I would go with it unless/until you find something better - which is only really possible
if there is
some sort of 'menu button has been pressed' event binding
built into
wxPython (which I doubt).
EVT_CONTEXT_MENU should do it, at least on Windows. It will be sent for either the menu key, or an unhandled right-click.
Good news and bad news.
Good news - EVT_CONTEXT_MENU responds to both menu key and right-click on
both MSW and GTK2, using a TextCtrl for testing.
Getting the position seems a bit awkward. The event has a GetPosition()
method. If the menu key is pressed it returns (-1,-1). if the mouse is
right-clicked it returns the position relative to the entire screen, not the
active frame. You can calculate the desired position regardless, as the
event is bound to a particular control, and you should know the position of
the control, but then you are ignoring GetPosition().
You can use ScreenToClient to convert the coords if needed. OTOH, PopupMenu has been changed to position the menu near the cursor if no position is given, and it has been suggested that it is better to let it position itself in this way, in case there are ever platform differences in how the context menu should be shown
There is also a
SetPosition() method. I read the docs, but I did not understand how you are
supposed to use it.
It is for the sender of the event.
Bad news - I actually want to trigger the event from a TreeCtrl or
TreeListCtrl, by clicking/pressing on a given tree item, detecting the event
and detecting which item triggered it. I tried binding EVT_CONTEXT_MENU to
the TreeCtrl, but it does not respond to the event at all.
Probably because the treectrl is consuming the mouse event. However it generates a EVT_TREE_ITEM_MENU event that you can use instead.
···
"Frank Millman" <frank@chagford.com> wrote:
--
Robin Dunn
Software Craftsman http://wxPython.org Java give you jitters? Relax with wxPython!
Frank Millman wrote:
>
> Good news and bad news.
>
> Good news - EVT_CONTEXT_MENU responds to both menu key and
right-click
> on both MSW and GTK2, using a TextCtrl for testing.
>
> Getting the position seems a bit awkward. The event has a
> GetPosition() method. If the menu key is pressed it returns
(-1,-1).
> if the mouse is right-clicked it returns the position
relative to the
> entire screen, not the active frame. You can calculate the desired
> position regardless, as the event is bound to a particular control,
> and you should know the position of the control, but then
you are ignoring GetPosition().
You can use ScreenToClient to convert the coords if needed.
OTOH, PopupMenu has been changed to position the menu near
the cursor if no position is given, and it has been suggested
that it is better to let it position itself in this way, in
case there are ever platform differences in how the context
menu should be shown
I can see that this is a very subjective issue. For example, the demo uses a
StaticText control to demonstrate a popup menu, without providing a menu
position. On both MSW and GTK2, the menu appears next to the mouse cursor,
which looks correct. If I try pressing the menu key, MSW does not react at
all, whereas GTK2 does pop up the menu, but it appears next to the mouse
cursor, even if it is nowhere near the StaticText control. I think I will
stick to positioning it manually until I understand things better.
>
> Bad news - I actually want to trigger the event from a TreeCtrl or
> TreeListCtrl, by clicking/pressing on a given tree item,
detecting the
> event and detecting which item triggered it. I tried binding
> EVT_CONTEXT_MENU to the TreeCtrl, but it does not respond
to the event at all.
Probably because the treectrl is consuming the mouse event.
However it generates a EVT_TREE_ITEM_MENU event that you can
use instead.
Thanks for the pointer. It is in the docs, so I should have looked first -
sorry about that.
I tried it, but unfortunately it does not seem to work with a TreeListCtrl,
so I will stick with my existing method for now.