Skip to content

module SF::Keyboard #

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 module 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.key_pressed?(SF::Keyboard::Left)
  # move left...
elsif SF::Keyboard.key_pressed?(SF::Keyboard::Right)
  # move right...
elsif SF::Keyboard.key_pressed?(SF::Keyboard::Escape)
  # quit...
elsif SF::Keyboard.key_pressed?(SF::Keyboard::Scan::Grave)
  # open in-game command line (if it's not already open)

See also: SF::Joystick, SF::Mouse, SF::Touch

Class methods#

.delocalize(key : Keyboard::Key) : Keyboard::Scan::Scancode#

Identify the physical key corresponding to a logical one

  • 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

View source

.get_description(code : Keyboard::Scan::Scancode) : String#

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.


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

View source

.key_pressed?(key : Keyboard::Key) : Bool#

Check if a key is pressed

  • key - Key to check

Returns: True if the key is pressed, false otherwise

View source

.key_pressed?(code : Keyboard::Scan::Scancode) : Bool#

Check if a key is pressed

  • code - Scancode to check

Returns: True if the physical key is pressed, false otherwise

View source

.localize(code : Keyboard::Scan::Scancode) : Keyboard::Key#

Localize a physical key to a logical one

  • 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

View source

.virtual_keyboard_visible=(visible : Bool)#

Show or hide the virtual keyboard


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.

  • visible - True to show, false to hide
View source