wxKeyEvent(3)
π wxKeyEvent π
okay bestie, so this is the wxKeyEvent(3) page β Erlang module definition, wxWidgets docs vibe, and it's all about key press and release events. grab a snack, this one's a chonker π
β¨ NAME β¨
wxKeyEvent β this event class contains information about key press and release events. periodt. π
πΈ DESCRIPTION πΈ
the main character of this whole event is... the key being pressed or released. that's the headline. π£ you can get at it with either getKeyCode/1 or getUnicodeKey/1.
for printable characters, pls use getUnicodeKey/1 β it works for any keys, including the non-Latin-1 characters you can type when you're using national keyboard layouts, and honestly it just eats. π₯ getKeyCode/1 is the one you want for special characters (think cursor arrow keys, or HOME, or INS, and so on) β those correspond to ?wxKeyCode enum elements above the WXK_START constant. and yeah, getKeyCode/1 also returns the character code for Latin-1 keys, but that's purely for compatibility; the way that it does not work for Unicode characters in general... it will straight up return WXK_NONE for any non-Latin-1 one. π
so for that reason, it's recommended to always use getUnicodeKey/1 and only fall back to getKeyCode/1 if getUnicodeKey/1 returned WXK_NONE β because that means the event corresponds to a non-printable special key. no cap.
both of these functions can be used with the events of wxEVT_KEY_DOWN, wxEVT_KEY_UP and wxEVT_CHAR types, but bestie, the values they return are different for the first two events versus the last one. π³
for wxEVT_CHAR, the key returned corresponds to the character that would appear in, like, 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 the Caps Lock modifier. for example, if the A key is pressed without Shift being held down, the wxKeyEvent of type wxEVT_CHAR generated for that key press will return (from either getKeyCode/1 or getUnicodeKey/1, since their meanings coincide for ASCII characters) key code of 97, which corresponds to 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 code would be 65, i.e. the 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 β it depends only on the physical key being pressed and is not translated to its logical representation using the current keyboard state. π§
such untranslated key codes are defined as follows:
- for the letters, they correspond to the upper case value of the letter.
- for the other alphanumeric keys (e.g.
7or+), the untranslated key code corresponds to the character produced by the key when it is pressed without Shift. e.g. in the standard US keyboard layout, the untranslated key code for the key=/+in the upper right corner of the keyboard is61, which is the ASCII value of=. - for the rest of the keys (i.e. special non-printable keys), it is the same as the normal key code, as no translation is used anyhow.
notice that the first rule applies to all Unicode letters, not just the usual Latin-1 ones. however, for non-Latin-1 letters only getUnicodeKey/1 can be used to retrieve the key code, since getKeyCode/1 just returns WXK_NONE in this case. π
to summarize, because we love a summary: you should handle wxEVT_CHAR if you need the translated key, and wxEVT_KEY_DOWN if you only need the value of the key itself, independent of the current keyboard state. π«Ά
Note: not all key down events may be generated by the user. as an example,wxEVT_KEY_DOWNwith=key code can be generated using the standard US keyboard layout but not using the German one, because the=key corresponds to the Shift-0 key combination in this layout, and the key code for it is0, not=. because of this, you should avoid requiring your users to type key events that might be impossible to enter on their keyboard. be so fr about your users' keyboards. β¨οΈ
another difference between key and char events is that another kind of translation is done for the char ones when the Control key is pressed: char events for ASCII letters in this case carry codes corresponding to the ASCII value of Ctrl-Letter, i.e. 1 for Ctrl-A, 2 for Ctrl-B, and so on until 26 for Ctrl-Z. this is convenient for terminal-like applications and can be completely ignored by all the other ones (if you need to handle Ctrl-A, it is probably a better idea to use the key event rather than the char one, lowkey). notice that currently no translation is done for the presses of [, `, ], ^ and _ keys, which might be mapped to ASCII values from 27 to 31. since version 2.9.2, the enum values WXK_CONTROL_A β¦ WXK_CONTROL_Z can be used instead of the non-descriptive constant values 1-26. iconic glow-up tbh. β¨
finally, modifier keys only generate key events but no char events at all. the modifier keys are WXK_SHIFT, WXK_CONTROL, WXK_ALT and the various WXK_WINDOWS_XXX from ?wxKeyCode enum.
modifier key events are special in one additional aspect: usually the keyboard state associated with a key press is well defined, e.g. shiftDown/1 returns true only if the Shift key was held pressed when the key that generated this event itself was pressed. there is an ambiguity for the key press events for the Shift key itself, however. by convention, it is considered to be already pressed when it is pressed, and already released when it is released. in other words, the wxEVT_KEY_DOWN event for the Shift key itself will have wxMOD_SHIFT in getModifiers/1 and shiftDown/1 will return true, while the wxEVT_KEY_UP event for Shift itself will not have wxMOD_SHIFT in its modifiers and shiftDown/1 will return false. she contains multitudes. π
Tip: you may discover the key codes and modifiers generated by all the keys on your system interactively by running the page_samples_keyboard wxWidgets sample and pressing some keys in it. go press keys, bestie, it's basically a vibe check for your keyboard. π
Note: if a key down (EVT_KEY_DOWN) event is caught and the event handler does not callevent.Skip(), then the corresponding char event (EVT_CHAR) will not happen. this is by design, and it enables the programs that handle both types of events to avoid processing the same key twice. as a consequence, if you do not want to suppress thewxEVT_CHARevents for the keys you handle, always callevent.Skip()in yourwxEVT_KEY_DOWNhandler. not doing so may also prevent accelerators defined using this key from working. π
Note: if a key is maintained in a pressed state, you will typically get a lot of (automatically generated) key down events but only one key up one at the end when the key is released β so it is wrong to assume that there is one up event corresponding to each down one. not me thinking they're 1:1 π
Note: for Windows programmers β the key and char events in wxWidgets are similar to but slightly different from WindowsWM_KEYDOWNandWM_CHARevents. in particular, the Alt-x combination will generate a char event in wxWidgets (unless it is used as an accelerator), and almost all keys, including ones without ASCII equivalents, generate char events too. slay, more char events for everyone. π₯
this class is derived, and can use functions, from:
wxEvent
wxWidgets docs: wxKeyEvent
π¦ Events π¦
use wxEvtHandler:connect/3 with wxKeyEventType to subscribe to events of this type. yes ma'am, that's the whole plug-in-and-go situation. π
β DATA TYPES β
wxKey() :: #wxKey{}.
wxKeyEvent() :: wx:wx_object().
wxKeyEventType() :: char | char_hook | key_down | key_up.
and that's where part one leaves us, bestie β part 2 is coming, don't go anywhere π
π οΈ FUNCTIONS π οΈ
okay bestie, this is the part where we actually do stuff. every single one of these is a method you call on a wxKeyEvent(). let's get into it π
altDown(This) -> boolean() when This :: wxKeyEvent().
returns true if the Alt key is pressed. β
heads up though: getModifiers/1 should usually be used instead of this one. like, it exists, it works, but it's not the main character here, iykyk.
cmdDown(This) -> boolean() when This :: wxKeyEvent().
returns true if the key used for command accelerators is pressed. π«
same as controlDown/1. Deprecated. π (yes, deprecated, we don't gatekeep, we just tell you.)
also: getModifiers/1 should usually be used instead of this one.
controlDown(This) -> boolean() when This :: wxKeyEvent().
returns true if the Control key β or the Apple/Command key under macOS β is pressed. π
this function doesn't distinguish between right and left control keys. nope, it does not care which side you're on, no cap.
and once more for the people in the back: getModifiers/1 should usually be used instead of this one.
getKeyCode(This) -> integer() when This :: wxKeyEvent().
returns the key code of the key that generated this event. π
ASCII symbols return normal ASCII values, while events from special keys such as "left cursor arrow" (WXK_LEFT) return values outside of the ASCII range. see ?wxKeyCode for a full list of the virtual key codes.
note that this method returns a meaningful value only for special non-alphanumeric keys, or if the user entered a Latin-1 character (this includes ASCII and the accented letters found in Western European languages, but not letters of other alphabets such as e.g. Cyrillic). otherwise it simply returns WXK_NONE, and getUnicodeKey/1 should be used to obtain the corresponding Unicode character. π
using getUnicodeKey/1 is in general the right thing to do if you're interested in the characters typed by the user; getKeyCode/1 should be only used for special keys (for which getUnicodeKey/1 returns WXK_NONE). to handle both kinds of keys you might write:
(the manpage is like "you might write:" and thenβ¦ nothing. the snippet did not make it into this copy bestie, I'm as devastated as you are πΈ β the words are all here, the code block just ghosted us.)
getModifiers(This) -> integer() when This :: wxKeyEvent().
return the bit mask of all pressed modifier keys. π
the return value is a combination of wxMOD_ALT, wxMOD_CONTROL, wxMOD_SHIFT and wxMOD_META bit masks. additionally, wxMOD_NONE is defined as 0, i.e. corresponds to no modifiers (see HasAnyModifiers() (not implemented in wx)) and wxMOD_CMD is either wxMOD_CONTROL (MSW and Unix) or wxMOD_META (Mac), see cmdDown/1. see ?wxKeyModifier for the full list of modifiers. β¨
notice that this function is easier to use correctly than, for example, controlDown/1, because when using the latter you also have to remember to test that none of the other modifiers is pressed:
(yep, another "here's the thing you'd write" moment, and again the code is MIA in this copy π)
β¦and forgetting to do it can result in serious program bugs (e.g. program not working with European keyboard layout, where the AltGr key β which is seen by the program as a combination of CTRL and ALT β is used). π¨ on the other hand, you can simply write:
(you guessed it, snippet not present π) β with this function. π
getPosition(This) -> {X :: integer(), Y :: integer()} when This :: wxKeyEvent().
obtains the position (in client coordinates) at which the key was pressed. π
notice that under most platforms this position is simply the current mouse pointer position and has no special relationship to the key event itself. like, it's giving "coordinates" but it's really just where your mouse was chilling. π±οΈ
x and y may be NULL if the corresponding coordinate is not needed.
getRawKeyCode(This) -> integer() when This :: wxKeyEvent().
returns the raw key code for this event. π₯
the flags are platform-dependent and should only be used if the functionality provided by other wxKeyEvent methods is insufficient.
- under MSW, the raw key code is the value of
wParamparameter of the corresponding message. - under GTK, the raw key code is the
keyvalfield of the corresponding GDK event. - under macOS, the raw key code is the
keyCodefield of the correspondingNSEvent.
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. π«Ά (yes, the preprocessor is your friend here, no shame.)
getRawKeyFlags(This) -> integer() when This :: wxKeyEvent().
returns the low level key flags for this event. π©
again: the flags are platform-dependent and should only be used if the functionality provided by other wxKeyEvent methods is insufficient.
- under MSW, the raw flags are just the value of
lParamparameter of the corresponding message. - under GTK, the raw flags contain the
hardware_keycodefield of the corresponding GDK event. - under macOS, the raw flags contain the modifiers state.
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. π«Ά
getUnicodeKey(This) -> integer() when This :: wxKeyEvent().
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 that case you should use getKeyCode/1 to retrieve the value of the key.
this function is only available in Unicode build, i.e. when wxUSE_UNICODE is 1. β
getX(This) -> integer() when This :: wxKeyEvent().
returns the X position (in client coordinates) of the event. β‘οΈ
see: getPosition/1
getY(This) -> integer() when This :: wxKeyEvent().
returns the Y position (in client coordinates) of the event. β¬οΈ
see: getPosition/1
hasModifiers(This) -> boolean() when This :: wxKeyEvent().
returns true if Control or Alt are pressed. β
checks if Control, Alt, or β under macOS only β the Command key are pressed (notice that the real Control key is still taken into account under OS X too).
this method returns false if only Shift is pressed, for compatibility reasons, and also because pressing Shift usually doesn't change the interpretation of key events. see HasAnyModifiers() (not implemented in wx) if you want to take Shift into account as well. π
metaDown(This) -> boolean() when This :: wxKeyEvent().
returns true if the Meta/Windows/Apple key is pressed. πͺπ
this function tests the state of the key traditionally called Meta under Unix systems, and Windows keys under MSW. notice that getModifiers/1 should usually be used instead of this one. π
see: cmdDown/1
shiftDown(This) -> boolean() when This :: wxKeyEvent().
returns true if the Shift key is pressed. β¬οΈ
this function doesn't distinguish between right and left shift keys. both sides are just⦠shift. iconic, honestly.
yep, you know the drill: getModifiers/1 should usually be used instead of this one.
and that's a wrap on the whole page bestie π this little love letter was brought to you by Ericsson AB, shipping wx 2.6, and it's literally titled wxKeyEvent(3) β date stamp and all, because even legends come with a footer. go forth and handle your key events correctly, you ate πβ¨
π¦ where this page comes from
- ubuntu stonking: erlang-doc 1:29.0.6+dfsg-1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms