module SFML::Keyboard
Keyboard key code <-> symbol translation, plus Keyboard.key_pressed?(:esc).
Two concepts: * Key β logical / layout-dependent. :a on a QWERTY keyboard is the physical Q on AZERTY. Use this for βwhat does the user meanβ (text input, layout-aware shortcuts). * Scancode β physical / layout-independent. The key at the position you expect WASD to be will always be :scan_w / :scan_a / :scan_s / :scan_d. Use this for βthe WASD keysβ that should stay in the same physical place regardless of keyboard layout. Standard in modern games.
The KEY_CODES / SCAN_CODES arrays are load-bearing: order matches the sfKeyCode / sfScancode enums in CSFML/Window/Keyboard.h.
Constants
- ALIASES
-
Friendly aliases users might reach for naturally.
- KEY_CODES
-
All key symbols in CSFML
sfKeyCodeenum order. - SCAN_CODES
-
Matches sfScancode in CSFML/Window/Keyboard.h exactly. sfScanUnknown is -1, represented here by :scan_unknown via
scancode_to_symbol. - SCAN_SYMBOL_TO_CODE
-
Scancode symbol β integer code Hash.
- SYMBOL_TO_CODE
-
Key symbol β integer code Hash.
Public Instance Methods
Source
# File lib/sfml/window/keyboard.rb, line 90 def code_to_symbol(code) return :unknown if code < 0 || code >= KEY_CODES.length KEY_CODES[code] end
Returns the code to symbol.
Source
# File lib/sfml/window/keyboard.rb, line 139 def delocalize(key) scancode_to_symbol(C::Window.sfKeyboard_delocalize(symbol_to_code(key))) end
Inverse of localize β find the physical scancode that would produce this logical key under the current layout. Returns a scancode symbol.
Source
# File lib/sfml/window/keyboard.rb, line 145 def description(scancode) C::Window.sfKeyboard_getDescription(symbol_to_scancode(scancode)) end
Human-readable description for a scancode under the current layout β e.g. :scan_w β "W" on QWERTY, "Z" on AZERTY.
Source
# File lib/sfml/window/keyboard.rb, line 119 def key_pressed?(symbol) C::Window.sfKeyboard_isKeyPressed(symbol_to_code(symbol)) end
SFML::Keyboard.key_pressed?(:escape) β logical key.
Source
# File lib/sfml/window/keyboard.rb, line 132 def localize(scancode) code_to_symbol(C::Window.sfKeyboard_localize(symbol_to_scancode(scancode))) end
Map a physical scancode to whatever logical key it produces under the current OS keyboard layout. Returns a key symbol (or :unknown for unmappable scancodes).
Source
# File lib/sfml/window/keyboard.rb, line 125 def scancode_pressed?(symbol) C::Window.sfKeyboard_isScancodePressed(symbol_to_scancode(symbol)) end
SFML::Keyboard.scancode_pressed?(:scan_w) β physical key regardless of keyboard layout.
Source
# File lib/sfml/window/keyboard.rb, line 105 def scancode_to_symbol(code) return :scan_unknown if code < 0 || code >= SCAN_CODES.length SCAN_CODES[code] end
Returns the scancode to symbol.
Source
# File lib/sfml/window/keyboard.rb, line 96 def symbol_to_code(symbol) symbol = ALIASES.fetch(symbol, symbol) SYMBOL_TO_CODE.fetch(symbol) do raise ArgumentError, "Unknown key symbol: #{symbol.inspect}. " \ "See SFML::Keyboard::KEY_CODES." end end
Returns the symbol to code.
Source
# File lib/sfml/window/keyboard.rb, line 111 def symbol_to_scancode(symbol) SCAN_SYMBOL_TO_CODE.fetch(symbol) do raise ArgumentError, "Unknown scancode symbol: #{symbol.inspect}. " \ "See SFML::Keyboard::SCAN_CODES." end end
Returns the symbol to scancode.
Source
# File lib/sfml/window/keyboard.rb, line 151 def virtual_keyboard_visible=(value) C::Window.sfKeyboard_setVirtualKeyboardVisible(!!value) end
On-screen / virtual keyboard toggle. No-op on desktop platforms; meaningful on mobile/touchscreen builds.