gpt4 book ai didi

python - wxPython 中的分组事件

转载 作者:行者123 更新时间:2023-12-01 00:37:06 26 4
gpt4 key购买 nike

我想在wxPython中处理两个事件。具体来说,我想将 ON_KEY_DOWN 分组和CHAR调用一个方法的事件,其中包含有关按下哪个键以及该字符将产生什么(如果有的话)的信息。

引用文档:

While both of these functions can be used with the events of wxEVT_KEY_DOWN , wxEVT_KEY_UP and wxEVT_CHAR types, the values returned by them are different for the first two events and the last one. For the latter, the key returned corresponds to the character that would appear in e.g. a text zone if the user pressed the key in it. As such, its value depends on the current state of the Shift key and, for the letters, on the state of Caps Lock modifier. For example, if A key is pressed without Shift being held down, wx.KeyEvent of type wxEVT_CHAR generated for this key press will return (from either GetKeyCode or GetUnicodeKey as their meanings coincide for ASCII characters) key code of 97 corresponding the ASCII value of a . And if the same key is pressed but with Shift being held (or Caps Lock being active), then the key could would be 65, i.e. ASCII value of capital A . However for the key down and up events the returned key code will instead be A independently of the state of the modifier keys i.e. it depends only on physical key being pressed and is not translated to its logical representation using the current keyboard state.

问题是我想要这两个信息。如果按下某个键,我希望得到“原始键码”,例如 KEY_UP 或 KLEY_DOWN ,以及相应的键入字符(如果有)。我试图想出一个解决方案。但是CHAR不会一直引发(某些按键不会产生任何字符)。

The documentation gives the shift and CapLock example, which is somewhat odd. Consider, however, that not all users have a QWERTY keyboard. And this makes much more sense: if a user with an AZERTY keyboard presses on the letter corresponding to the 1 digit in QWERTY, then a & would be written (KEY_DOWN.KeyCode would yield '1' while CHAR.KeyCode would yield &. Does it make more sense?

那么如何拦截这两个事件并使用这两种信息调用一个方法(据了解 CHAR 并不总是会引发)?

我想我已经找到了解决方案:如果我可以访问事件循环或事件处理程序的事件队列,也许我可以检查:如果 KEY_DOWN引发事件,然后我检查下一个事件是否是 CHAR如果是这样,请从 CHAR 检索所需的信息并调用单个方法。但我不知道如何做到这一点,而且文档也没有(据我所知)准确描述 CHAR 何时出现。事件被引发(它可能不完全在 KEY_DOWN 之后,它可能就在 KEU_UP 之前或其他地方)。

另一种可能性是“强制”wx“转换”a KEY_DOWN事件成CHAR事件。同样,这听起来非常具体,但我找不到如何做到这一点。总而言之,我需要的是将“原始按键代码”转换为系统“键入的字符”,我也不需要完整的事件。

代码示例

显然,我无法编写代码,但这里有一个我想要实现的小例子:

import wx

class TestPanel(wx.Panel):

def __init__(self, parent):
super().__init__(parent)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.label = wx.StaticText(self, label="Type something here")
self.text = wx.TextCtrl(self)
self.sizer.Add(self.label)
self.sizer.Add(self.text)
self.SetSizerAndFit(self.sizer)

# Binding isn't possible
#self.text.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)

def OnKeyDown(self, keycode, unicode=None):
"""
The `keycode` key was pressed.

It generated (if anything) the `unicode` character.

"""
print(f"The {key_code} was pressed, generating the {unicode} character.")

app = wx.App()
frame = wx.Frame(None)
panel = TestPanel(frame)
frame.Show()
app.MainLoop()

这里的挑战是将文本字段连接到 OnKeyDown方法以同时传输 key 代码和 unicode key 的方式。 (GetUnincodeKey() 上的 EVT_KEY_DOWN 不起作用,它严格等同于 key_code,这对我没有帮助)。

回顾一下,如果您在键盘上按 a,则 key_code 应为 65(大写 A),unicode 应为 97(小写 a),除非 caplock 已打开。如果您使用的布局与 QWERTY 不同,则 unicode 应始终是出现在文本中的字母(并且此信息仅在 CHAR 事件中给出)。希望能澄清!

最佳答案

如果没有代码,有时很难想象您正在尝试执行的操作。
我认为您缺少的是 event.Skip()

跳过()

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 Without Skip (or equivalently if Skip(false) is used), the event will not be processed any more. 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.

此外,来自文档:

GetUnicodeKey(self) Returns the Unicode character corresponding to this key event.

If the key pressed doesn’t have any character value (e.g. a cursor key) this method will return WXK_NONE . In this case you should use GetKeyCode to retrieve the value of the key.

因此我们可以将以下内容绑定(bind)到 KEY_DOWN、KEY_UP 和 CHAR:

def EvtKey(self, event):
if event.GetUnicodeKey():
print("A value key")
print(chr(event.GetUnicodeKey()))
else:
print("a key event")
print(event.GetKeyCode())
event.Skip()

根据您的评论进行编辑:我建议您看一下 EVT_CHAREVT_CHAR_HOOK

import wx

class TestPanel(wx.Panel):

def __init__(self, parent):
super().__init__(parent)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.label = wx.StaticText(self, label="Type something here")
self.text = wx.TextCtrl(self)
self.sizer.Add(self.label)
self.sizer.Add(self.text)
self.SetSizerAndFit(self.sizer)

self.text.Bind(wx.EVT_CHAR, self.OnKeyDown)
# The choice here will depend on exactly what you want
# CHAR_HOOK will additionally give shift, ctrl, alt etc
#self.text.Bind(wx.EVT_CHAR_HOOK, self.OnKeyDown)

def OnKeyDown(self, event):
key_code = event.GetUnicodeKey()
control_key = event.GetKeyCode()
if key_code == 0:
print(f"The control key {control_key} was pressed.")
else:
unicode = chr(key_code)
"""
The `keycode` key was pressed.

It generated (if anything) the `unicode` character.

"""
print(f"The {key_code} was pressed, generating the {unicode} character.")
event.Skip()

app = wx.App()
frame = wx.Frame(None)
panel = TestPanel(frame)
frame.Show()
app.MainLoop()

关于python - wxPython 中的分组事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57654502/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com