简体   繁体   English

wx.EVT_KEY_DOWN - evt.skip() 不起作用

[英]wx.EVT_KEY_DOWN - evt.skip() doesn't work

I created a gui in wxpython.我在 wxpython 中创建了一个 gui。 I tried to use wx.EVT_KEY_DOWN.我尝试使用 wx.EVT_KEY_DOWN。 When I press a key, I want to know if the key changed the messageTxt (It was't a key such as right, shift, alt etc...).当我按下一个键时,我想知道该键是否改变了 messageTxt(它不是一个键,如右、移、alt 等......)。 I prints the messageTxt before and after the evt.Skip() but it doen't changes, only in the second char i can see the last changes.我在 evt.Skip() 之前和之后打印 messageTxt 但它没有改变,只有在第二个字符中我才能看到最后的变化。 Someone know how do I get the new messageTxt after the evt.Skip()?有人知道如何在 evt.Skip() 之后获得新的 messageTxt? By that, I could compare the text before the skipping and after and get the result it there was a change.通过那个,我可以比较跳过之前和之后的文本,并得到结果有变化。 Here is some code that explaines the problem.这是一些解释问题的代码。

import wx
from wx.stc import StyledTextCtrl


def On_KeyDown(evt):
    x, y = messageTxt.GetSelection()
    # If something is selected, de-select it
    if x != y:
        messageTxt.SetEmptySelection(y)
    else:
        print("Before skipping", messageTxt.GetText())
        evt.Skip()
        print("After skipping", messageTxt.GetText())


app = wx.App()
frame = wx.Frame(None, -1, title='2', pos=(0, 0), size=(500, 500))
frame.Show(True)
messageTxt = StyledTextCtrl(frame, id=wx.ID_ANY, pos=(0, 0), size=(100 * 3, 100),
                            style=wx.TE_MULTILINE, name="File")

messageTxt.Bind(wx.EVT_KEY_DOWN, On_KeyDown)

app.SetTopWindow(frame)
app.MainLoop()

Your problem is that EVT_KEY_DOWN is working.您的问题是EVT_KEY_DOWN正在工作。
The key down event occurs before the key itself is recognised.按键按下事件发生在按键本身被识别之前。
You should check the EVT_KEY_UP and then test for your special keys.您应该检查EVT_KEY_UP ,然后测试您的special键。

import wx
from wx.stc import StyledTextCtrl

def On_KeyDown(evt):
    x, y = messageTxt.GetSelection()
    # If something is selected, de-select it
    if x != y:
        messageTxt.SetEmptySelection(y)
    else:
        evt.Skip()

def On_KeyUp(evt):
    print("Text :", messageTxt.GetText())
    k = evt.GetKeyCode()
    if k in (wx.WXK_SHIFT,wx.WXK_ALT,wx.WXK_CONTROL,wx.WXK_DOWN):
        print('Special key')
    evt.Skip()

app = wx.App()
frame = wx.Frame(None, -1, title='2', pos=(0, 0), size=(500, 500))
frame.Show(True)
messageTxt = StyledTextCtrl(frame, id=wx.ID_ANY, pos=(0, 0), size=(100 * 3, 100),
                            style=wx.TE_MULTILINE, name="File")

messageTxt.Bind(wx.EVT_KEY_DOWN, On_KeyDown)
messageTxt.Bind(wx.EVT_KEY_UP, On_KeyUp)

app.SetTopWindow(frame)
app.MainLoop()

However you will need to seperate these functions into wx.EVT_KEY_UP and wx.EVT_KEY_DOWN functions or they will work at cross purposes to each other ie either the key changing test will work and the selection fails or vice-versa.但是,您需要将这些函数分离为 wx.EVT_KEY_UP 和 wx.EVT_KEY_DOWN 函数,否则它们将相互交叉工作,即密钥更改测试将起作用而选择失败,反之亦然。

You may be misunderstanding wx.event.Skip()你可能误解了wx.event.Skip()

Skip(self, skip=True) This method can be used inside an event handler to control whether further event handlers bound to this event will be called after the current one returns. Skip(self, skip=True) 该方法可以在事件处理程序中使用,以控制在当前事件返回后是否将调用绑定到此事件的其他事件处理程序。

Without Skip (or equivalently if Skip(false) is used), the event will not be processed any more.如果没有 Skip(或等效地,如果使用 Skip(false)),将不再处理该事件。 If Skip(true) is called, the event processing system continues searching for a further handler function for this event, even though it has been processed already in the current handler.如果 Skip(true) 被调用,事件处理系统会继续为这个事件寻找进一步的处理函数,即使它已经在当前处理函数中被处理了。

In general, it is recommended to skip all non-command events to allow the default handling to take place.通常,建议跳过所有非命令事件以允许进行默认处理。 The command events are, however, normally not skipped as usually a single command such as a button click or menu item selection must only be processed by one handler.但是,通常不会跳过命令事件,因为通常单个命令(例如按钮单击或菜单项选择)只能由一个处理程序处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM