wxKeyEvent Class Reference
[Events]

#include <wx/event.h>

Inheritance diagram for wxKeyEvent:

wxEvent wxKeyboardState wxObject

List of all members.


Detailed 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. 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 wxKeyEvent::ShiftDown(), in general you should use 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.

You may discover how the other keys on your system behave interactively by running the Text Sample wxWidgets sample and pressing some keys in any of the text controls shown in it.

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.

Note:
If a key down (EVT_KEY_DOWN) event is caught and the event handler does not call event.Skip() then the corresponding char event (EVT_CHAR) will not happen. This is by design and enables the programs that handle both types of events to be a bit simpler.

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).

Events:

The following event handler macros redirect the events to member function handlers 'func' with prototypes like:

void handlerFuncName(wxKeyEvent& event)
Event macros:
See also:
wxKeyboardState
Library:  wxCore

Category:  Events

Public Member Functions

 wxKeyEvent (wxEventType keyEventType=wxEVT_NULL)
int GetKeyCode () const
wxUint32 GetRawKeyCode () const
wxUint32 GetRawKeyFlags () const
wxChar GetUnicodeKey () const
wxCoord GetX () const
wxCoord GetY () const
wxPoint GetPosition () const
void GetPosition (long *x, long *y) const

Constructor & Destructor Documentation

wxKeyEvent::wxKeyEvent ( wxEventType  keyEventType = wxEVT_NULL  ) 

Constructor. Currently, the only valid event types are wxEVT_CHAR and wxEVT_CHAR_HOOK.


Member Function Documentation

int wxKeyEvent::GetKeyCode (  )  const

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. See Keycodes for a full list of the virtual key codes.

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().

wxPoint wxKeyEvent::GetPosition (  )  const

Obtains the position (in client coordinates) at which the key was pressed.

void wxKeyEvent::GetPosition ( long *  x,
long *  y 
) const

Obtains the position (in client coordinates) at which the key was pressed.

wxUint32 wxKeyEvent::GetRawKeyCode (  )  const

Returns the raw key code for this event. This is a platform-dependent scan code which should only be used in advanced applications.

Note:
Currently the raw key codes are not supported by all ports, use #ifdef wxHAS_RAW_KEY_CODES to determine if this feature is available.

wxUint32 wxKeyEvent::GetRawKeyFlags (  )  const

Returns the low level key flags for this event. The flags are platform-dependent and should only be used in advanced applications.

Note:
Currently the raw key flags are not supported by all ports, use #ifdef wxHAS_RAW_KEY_CODES to determine if this feature is available.

wxChar wxKeyEvent::GetUnicodeKey (  )  const

Returns the Unicode character corresponding to this key event.

This function is only available in Unicode build, i.e. when wxUSE_UNICODE is 1.

wxCoord wxKeyEvent::GetX (  )  const

Returns the X position (in client coordinates) of the event.

wxCoord wxKeyEvent::GetY (  )  const

Returns the Y position (in client coordinates) of the event.



wxWidgets logo

[ top ]