*********** wx.KeyEvent *********** Inheritance diagram for `wx.KeyEvent`: | .. inheritance-diagram:: wx.KeyEvent | Description =========== This event class contains information about keypress (character) events. Notice that there are three different kinds of keyboard events in wxWidgets: key down and up events and char events. The difference between the first two is clear -- the first corresponds to a key press and the second to a key release - otherwise they are identical. Just note that if the key is maintained in a pressed state you will typically get a lot of (automatically generated) down events but only one up so it is wrong to assume that there is one up event corresponding to each down one. Both key events provide untranslated key codes while the char event carries the translated one. The untranslated code for alphanumeric keys is always an upper case value. For the other keys it is one of ``WXK_XXX`` values from the keycodes table. The translated key is, in general, the character the user expects to appear as the result of the key combination when typing the text into a text entry zone, for example. A few examples to clarify this (all assume that ``CAPS LOCK`` is unpressed and the standard US keyboard): when the ``'A'`` key is pressed, the key down event key code is equal to ``ASCII A`` == 65. But the char event key code is ``ASCII a`` == 97. On the other hand, if you press both ``SHIFT`` and ``'A'`` keys simultaneously , the key code in key down event will still be just ``'A'`` while the char event key code parameter will now be ``'A'`` as well. Although in this simple case it is clear that the correct key code could be found in the key down event handler by checking the value returned by `ShiftDown <#ShiftDown>`_, in general you should use ``wx.EVT_CHAR`` for this as for non-alphanumeric keys the translation is keyboard-layout dependent and can only be done properly by the system itself. Another kind of translation is done when the control key is pressed: for example, for ``CTRL-A`` key press the key down event still carries the same key code ``'a'`` as usual but the char event will have key code of 1, the ASCII value of this key combination. .. note:: If a key down (``wx.EVT_KEY_DOWN``) event is caught and the event handler does not call `event.Skip()` then the corresponding char event (``wx.EVT_CHAR``) will not happen. This is by design and enables the programs that handle both types of events to be a bit simpler. .. note:: Note for Windows programmers: The key and char events in wxWidgets are similar to but slightly different from Windows ``WM_KEYDOWN`` and ``WM_CHAR`` events. In particular, ``Alt-x`` combination will generate a char event in wxWidgets (unless it is used as an accelerator). .. tip:: be sure to call `event.Skip()` for events that you don't process in key event function, otherwise menu shortcuts may cease to work under Windows. Derived From ^^^^^^^^^^^^^ * `wx.Event `_ Event Handling ^^^^^^^^^^^^^^ ================================================== ================================================== Event Name Description ================================================== ================================================== wx.EVT_KEY_DOWN(func) Process a ``wx.wxEVT_KEY_DOWN`` event (any key has been pressed). wx.EVT_KEY_UP(func) Process a ``wx.wxEVT_KEY_UP`` event (any key has been released). wx.EVT_CHAR(func) Process a ``wx.wxEVT_CHAR`` event. ================================================== ================================================== Methods Summary ^^^^^^^^^^^^^^^ * `__init__ <#__init__>`_ * `AltDown <#AltDown>`_ * `CmdDown <#CmdDown>`_ * `ControlDown <#ControlDown>`_ * `GetKeyCode <#GetKeyCode>`_ * `GetModifiers <#GetModifiers>`_ * `GetPosition <#GetPosition>`_ * `GetPositionTuple <#GetPositionTuple>`_ * `GetRawKeyCode <#GetRawKeyCode>`_ * `GetRawKeyFlags <#GetRawKeyFlags>`_ * `GetUnicodeKey <#GetUnicodeKey>`_ * `GetX <#GetX>`_ * `GetY <#GetY>`_ * `HasModifiers <#HasModifiers>`_ * `MetaDown <#MetaDown>`_ * `SetUnicodeKey <#SetUnicodeKey>`_ * `ShiftDown <#ShiftDown>`_ Properties Summary ^^^^^^^^^^^^^^^^^^ * `KeyCode <#KeyCode>`_ * `Modifiers <#Modifiers>`_ * `Position <#Position>`_ * `RawKeyCode <#RawKeyCode>`_ * `RawKeyFlags <#RawKeyFlags>`_ * `UnicodeKey <#UnicodeKey>`_ * `X <#X>`_ * `Y <#Y>`_ * `m_altDown <#m_altDown>`_ * `m_controlDown <#m_controlDown>`_ * `m_keyCode <#m_keyCode>`_ * `m_metaDown <#m_metaDown>`_ * `m_rawCode <#m_rawCode>`_ * `m_rawFlags <#m_rawFlags>`_ * `m_scanCode <#m_scanCode>`_ * `m_shiftDown <#m_shiftDown>`_ * `m_x <#m_x>`_ * `m_y <#m_y>`_ Class API ========= Methods ^^^^^^^ .. method:: __init__(eventType=wx.wxEVT_NULL) Construct a new `wx.KeyEvent `_. **Parameters:** * `eventType` (eventtype) | **Returns:** `wx.KeyEvent `_ -------- .. method:: AltDown() Returns ``True`` if the ``Alt`` key was down at the time of the key event. | **Returns:** `bool` .. note:: Notice that `GetModifiers <#GetModifiers>`_ is easier to use correctly than this function so you should consider using it in new code. -------- .. method:: CmdDown() ``CMD`` is a pseudo key which is the same as ``Ctrl`` for PC and Unix platforms but the special ``APPLE`` (a.k.a as ``COMMAND``) key under Macs: it makes often sense to use it instead of, say, `ControlDown()` because ``Cmd`` key is used for the same thing under Mac as ``Ctrl`` elsewhere (but ``Ctrl`` still exists, just not used for this purpose under Mac). So for non-Mac platforms this is the same as `ControlDown <#ControlDown>`_ and under Mac this is the same as `MetaDown <#MetaDown>`_. | **Returns:** `bool` -------- .. method:: ControlDown() Returns ``True`` if the control key was down at the time of the key event. | **Returns:** `bool` .. note:: Notice that `GetModifiers <#GetModifiers>`_ is easier to use correctly than this function so you should consider using it in new code. -------- .. method:: GetKeyCode() Returns the virtual key code. ASCII events return normal ASCII values, while non-ASCII events return values such as ``WXK_LEFT`` for the left cursor key. | **Returns:** `int` .. note:: Note that in Unicode build, the returned value is meaningful only if the user entered a character that can be represented in current locale's default charset. You can obtain the corresponding Unicode character using `GetUnicodeKey <#GetUnicodeKey>`_. -------- .. method:: GetModifiers() Return the bitmask of modifier keys which were pressed when this event happened. | **Returns:** `int` .. note:: Notice that this function is easier to use correctly than, for example, `ControlDown <#ControlDown>`_ because when using the latter you also have to remember to test that none of the other modifiers is pressed:: if event.ControlDown() and not event.AltDown() and \ not event.ShiftDown() and not event.MetaDown(): # ... handle Ctrl-XXX ... and forgetting to do it can result in serious program bugs (e.g. program not working with European keyboard layout where ``ALTGR`` key which is seen by the program as combination of ``CTRL`` and ``ALT`` is used). On the other hand, you can simply write:: if event.GetModifiers() == wx.MOD_CONTROL: # ... handle Ctrl-XXX ... with this function. -------- .. method:: GetPosition() Obtains the position (in client coordinates) at which the key was pressed. | **Returns:** `wx.Point <../Widgets/wx.Point.html>`_ -------- .. method:: GetPositionTuple() Find the position of the event, if applicable. | **Returns:** `(x, y)` -------- .. method:: GetRawKeyCode() Returns the raw key code for this event. This is a platform-dependent scan code which should only be used in advanced applications. | **Returns:** `int` -------- .. method:: GetRawKeyFlags() Returns the low level key flags for this event. The flags are platform-dependent and should only be used in advanced applications. | **Returns:** `int` -------- .. method:: GetUnicodeKey() Returns the Unicode character corresponding to this key event. This function is only available in Unicode build, i.e. when ``wx.USE_UNICODE`` is 1. | **Returns:** `string` -------- .. method:: GetX() Returns the `X` position (in client coordinates) of the event. | **Returns:** `long` -------- .. method:: GetY() Returns the `Y` (in client coordinates) position of the event. | **Returns:** `long` -------- .. method:: HasModifiers() Returns ``True`` if either ``CTRL`` or ``ALT`` keys was down at the time of the key event. | **Returns:** `bool` .. note:: Note that this function does not take into account neither ``SHIFT`` nor ``META`` key states (the reason for ignoring the latter is that it is common for ``NUMLOCK`` key to be configured as ``META`` under X but the key presses even while ``NUMLOCK`` is on should be still processed normally). -------- .. method:: MetaDown() Returns ``True`` if the Meta key was down at the time of the key event. | **Returns:** `bool` .. note:: Notice that `GetModifiers <#GetModifiers>`_ is easier to use correctly than this function so you should consider using it in new code. -------- .. method:: SetUnicodeKey(uniChar) Set the Unicode value of the key event, but only if this is a Unicode build of wxPython. **Parameters:** * `uniChar` (int) -------- .. method:: ShiftDown() Returns ``True`` if the shift key was down at the time of the key event. | **Returns:** `bool` .. note:: Notice that `GetModifiers <#GetModifiers>`_ is easier to use correctly than this function so you should consider using it in new code. -------- Properties ^^^^^^^^^^ .. attribute:: KeyCode See `GetKeyCode <#GetKeyCode>`_ .. attribute:: Modifiers See `GetModifiers <#GetModifiers>`_ .. attribute:: Position See `GetPosition <#GetPosition>`_ .. attribute:: RawKeyCode See `GetRawKeyCode <#GetRawKeyCode>`_ .. attribute:: RawKeyFlags See `GetRawKeyFlags <#GetRawKeyFlags>`_ .. attribute:: UnicodeKey See `GetUnicodeKey <#GetUnicodeKey>`_ and `SetUnicodeKey <#SetUnicodeKey>`_ .. attribute:: X See `GetX <#GetX>`_ .. attribute:: Y See `GetY <#GetY>`_ .. attribute:: m_altDown See `AltDown <#AltDown>`_ .. warning:: This is deprecated! Use `GetModifiers <#GetModifiers>`_ instead. .. attribute:: m_controlDown See `ControlDown <#ControlDown>`_ .. warning:: This is deprecated! Use `GetModifiers <#GetModifiers>`_ instead. .. attribute:: m_keyCode See `GetKeyCode <#GetKeyCode>`_ .. warning:: This is deprecated! Use `GetKeyCode <#GetKeyCode>`_ instead. .. attribute:: m_metaDown See `MetaDown <#MetaDown>`_ .. warning:: This is deprecated! Use `GetModifiers <#GetModifiers>`_ instead. .. attribute:: m_rawCode See `GetRawKeyCode <#GetRawKeyCode>`_ .. attribute:: m_rawFlags See `GetRawKeyFlags <#GetRawKeyFlags>`_ .. attribute:: m_scanCode .. attribute:: m_shiftDown See `ShiftDown <#ShiftDown>`_ .. warning:: This is deprecated! Use `GetModifiers <#GetModifiers>`_ instead. .. attribute:: m_x See `GetX <#GetX>`_ .. warning:: This is deprecated! Use `GetX <#GetX>`_ instead. .. attribute:: m_y See `GetY <#GetY>`_ .. warning:: This is deprecated! Use `GetY <#GetY>`_ instead.