Esc key from menu

I believe I have found a bug in wxpython (or wxWidgets). I am using WIndows 11, wxpython 4.2.1 wxWidgets 3.2.2.1, Python 3.11.5. Take this simple program (simplified from Getting Started - wxPyWiki):
import wx
class MainWindow(wx.Frame):
def init(self, parent, title):
wx.Frame.init(self, parent, title=title, size=(400,400))
wx.TextCtrl(self, style=wx.TE_MULTILINE)
filemenu = wx.Menu()
filemenu.Append(wx.ID_EXIT, “E&xit”)
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"&File")
self.SetMenuBar(menuBar)
self.Show(True)
app = wx.App(False)
frame = MainWindow(None, “Sample editor”)
app.MainLoop()
Hit Alt-F then Esc: the menu does disappear but the TextCtrl does not properly regain the focus: the first charer typed is ignored except there is a bleep.
The only fix I have found that works is:

  1. TextCtrl traps EVT_MENU_CLOSE
  2. Save mouse position
  3. Simulate a click with wx.UIActionSimulator
  4. Restore mouse position
  5. If the menu is used to say pop up a dialog, do wx.Yield() before popping it up (otherwise there is a bleep because the dialog sees a click outside itself).
    Rob Cliffe

Sorry step 3 should read
Simulate a mouse click on the frame title (or anywhere on the app but not on the TextCtrl or one of its scrollbars)
And “charer” was a typo for “character”.

In your post, if you put three backtick characters ` before the first line of code and three backtick characters after the last line of code, then the forum software won’t reformat it (which loses the double underscores and indentation and changes the quotes into invalid characters).

e.g.

import wx

class MainWindow(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(400,400))
        wx.TextCtrl(self, style=wx.TE_MULTILINE)
        filemenu = wx.Menu()
        filemenu.Append(wx.ID_EXIT, "E&xit")
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"&File")
        self.SetMenuBar(menuBar)
        self.Show(True)

app = wx.App(False)
frame = MainWindow(None, "Sample editor")
app.MainLoop()

I ran that code using wxPython 4.2.1 gtk3 (phoenix) wxWidgets 3.2.4 + Python 3.12.3 + Linux Mint 22.
After pressing Alt-F then Esc the TextCtrl did regain the focus and no typed characters were lost. It appears to be working as expected on Linux.

do Esc twice (there is a note menu shortcuts and Windows) :sweat_smile:

or you simply kill the ALT-F hot key (this hotkey may be the stumbling block on Windows here :roll_eyes:)

import wx

class MainWindow(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(400,400))
        wx.TextCtrl(self, style=wx.TE_MULTILINE)
        filemenu = wx.Menu()
        filemenu.Append(wx.ID_EXIT, "E&xit")
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"&File")
        self.SetMenuBar(menuBar)
        self.hotkey = wx.NewIdRef()
        self.RegisterHotKey(self.hotkey, wx.MOD_ALT, 70)
        self.Show(True)

    def hotkey(self, _):
        pass

app = wx.App(False)
frame = MainWindow(None, "Sample editor")
app.MainLoop()