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...
end

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

Class methods#

.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

.virtual_keyboard_visible=(visible : Bool) #

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.

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