wxPython:绑定wx.EVT_CHAR_HOOK禁用TextCtrl退格 [英] wxPython: binding wx.EVT_CHAR_HOOK disables TextCtrl backspace

查看:1248
本文介绍了wxPython:绑定wx.EVT_CHAR_HOOK禁用TextCtrl退格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个wx.TextCtrl,我想要能够输入,但也可以检测按键,如UP,DOWN,RETURN,ESC。

I have a wx.TextCtrl and I want to be able to type in it, but also detect key presses such as UP, DOWN, RETURN, ESC.

所以我绑定wx.EVT_KEY_DOWN来识别任何按键,wx.EVT_CHAR_HOOK做同样的事情,即使TextCtrl有焦点。

So I binded wx.EVT_KEY_DOWN to recognize any key press, and wx.EVT_CHAR_HOOK to do the same thing even when TextCtrl has focus.

self.Bind(wx.EVT_KEY_DOWN, self.keyPressed)
self.Bind(wx.EVT_CHAR_HOOK, self.keyPressed)

按键UP,DOWN,RETURN,ESC被识别并工作正常,但由于绑定EVT_CHAR_HOOK当我输入TextCtrl时,我不能使用LEFT RIGHT BACKSPACE SHIFT。

Key presses UP, DOWN, RETURN, ESC were recognized and working fine, but due to binding EVT_CHAR_HOOK I cannot use LEFT RIGHT BACKSPACE SHIFT anymore when I type in the TextCtrl.

任何建议?

推荐答案

您应该在事件处理程序的末尾调用 event.Skip()来进一步传播。这对我有用:

You should call event.Skip() at the end of the event handler to propagate it further. This works for me:

import wx

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.panel = wx.Panel(self)
        self.text = wx.TextCtrl(self.panel)
        self.text.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
        self.text.Bind(wx.EVT_KEY_UP, self.OnKeyUp)

        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.text, 1)

        self.panel.SetSizerAndFit(self.sizer)  
        self.Show()

    def OnKeyDown(self, e):      
        code = e.GetKeyCode()
        if code == wx.WXK_ESCAPE:
            print("Escape")
        if code == wx.WXK_UP:
            print("Up")
        if code == wx.WXK_DOWN:
            print("Down")
        e.Skip()

    def OnKeyUp(self, e):
        code = e.GetKeyCode()
        if code == wx.WXK_RETURN:
            print("Return")
        e.Skip()


app = wx.App(False)
win = MainWindow(None)
app.MainLoop()

这篇关于wxPython:绑定wx.EVT_CHAR_HOOK禁用TextCtrl退格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆