| Version: 3.3.1

Public Member Functions | List of all members
wxKeyboardState Class Reference
Class List by Category » Events

`#include <wx/kbdstate.h>`

Inheritance diagram for wxKeyboardState:
[legend]

## Detailed Description

Provides methods for testing the state of the keyboard modifier keys.

This class is used as a base class of wxKeyEvent and wxMouseState and, hence, indirectly, of wxMouseEvent, so its methods may be used to get information about the modifier keys which were pressed when the event occurred.

This class is implemented entirely inline in <wx/kbdstate.h> and thus has no linking requirements.

##

Library: None; this class implementation is entirely header-based.
Category: Events

See also
  wxKeyEvent, wxMouseState

## Public Member Functions
| wxKeyboardState (bool controlDown=false, bool shiftDown=false, bool altDown=false, bool metaDown=false)
| Constructor initializes the modifier key settings. More...
int | GetModifiers () const
| Return the bit mask of all pressed modifier keys. More...
bool | HasAnyModifiers () const
| Returns true if any modifiers at all are pressed. More...
bool | HasModifiers () const
| Returns true if Control or Alt are pressed. More...
bool | ControlDown () const
| Returns true if the Control key or Apple/Command key under macOS is pressed. More...
bool | RawControlDown () const
| Returns true if the Control key (also under macOS). More...
bool | ShiftDown () const
| Returns true if the Shift key is pressed. More...
bool | MetaDown () const
| Returns true if the Meta/Windows/Apple key is pressed. More...
bool | AltDown () const
| Returns true if the Alt key is pressed. More...
bool | CmdDown () const
| Returns true if the key used for command accelerators is pressed. More...
void | SetControlDown (bool down)
void | SetRawControlDown (bool down)
void | SetShiftDown (bool down)
void | SetAltDown (bool down)
void | SetMetaDown (bool down)

## Constructor & Destructor Documentation

## ◆ wxKeyboardState()

wxKeyboardState::wxKeyboardState | ( | bool | controlDown = `false`,
|  | bool | shiftDown = `false`,
|  | bool | altDown = `false`,
|  | bool | metaDown = `false`
| ) |  |
| explicit

Constructor initializes the modifier key settings.

By default, no modifiers are active.

## Member Function Documentation

## ◆ AltDown()

bool wxKeyboardState::AltDown | ( |  | ) | const

Returns true if the Alt key is pressed.

Notice that GetModifiers() should usually be used instead of this one.

## ◆ CmdDown()

bool wxKeyboardState::CmdDown | ( |  | ) | const

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

Same as ControlDown(). Deprecated.

Notice that GetModifiers() should usually be used instead of this one.

## ◆ ControlDown()

bool wxKeyboardState::ControlDown | ( |  | ) | const

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 GetModifiers() should usually be used instead of this one.

## ◆ GetModifiers()

int wxKeyboardState::GetModifiers | ( |  | ) | const

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()) and `wxMOD_CMD` is either `wxMOD_CONTROL` (MSW and Unix) or `wxMOD_META` (Mac), see CmdDown(). See wxKeyModifier for the full list of modifiers.

Notice that this function is easier to use correctly than, for example, ControlDown() because when using the latter you also have to remember to test that none of the other modifiers is pressed:

if ( ControlDown() && !AltDown() && !ShiftDown() && !MetaDown() )
... handle Ctrl-XXX ...
wxKeyboardState::ShiftDown
bool ShiftDown() const
Returns true if the Shift key is pressed.
wxKeyboardState::AltDown
bool AltDown() const
Returns true if the Alt key is pressed.
wxKeyboardState::MetaDown
bool MetaDown() const
Returns true if the Meta/Windows/Apple key is pressed.
wxKeyboardState::ControlDown
bool ControlDown() const
Returns true if the Control key or Apple/Command key under macOS is pressed.

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 ( GetModifiers() == wxMOD_CONTROL )
... handle Ctrl-XXX ...
wxKeyboardState::GetModifiers
int GetModifiers() const
Return the bit mask of all pressed modifier keys.
wxMOD_CONTROL
@ wxMOD_CONTROL
Ctlr Key, corresponds to Command key on macOS.
Definition: defs.h:1136

with this function.

## ◆ HasAnyModifiers()

bool wxKeyboardState::HasAnyModifiers | ( |  | ) | const

Returns true if any modifiers at all are pressed.

This is equivalent to `GetModifiers()` `!=` `wxMOD_NONE`.

Notice that this is different from HasModifiers() 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.

Since
  2.9.5

## ◆ HasModifiers()

bool wxKeyboardState::HasModifiers | ( |  | ) | const

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 HasAnyModifiers() if you want to take Shift into account as well.

## ◆ MetaDown()

bool wxKeyboardState::MetaDown | ( |  | ) | const

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 GetModifiers() should usually be used instead of this one.

See also
  CmdDown()

## ◆ RawControlDown()

bool wxKeyboardState::RawControlDown | ( |  | ) | const

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

This function doesn't distinguish between right and left control keys.

Notice that GetModifiers() should usually be used instead of this one.

## ◆ SetAltDown()

void wxKeyboardState::SetAltDown | ( | bool | down | ) |

## ◆ SetControlDown()

void wxKeyboardState::SetControlDown | ( | bool | down | ) |

## ◆ SetMetaDown()

void wxKeyboardState::SetMetaDown | ( | bool | down | ) |

## ◆ SetRawControlDown()

void wxKeyboardState::SetRawControlDown | ( | bool | down | ) |

## ◆ SetShiftDown()

void wxKeyboardState::SetShiftDown | ( | bool | down | ) |

## ◆ ShiftDown()

bool wxKeyboardState::ShiftDown | ( |  | ) | const

Returns true if the Shift key is pressed.

This function doesn't distinguish between right and left shift keys.

Notice that GetModifiers() should usually be used instead of this one.