SFML - Simple and Fast Multimedia Library

SFML 2.6.1

- Main Page
- Related Pages
- Topics
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Loading...
Searching...
No Matches
Classes | Public Types | Static Public Member Functions | List of all members
sf::Keyboard Class Reference
Window module

Give access to the real-time state of the keyboard. More...

`#include <SFML/Window/Keyboard.hpp>`

## Classes
struct | Scan
| Scancodes. More...

## Public Types
enum | Key { Unknown = -1 , A = 0 , B , C , D , E , F , G , H , I , J , K , L , M , N , O , P , Q , R , S , T , U , V , W , X , Y , Z , Num0 , Num1 , Num2 , Num3 , Num4 , Num5 , Num6 , Num7 , Num8 , Num9 , Escape , LControl , LShift , LAlt , LSystem , RControl , RShift , RAlt , RSystem , Menu , LBracket , RBracket , Semicolon , Comma , Period , Apostrophe , Slash , Backslash , Grave , Equal , Hyphen , Space , Enter , Backspace , Tab , PageUp , PageDown , End , Home , Insert , Delete , Add , Subtract , Multiply , Divide , Left , Right , Up , Down , Numpad0 , Numpad1 , Numpad2 , Numpad3 , Numpad4 , Numpad5 , Numpad6 , Numpad7 , Numpad8 , Numpad9 , F1 , F2 , F3 , F4 , F5 , F6 , F7 , F8 , F9 , F10 , F11 , F12 , F13 , F14 , F15 , Pause , KeyCount , Tilde = Grave , Dash = Hyphen , BackSpace = Backspace , BackSlash = Backslash , SemiColon = Semicolon , Return = Enter , Quote = Apostrophe }
| Key codes. More...
typedef Scan::Scancode | Scancode

## Static Public Member Functions
static bool | isKeyPressed (Key key)
| Check if a key is pressed.
static bool | isKeyPressed (Scancode code)
| Check if a key is pressed.
static Key | localize (Scancode code)
| Localize a physical key to a logical one.
static Scancode | delocalize (Key key)
| Identify the physical key corresponding to a logical one.
static String | getDescription (Scancode code)
| Provide a string representation for a given scancode.
static void | setVirtualKeyboardVisible (bool visible)
| Show or hide the virtual keyboard.

## Detailed Description

Give access to the real-time state of the keyboard.

sf::Keyboard provides an interface to the state of the keyboard.

It only contains static functions (a single keyboard is assumed), so it's not meant to be instantiated.

This class allows users to query the keyboard state at any time and directly, without having to deal with a window and its events. Compared to the KeyPressed and KeyReleased events, sf::Keyboard can retrieve the state of a key at any time (you don't need to store and update a boolean on your side in order to know if a key is pressed or released), and you always get the real state of the keyboard, even if keys are pressed or released when your window is out of focus and no event is triggered.

Usage example:

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
// move left...
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
// move right...
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
// quit...
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Grave))
{
// open in-game command line (if it's not already open)
}
sf::Keyboard::isKeyPressed
static bool isKeyPressed(Key key)
Check if a key is pressed.
sf::Keyboard::Right
@ Right
Right arrow.
Definition Keyboard.hpp:129
sf::Keyboard::Escape
@ Escape
The Escape key.
Definition Keyboard.hpp:93
sf::Keyboard::Left
@ Left
Left arrow.
Definition Keyboard.hpp:128
sf::Keyboard::Scan::Grave
@ Grave
Keyboard ` and ~ key.
Definition Keyboard.hpp:246

See also
  sf::Joystick, sf::Mouse, sf::Touch

Definition at line 42 of file Keyboard.hpp.

## Member Typedef Documentation

## ◆ Scancode

typedef Scan::Scancode sf::Keyboard::Scancode

Definition at line 356 of file Keyboard.hpp.

## Member Enumeration Documentation

## ◆ Key

enum sf::Keyboard::Key

Key codes.

The enumerators refer to the "localized" key; i.e. depending on the layout set by the operating system, a key can be mapped to `Y` or `Z`.

Enumerator
Unknown | Unhandled key.
A | The A key.
B | The B key.
C | The C key.
D | The D key.
E | The E key.
F | The F key.
G | The G key.
H | The H key.
I | The I key.
J | The J key.
K | The K key.
L | The L key.
M | The M key.
N | The N key.
O | The O key.
P | The P key.
Q | The Q key.
R | The R key.
S | The S key.
T | The T key.
U | The U key.
V | The V key.
W | The W key.
X | The X key.
Y | The Y key.
Z | The Z key.
Num0 | The 0 key.
Num1 | The 1 key.
Num2 | The 2 key.
Num3 | The 3 key.
Num4 | The 4 key.
Num5 | The 5 key.
Num6 | The 6 key.
Num7 | The 7 key.
Num8 | The 8 key.
Num9 | The 9 key.
Escape | The Escape key.
LControl | The left Control key.
LShift | The left Shift key.
LAlt | The left Alt key.
LSystem | The left OS specific key: window (Windows and Linux), apple (macOS), ...
RControl | The right Control key.
RShift | The right Shift key.
RAlt | The right Alt key.
RSystem | The right OS specific key: window (Windows and Linux), apple (macOS), ...
Menu | The Menu key.
LBracket | The [ key.
RBracket | The ] key.
Semicolon | The ; key.
Comma | The , key.
Period | The . key.
Apostrophe | The ' key.
Slash | The / key.
Backslash | The \ key.
Grave | The ` key.
Equal | The = key.
Hyphen | The - key (hyphen)
Space | The Space key.
Enter | The Enter/Return keys.
Backspace | The Backspace key.
Tab | The Tabulation key.
PageUp | The Page up key.
PageDown | The Page down key.
End | The End key.
Home | The Home key.
Insert | The Insert key.
Delete | The Delete key.
Add | The + key.
Subtract | The - key (minus, usually from numpad)
Multiply | The * key.
Divide | The / key.
Left | Left arrow.
Right | Right arrow.
Up | Up arrow.
Down | Down arrow.
Numpad0 | The numpad 0 key.
Numpad1 | The numpad 1 key.
Numpad2 | The numpad 2 key.
Numpad3 | The numpad 3 key.
Numpad4 | The numpad 4 key.
Numpad5 | The numpad 5 key.
Numpad6 | The numpad 6 key.
Numpad7 | The numpad 7 key.
Numpad8 | The numpad 8 key.
Numpad9 | The numpad 9 key.
F1 | The F1 key.
F2 | The F2 key.
F3 | The F3 key.
F4 | The F4 key.
F5 | The F5 key.
F6 | The F6 key.
F7 | The F7 key.
F8 | The F8 key.
F9 | The F9 key.
F10 | The F10 key.
F11 | The F11 key.
F12 | The F12 key.
F13 | The F13 key.
F14 | The F14 key.
F15 | The F15 key.
Pause | The Pause key.
KeyCount | Keep last – the total number of keyboard keys.
Tilde | Deprecated: Use Grave instead
Dash | Deprecated: Use Hyphen instead
BackSpace | Deprecated: Use Backspace instead
BackSlash | Deprecated: Use Backslash instead
SemiColon | Deprecated: Use Semicolon instead
Return | Deprecated: Use Enter instead
Quote | Deprecated: Use Apostrophe instead

Definition at line 54 of file Keyboard.hpp.

## Member Function Documentation

## ◆ delocalize()

static Scancode sf::Keyboard::delocalize | ( | Key | key | ) |
| static

Identify the physical key corresponding to a logical one.

Parameters

  key | Key to "delocalize"

Returns
  The scancode corresponding to the key under the current keyboard layout used by the operating system, or sf::Keyboard::Scan::Unknown when the key cannot be mapped to a sf::Keyboard::Scancode.

See also
  localize

## ◆ getDescription()

static String sf::Keyboard::getDescription | ( | Scancode | code | ) |
| static

Provide a string representation for a given scancode.

The returned string is a short, non-technical description of the key represented with the given scancode. Most effectively used in user interfaces, as the description for the key takes the users keyboard layout into consideration.

Warning
  The result is OS-dependent: for example, sf::Keyboard::Scan::LSystem is "Left Meta" on Linux, "Left Windows" on Windows and "Left Command" on macOS.

The current keyboard layout set by the operating system is used to interpret the scancode: for example, sf::Keyboard::Semicolon is mapped to ";" for layout and to "é" for others.

Returns
  The localized description of the code

## ◆ isKeyPressed() [1/2]

static bool sf::Keyboard::isKeyPressed | ( | Key | key | ) |
| static

Check if a key is pressed.

Parameters

  key | Key to check

Returns
  True if the key is pressed, false otherwise

## ◆ isKeyPressed() [2/2]

static bool sf::Keyboard::isKeyPressed | ( | Scancode | code | ) |
| static

Check if a key is pressed.

Parameters

  code | Scancode to check

Returns
  True if the physical key is pressed, false otherwise

## ◆ localize()

static Key sf::Keyboard::localize | ( | Scancode | code | ) |
| static

Localize a physical key to a logical one.

Parameters

  code | Scancode to localize

Returns
  The key corresponding to the scancode under the current keyboard layout used by the operating system, or sf::Keyboard::Unknown when the scancode cannot be mapped to a Key.

See also
  delocalize

## ◆ setVirtualKeyboardVisible()

static void sf::Keyboard::setVirtualKeyboardVisible | ( | bool | visible | ) |
| static

Show or hide the virtual keyboard.

Warning
  The virtual keyboard is not supported on all systems. It will typically be implemented on mobile OSes (Android, iOS) but not on desktop OSes (Windows, Linux, ...).

If the virtual keyboard is not available, this function does nothing.

Parameters

  visible | True to show, false to hide

The documentation for this class was generated from the following file:

- Keyboard.hpp