Text input not working in wxPython (characters, tab, enter/return keys) - solutions available
Keyboard issues in wxPython may have multiple causes at the same time, so try apply all the fixes below until it works.
Issue 1/3
If characters don't work in inputs make sure you have set the wx.WANTS_CHARS style to all parent controls, including top level window.
Issue 2/3
Some wx controls may still cause issues and to resolve them is to remove the wx.TAB_TRAVERSAL style for all parent controls. See example function that removes such style recurrently for all parent controls:
def fixKeyboard(ctrl):
while ctrl:
ctrl.SetWindowStyle(ctrl.GetWindowStyle() & ~wx.TAB_TRAVERSAL)
ctrl = ctrl.GetParent()
Issue 3/3
Some controls can still cause issues even with wx.WANTS_CHARS style applied and wx.TAB_TRAVERSAL style revmoed. For example when using the wx.aui.AuiNotebook control the TAB and RETURN (enter) keys don't work. To fix it you have to bind to wx.EVT_CHAR_HOOK and send these key events manually to CEF browser with the Browser.SendKeyEvent function. You can find sample code here: https://github.com/cztomczak/cefpython/issues/508#issuecomment-468456083
What CEF version? 31.1 is the latest. What wxPython version? Please paste the whole wx version string that is displayed in the wxpython.py example in console (see for example http://cefpython.googlecode.com/git/cefpython/var/wxpython.png).
sample3.py works fine for me with wx 2.8. Looks like an issue with the wxWidgets flatnotebook control in wx 3.0, the wx.WANTS_CHARS style is not applied. Make sure that yu have this line in sample3.py:
self.tabs.SetWindowStyleFlag(wx.WANTS_CHARS)
Original comment by [email protected] on 15 Dec 2014 at 7:40
See also Issue #80 that explains in details the need for the wx.WANTS_CHARS style for the keyboard to work.
Original comment by [email protected] on 15 Dec 2014 at 7:44
the wx version string is "VERSION_STRING = '3.0.1.1'" the cefpython verison is "version = "31.0" I am sure following code exists in sample.py self.tabs.SetWindowStyleFlag(wx.WANTS_CHARS) I will try 31.1 later
Original comment by [email protected] on 16 Dec 2014 at 5:19
I can confirm this issue. Tested with cefpython3 31.2 for Python 2.7 64bit and wx.version=3.0.2.0 msw (classic). The issue occurs in both sample2.py and sample3.py. The sample1.py is running fine, because it embeds cef browser in main frame and there is no need to use the wx.WANTS_CHARS for main frame.
This issue should be reported to wxWidgets/wxPython (http://trac.wxwidgets.org/).
I haven't tested it yet on other platforms, but I think this is a Windows only issue.
In the meantime you can fix it by propagating the Char event manually to child windows. This can be done either with a global hook to wx.EVT_CHAR. Or if that doesn't work try wx.EVT_CHAR_HOOK. EVT_CHAR_HOOK is a Windows only event I think, kind of a hack for Windows issues with Char events. For more details on wx.EVT_CHAR_HOOK see: http://docs.wxwidgets.org/3.0/classwx_key_event.html .
Original comment by [email protected] on 20 Jan 2015 at 5:46
@comment 5: The fix to propagate Char events to child windows - may require using Windows API (pywin32 extension) to send Char events to CEF browser child windows. You can get native window handle (HWND) by calling browser.GetWindowHandle(). To send the Char event using pywin32 use this code:
c = "a"
win32api.SendMessage(hwnd, win32con.WM_CHAR, ord(c), 0)
To get browser object using chromectrl API use these methods:
ChromeWindow.GetBrowser()
ChromeCtrl.chromeWindow.GetBrowser()
Original comment by [email protected] on 20 Jan 2015 at 6:04
This issue occurs in both wxPython 2.9 and 3.x. In 2.8 wx.WANTS_CHARS style is applied fine.
Original comment by [email protected] on 17 Mar 2015 at 8:11
In wxPython 2.9 issue can be fixed in the wxpython.py example by setting the wx.WANTS_CHARS style on the top-level window.
win_style = wx.DEFAULT_FRAME_STYLE|wx.WANTS_CHARS
parent = wx.GetApp().GetTopWindow()
parent.SetWindowStyle(win_style)
However this does not fix the problem in the sample2.py example.
Original comment by [email protected] on 19 Mar 2015 at 5:57
@comment 7: Have you actually been able to get this method working? I've tried your suggestion (Python 2.7, x64, wxPython 3.x), but the browser instance does not seem to receive the message, i.e. in input controls no chars are being input.
Original comment by [email protected] on 27 May 2015 at 5:42
@comment 9: I have tried to reproduce your solution for wxPython 2.9, but that didn't work either.
Original comment by [email protected] on 27 May 2015 at 6:05
Input char only works in frame, not in panel with latest release of wxpython (> 2.9.x) !!
Original comment by [email protected] on 12 Jul 2015 at 8:08
Hi, I found text typing does not trigger events which type should be cefpython.KEYEVENT_CHAR. That is why it is not working.
This should be reported to the wxPython Issue Tracker: http://trac.wxwidgets.org/
The solution for keyboard issues is to remove the wx.TAB_TRAVERSAL style for all parent controls. Edited the first post in this issue with a patch for the wx/sample2.py example.