Class: Wx::KeyEvent

Inherits:
Event show all
Defined in:
lib/wx/doc/gen/events.rb

Overview

This event class contains information about key press and release events.

The main information carried by this event is the key being pressed or released. It can be accessed using one of #get_unicode_key, #get_key_code or #get_raw_key_code functions. For the printable characters, #get_unicode_key should be used as it works for any keys, including non-Latin-1 characters that can be entered when using national keyboard layouts. #get_key_code should be used to handle special characters (such as cursor arrows keys or HOME or INS and so on) which correspond to KeyCode enum elements above the Wx::KeyCode::K_START constant. While #get_key_code also returns the character code for Latin-1 keys for compatibility, it doesn’t work for Unicode characters in general and will return Wx::KeyCode::K_NONE for any non-Latin-1 ones. If both #get_unicode_key and #get_key_code return Wx::KeyCode::K_NONE then the key has no WXK_xxx mapping and #get_raw_key_code can be used to distinguish between keys, but raw key codes are platform specific. For these reasons, it is recommended to always use #get_unicode_key and only fall back to #get_key_code if #get_unicode_key returned Wx::KeyCode::K_NONE, meaning that the event corresponds to a non-printable special keys, then optionally check #get_raw_key_code if #get_key_code also returned Wx::KeyCode::K_NONE or simply ignore that key. While these three functions can be used with the events of EVT_KEY_DOWN, EVT_KEY_UP and EVT_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, KeyEvent of type EVT_CHAR generated for this key press will return (from either #get_key_code or #get_unicode_key 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. 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. 7 or +), the untranslated key code corresponds to the character produced by the key when it is pressed without Shift. E.g. in standard US keyboard layout the untranslated key code for the key =/+ in the upper right corner of the keyboard is 61 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 #get_unicode_key can be used to retrieve the key code as #get_key_code just returns Wx::KeyCode::K_NONE in this case. Also, note that EVT_CHAR events are not generated for keys which do not have a wxWidgets mapping, so #get_raw_key_code should never be required for this event. To summarize: you should handle EVT_CHAR if you need the translated key and EVT_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, EVT_KEY_DOWN with = key code can be generated using the standard US keyboard layout but not using the German one because the = key corresponds to Shift-0 key combination in this layout and the key code for it is 0, not =. Because of this you should avoid requiring your users to type key events that might be impossible to enter on their keyboard.

Another difference between key and char events is that another kind of translation is done for the latter ones when the Control key is pressed: char events for ASCII letters in this case carry codes corresponding to the ASCII value of Ctrl-Latter, 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). 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 Wx::KeyCode::K_CONTROL_A - Wx::KeyCode::K_CONTROL_Z can be used instead of the non-descriptive constant values 1-26. Finally, modifier keys only generate key events but no char events at all. The modifiers keys are Wx::KeyCode::K_SHIFT, Wx::KeyCode::K_CONTROL, Wx::KeyCode::K_ALT and various Wx::KeyCode::K_WINDOWS_XXX from KeyCode enum. Modifier keys events are special in one additional aspect: usually the keyboard state associated with a key press is well defined, e.g. Wx::KeyboardState#shift_down 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 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, EVT_KEY_DOWN event for the Shift key itself will have Wx::KeyModifier::MOD_SHIFT in #get_modifiers and #shift_down will return true while the EVT_KEY_UP event for Shift itself will not have Wx::KeyModifier::MOD_SHIFT in its modifiers and #shift_down will return false. Tip: You may discover the key codes and modifiers generated by all the keys on your system interactively by running the Key Event Sample wxWidgets sample and pressing some keys in it.

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 avoid processing the same key twice. As a consequence, if you do not want to suppress the EVT_CHAR events for the keys you handle, always call event.Skip() in your EVT_KEY_DOWN handler. Not doing 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.

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) and almost all keys, including ones without ASCII equivalents, generate char events too.

Events using this class

The following event-handler methods redirect the events to member method or handler blocks for KeyEvent events. Event handler methods:

  • EvtHandler#evt_key_down(meth = nil, &block): Process a EVT_KEY_DOWN event (any key has been pressed). If this event is handled and not skipped, EVT_CHAR will not be generated at all for this key press (but EVT_KEY_UP will be).

  • EvtHandler#evt_key_up(meth = nil, &block): Process a EVT_KEY_UP event (any key has been released).

  • EvtHandler#evt_char(meth = nil, &block): Process a EVT_CHAR event.

  • EvtHandler#evt_char_hook(meth = nil, &block): Process a EVT_CHAR_HOOK event. Unlike all the other key events, this event is propagated upwards the window hierarchy which allows intercepting it in the parent window of the focused window to which it is sent initially (if there is no focused window, this event is sent to the App global object). It is also generated before any other key events and so gives the parent window an opportunity to modify the keyboard handling of its children, e.g. it is used internally by wxWidgets in some ports to intercept pressing Esc key in any child of a dialog to close the dialog itself when it's pressed. By default, if this event is handled, i.e. the handler doesn't call Event#skip, neither EVT_KEY_DOWN nor EVT_CHAR events will be generated (although EVT_KEY_UP still will be), i.e. it replaces the normal key events. However by calling the special #do_allow_next_event method you can handle EVT_CHAR_HOOK and still allow normal events generation. This is something that is rarely useful but can be required if you need to prevent a parent EVT_CHAR_HOOK handler from running without suppressing the normal key events. Finally notice that this event is not generated when the mouse is captured as it is considered that the window which has the capture should receive all the keyboard events too without allowing its parent TopLevelWindow to interfere with their processing.

Category: Events

See Also:

Instance Method Summary collapse

Methods inherited from Event

#clone, #get_event_category, #get_event_object, #get_event_type, #get_id, #get_skipped, #get_timestamp, #is_command_event, #resume_propagation, #set_event_object, #set_event_type, #set_id, #set_timestamp, #should_propagate, #skip, #stop_propagation

Methods inherited from Object

#clone, #dup, #is_same_as, #un_share

Constructor Details

#initialize(keyEventType = Wx::EVT_NULL) ⇒ Wx::KeyEvent

Constructor.

Currently, the only valid event types are EVT_CHAR and EVT_CHAR_HOOK.

Parameters:

  • keyEventType (Wx::GenericCollapsiblePane::EventType) (defaults to: Wx::EVT_NULL)


1127
# File 'lib/wx/doc/gen/events.rb', line 1127

def initialize(keyEventType=Wx::EVT_NULL) end

Instance Method Details

#alt_downBoolean

Returns true if the Alt key is pressed.

Notice that #get_modifiers should usually be used instead of this one.

Returns:

  • (Boolean)


1314
# File 'lib/wx/doc/gen/events.rb', line 1314

def alt_down; end

#cmd_downBoolean

Returns true if the key used for command accelerators is pressed.

Same as #control_down. Deprecated. Notice that #get_modifiers should usually be used instead of this one.

Returns:

  • (Boolean)


1321
# File 'lib/wx/doc/gen/events.rb', line 1321

def cmd_down; end

#control_downBoolean

Returns true if the Control key or Apple/Command key under macOS is pressed.

This function doesn’t distinguish between right and left control keys. Notice that #get_modifiers should usually be used instead of this one.

Returns:

  • (Boolean)


1287
# File 'lib/wx/doc/gen/events.rb', line 1287

def control_down; end

#do_allow_next_eventvoid

This method returns an undefined value.

Allow normal key events generation.

Can be called from EVT_CHAR_HOOK handler to indicate that the generation of normal events should not be suppressed, as it happens by default when this event is handled. The intended use of this method is to allow some window object to prevent EVT_CHAR_HOOK handler in its parent window from running by defining its own handler for this event. Without calling this method, this would result in not generating EVT_KEY_DOWN nor EVT_CHAR events at all but by calling it you can ensure that these events would still be generated, even if EVT_CHAR_HOOK event was handled.



1235
# File 'lib/wx/doc/gen/events.rb', line 1235

def do_allow_next_event; end

#get_key_codeInteger Also known as: key_code

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” (Wx::KeyCode::K_LEFT) return values outside of the ASCII range. See Wx::KeyCode 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 method returns Wx::KeyCode::K_NONE and #get_unicode_key should be used to obtain the corresponding Unicode character. Using #get_unicode_key is in general the right thing to do if you are interested in the characters typed by the user, #get_key_code should be only used for special keys (for which #get_unicode_key returns Wx::KeyCode::K_NONE). To handle both kinds of keys you might write:

def on_char(event)
     uc = event.get_unicode_key
     if uc
       # It's a "normal" character. Notice that this includes
       # control characters in 1..31 range, e.g. Wx::K_RETURN or
       # Wx::K_BACK, so check for them explicitly.
       if uc.ord >= 32
         Wx.log_message("You pressed '#{uc}'")
       else
         # It's a control character
         ...
       end
     else # No Unicode equivalent.
       # It's a special key, deal with all the known ones:
       case event.get_key_code
       when Wx::K_LEFT, Wx::K_RIGHT
           ... move cursor ...

       when Wx::K_F1:
           ... give help ...
       end
     end
  end

Returns:

  • (Integer)


1160
# File 'lib/wx/doc/gen/events.rb', line 1160

def get_key_code; end

#get_modifiersInteger Also known as: modifiers

Return the bit mask of all pressed modifier keys.

The return value is a combination of Wx::KeyModifier::MOD_ALT, Wx::KeyModifier::MOD_CONTROL, Wx::KeyModifier::MOD_SHIFT and Wx::KeyModifier::MOD_META bit masks. Additionally, Wx::KeyModifier::MOD_NONE is defined as 0, i.e. corresponds to no modifiers (see #has_any_modifiers) and Wx::KeyModifier::MOD_CMD is either Wx::KeyModifier::MOD_CONTROL (MSW and Unix) or Wx::KeyModifier::MOD_META (Mac), see #cmd_down. See Wx::KeyModifier for the full list of modifiers. Notice that this function is easier to use correctly than, for example, #control_down because when using the latter you also have to remember to test that none of the other modifiers is pressed:

if event.control_down && !event.alt_down && !event.shift_down && !event.meta_down
    # ... 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.get_modifiers == Wx::KeyModifier::MOD_CONTROL
    # ... handle Ctrl-XXX ...

with this function.

Returns:

  • (Integer)


1263
# File 'lib/wx/doc/gen/events.rb', line 1263

def get_modifiers; end

#get_positionWx::Point Also known as: position

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. x and y may be NULL if the corresponding coordinate is not needed.

Returns:



1119
# File 'lib/wx/doc/gen/events.rb', line 1119

def get_position; end

#get_raw_key_codeInteger Also known as: raw_key_code

Returns the raw key code for this event.

The flags are platform-dependent and should only be used if the functionality provided by other Wx::KeyEvent methods is insufficient. Under MSW, the raw key code is the value of wParam parameter of the corresponding message. Under GTK, the raw key code is the keyval field of the corresponding GDK event. Under macOS, the raw key code is the keyCode field of the corresponding NSEvent.

Note:

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

Returns:

  • (Integer)


1187
# File 'lib/wx/doc/gen/events.rb', line 1187

def get_raw_key_code; end

#get_raw_key_flagsInteger Also known as: raw_key_flags

Returns the low level key flags for this event.

The flags are platform-dependent and should only be used if the functionality provided by other Wx::KeyEvent methods is insufficient. Under MSW, the raw flags are just the value of lParam parameter of the corresponding message. Under GTK, the raw flags contain the hardware_keycode field 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 HAS_RAW_KEY_CODES to determine if this feature is available.

Returns:

  • (Integer)


1203
# File 'lib/wx/doc/gen/events.rb', line 1203

def get_raw_key_flags; end

#get_unicode_keyString Also known as: unicode_key

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 Wx::KeyCode::K_NONE. In this case you should use #get_key_code to retrieve the value of the key. This function is only available in Unicode build, i.e. when Setup::USE_UNICODE is 1.

Returns:

  • (String)


1211
# File 'lib/wx/doc/gen/events.rb', line 1211

def get_unicode_key; end

#get_xInteger Also known as: x

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

Returns:

  • (Integer)

See Also:



1219
# File 'lib/wx/doc/gen/events.rb', line 1219

def get_x; end

#get_yInteger Also known as: y

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

Returns:

  • (Integer)

See Also:



1227
# File 'lib/wx/doc/gen/events.rb', line 1227

def get_y; end

#has_any_modifiersBoolean Also known as: has_any_modifiers?

Returns true if any modifiers at all are pressed.

This is equivalent to get_modifiers != Wx::KeyModifier::MOD_NONE. Notice that this is different from #has_modifiers method which doesn’t take e.g. Shift modifier into account. This method is most suitable for mouse events when any modifier, including Shift, can change the interpretation of the event.

Returns:

  • (Boolean)


1271
# File 'lib/wx/doc/gen/events.rb', line 1271

def has_any_modifiers; end

#has_modifiersBoolean Also known as: has_modifiers?

Returns true if Control or Alt are pressed.

Checks if Control, Alt or, under macOS only, 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 #has_any_modifiers if you want to take Shift into account as well.

Returns:

  • (Boolean)


1279
# File 'lib/wx/doc/gen/events.rb', line 1279

def has_modifiers; end

#is_auto_repeatBoolean Also known as: auto_repeat?

Returns true if this event is an auto-repeat of the key, false if this is the initial key press.

Returns:

  • (Boolean)


1171
# File 'lib/wx/doc/gen/events.rb', line 1171

def is_auto_repeat; end

#is_key_in_category(category) ⇒ Boolean Also known as: key_in_category?

Returns true if the key is in the given key category.

Parameters:

Returns:

  • (Boolean)


1166
# File 'lib/wx/doc/gen/events.rb', line 1166

def is_key_in_category(category) end

#is_next_event_allowedBoolean Also known as: next_event_allowed?

Returns true if #do_allow_next_event had been called, false by default.

This method is used by wxWidgets itself to determine whether the normal key events should be generated after EVT_CHAR_HOOK processing.

Returns:

  • (Boolean)


1241
# File 'lib/wx/doc/gen/events.rb', line 1241

def is_next_event_allowed; end

#meta_downBoolean

Returns true if the Meta/Windows/Apple key is pressed.

This function tests the state of the key traditionally called Meta under Unix systems, Windows keys under MSW Notice that #get_modifiers should usually be used instead of this one.

Returns:

  • (Boolean)

See Also:



1308
# File 'lib/wx/doc/gen/events.rb', line 1308

def meta_down; end

#raw_control_downBoolean

Returns true if the Control key (also under macOS).

This function doesn’t distinguish between right and left control keys. Notice that #get_modifiers should usually be used instead of this one.

Returns:

  • (Boolean)


1294
# File 'lib/wx/doc/gen/events.rb', line 1294

def raw_control_down; end

#set_alt_down(down) ⇒ void Also known as: alt_down=

This method returns an undefined value.

Parameters:

  • down (Boolean)


1340
# File 'lib/wx/doc/gen/events.rb', line 1340

def set_alt_down(down) end

#set_control_down(down) ⇒ void Also known as: control_down=

This method returns an undefined value.

Parameters:

  • down (Boolean)


1325
# File 'lib/wx/doc/gen/events.rb', line 1325

def set_control_down(down) end

#set_meta_down(down) ⇒ void Also known as: meta_down=

This method returns an undefined value.

Parameters:

  • down (Boolean)


1345
# File 'lib/wx/doc/gen/events.rb', line 1345

def set_meta_down(down) end

#set_raw_control_down(down) ⇒ void Also known as: raw_control_down=

This method returns an undefined value.

Parameters:

  • down (Boolean)


1330
# File 'lib/wx/doc/gen/events.rb', line 1330

def set_raw_control_down(down) end

#set_shift_down(down) ⇒ void Also known as: shift_down=

This method returns an undefined value.

Parameters:

  • down (Boolean)


1335
# File 'lib/wx/doc/gen/events.rb', line 1335

def set_shift_down(down) end

#shift_downBoolean

Returns true if the Shift key is pressed.

This function doesn’t distinguish between right and left shift keys. Notice that #get_modifiers should usually be used instead of this one.

Returns:

  • (Boolean)


1301
# File 'lib/wx/doc/gen/events.rb', line 1301

def shift_down; end