Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:58:15

0001 ////////////////////////////////////////////////////////////
0002 //
0003 // SFML - Simple and Fast Multimedia Library
0004 // Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org)
0005 //
0006 // This software is provided 'as-is', without any express or implied warranty.
0007 // In no event will the authors be held liable for any damages arising from the use of this software.
0008 //
0009 // Permission is granted to anyone to use this software for any purpose,
0010 // including commercial applications, and to alter it and redistribute it freely,
0011 // subject to the following restrictions:
0012 //
0013 // 1. The origin of this software must not be misrepresented;
0014 //    you must not claim that you wrote the original software.
0015 //    If you use this software in a product, an acknowledgment
0016 //    in the product documentation would be appreciated but is not required.
0017 //
0018 // 2. Altered source versions must be plainly marked as such,
0019 //    and must not be misrepresented as being the original software.
0020 //
0021 // 3. This notice may not be removed or altered from any source distribution.
0022 //
0023 ////////////////////////////////////////////////////////////
0024 
0025 #ifndef SFML_KEYBOARD_HPP
0026 #define SFML_KEYBOARD_HPP
0027 
0028 ////////////////////////////////////////////////////////////
0029 // Headers
0030 ////////////////////////////////////////////////////////////
0031 #include <SFML/Window/Export.hpp>
0032 
0033 
0034 namespace sf
0035 {
0036 class String;
0037 
0038 ////////////////////////////////////////////////////////////
0039 /// \brief Give access to the real-time state of the keyboard
0040 ///
0041 ////////////////////////////////////////////////////////////
0042 class SFML_WINDOW_API Keyboard
0043 {
0044 public:
0045 
0046     ////////////////////////////////////////////////////////////
0047     /// \brief Key codes
0048     ///
0049     /// The enumerators refer to the "localized" key; i.e. depending
0050     /// on the layout set by the operating system, a key can be mapped
0051     /// to `Y` or `Z`.
0052     ///
0053     ////////////////////////////////////////////////////////////
0054     enum Key
0055     {
0056         Unknown = -1, //!< Unhandled key
0057         A = 0,        //!< The A key
0058         B,            //!< The B key
0059         C,            //!< The C key
0060         D,            //!< The D key
0061         E,            //!< The E key
0062         F,            //!< The F key
0063         G,            //!< The G key
0064         H,            //!< The H key
0065         I,            //!< The I key
0066         J,            //!< The J key
0067         K,            //!< The K key
0068         L,            //!< The L key
0069         M,            //!< The M key
0070         N,            //!< The N key
0071         O,            //!< The O key
0072         P,            //!< The P key
0073         Q,            //!< The Q key
0074         R,            //!< The R key
0075         S,            //!< The S key
0076         T,            //!< The T key
0077         U,            //!< The U key
0078         V,            //!< The V key
0079         W,            //!< The W key
0080         X,            //!< The X key
0081         Y,            //!< The Y key
0082         Z,            //!< The Z key
0083         Num0,         //!< The 0 key
0084         Num1,         //!< The 1 key
0085         Num2,         //!< The 2 key
0086         Num3,         //!< The 3 key
0087         Num4,         //!< The 4 key
0088         Num5,         //!< The 5 key
0089         Num6,         //!< The 6 key
0090         Num7,         //!< The 7 key
0091         Num8,         //!< The 8 key
0092         Num9,         //!< The 9 key
0093         Escape,       //!< The Escape key
0094         LControl,     //!< The left Control key
0095         LShift,       //!< The left Shift key
0096         LAlt,         //!< The left Alt key
0097         LSystem,      //!< The left OS specific key: window (Windows and Linux), apple (macOS), ...
0098         RControl,     //!< The right Control key
0099         RShift,       //!< The right Shift key
0100         RAlt,         //!< The right Alt key
0101         RSystem,      //!< The right OS specific key: window (Windows and Linux), apple (macOS), ...
0102         Menu,         //!< The Menu key
0103         LBracket,     //!< The [ key
0104         RBracket,     //!< The ] key
0105         Semicolon,    //!< The ; key
0106         Comma,        //!< The , key
0107         Period,       //!< The . key
0108         Apostrophe,   //!< The ' key
0109         Slash,        //!< The / key
0110         Backslash,    //!< The \ key
0111         Grave,        //!< The ` key
0112         Equal,        //!< The = key
0113         Hyphen,       //!< The - key (hyphen)
0114         Space,        //!< The Space key
0115         Enter,        //!< The Enter/Return keys
0116         Backspace,    //!< The Backspace key
0117         Tab,          //!< The Tabulation key
0118         PageUp,       //!< The Page up key
0119         PageDown,     //!< The Page down key
0120         End,          //!< The End key
0121         Home,         //!< The Home key
0122         Insert,       //!< The Insert key
0123         Delete,       //!< The Delete key
0124         Add,          //!< The + key
0125         Subtract,     //!< The - key (minus, usually from numpad)
0126         Multiply,     //!< The * key
0127         Divide,       //!< The / key
0128         Left,         //!< Left arrow
0129         Right,        //!< Right arrow
0130         Up,           //!< Up arrow
0131         Down,         //!< Down arrow
0132         Numpad0,      //!< The numpad 0 key
0133         Numpad1,      //!< The numpad 1 key
0134         Numpad2,      //!< The numpad 2 key
0135         Numpad3,      //!< The numpad 3 key
0136         Numpad4,      //!< The numpad 4 key
0137         Numpad5,      //!< The numpad 5 key
0138         Numpad6,      //!< The numpad 6 key
0139         Numpad7,      //!< The numpad 7 key
0140         Numpad8,      //!< The numpad 8 key
0141         Numpad9,      //!< The numpad 9 key
0142         F1,           //!< The F1 key
0143         F2,           //!< The F2 key
0144         F3,           //!< The F3 key
0145         F4,           //!< The F4 key
0146         F5,           //!< The F5 key
0147         F6,           //!< The F6 key
0148         F7,           //!< The F7 key
0149         F8,           //!< The F8 key
0150         F9,           //!< The F9 key
0151         F10,          //!< The F10 key
0152         F11,          //!< The F11 key
0153         F12,          //!< The F12 key
0154         F13,          //!< The F13 key
0155         F14,          //!< The F14 key
0156         F15,          //!< The F15 key
0157         Pause,        //!< The Pause key
0158 
0159         KeyCount,     //!< Keep last -- the total number of keyboard keys
0160 
0161         // Deprecated values:
0162 
0163         Tilde     = Grave,     //!< \deprecated Use Grave instead
0164         Dash      = Hyphen,    //!< \deprecated Use Hyphen instead
0165         BackSpace = Backspace, //!< \deprecated Use Backspace instead
0166         BackSlash = Backslash, //!< \deprecated Use Backslash instead
0167         SemiColon = Semicolon, //!< \deprecated Use Semicolon instead
0168         Return    = Enter,     //!< \deprecated Use Enter instead
0169         Quote     = Apostrophe //!< \deprecated Use Apostrophe instead
0170     };
0171 
0172     ////////////////////////////////////////////////////////////
0173     /// \brief Scancodes
0174     ///
0175     /// The enumerators are bound to a physical key and do not depend on
0176     /// the keyboard layout used by the operating system. Usually, the AT-101
0177     /// keyboard can be used as reference for the physical position of the keys.
0178     ///
0179     ////////////////////////////////////////////////////////////
0180     struct Scan
0181     {
0182         // TODO: replace with enum class in SFML 3.
0183         // Clang warns us rightfully that Scancode names shadow Key names.
0184         // A safer solution would be to use a C++11 scoped enumeration (enum class),
0185         // but it is not possible in SFML 2 which uses C++03.
0186         // For now, we just ignore those warnings.
0187         #if defined(__clang__)
0188             #pragma clang diagnostic push
0189             #pragma clang diagnostic ignored "-Wshadow"
0190         #endif
0191 
0192         enum Scancode
0193         {
0194             Unknown = -1,       //!< Represents any scancode not present in this enum
0195             A = 0,              //!< Keyboard a and A key
0196             B,                  //!< Keyboard b and B key
0197             C,                  //!< Keyboard c and C key
0198             D,                  //!< Keyboard d and D key
0199             E,                  //!< Keyboard e and E key
0200             F,                  //!< Keyboard f and F key
0201             G,                  //!< Keyboard g and G key
0202             H,                  //!< Keyboard h and H key
0203             I,                  //!< Keyboard i and I key
0204             J,                  //!< Keyboard j and J key
0205             K,                  //!< Keyboard k and K key
0206             L,                  //!< Keyboard l and L key
0207             M,                  //!< Keyboard m and M key
0208             N,                  //!< Keyboard n and N key
0209             O,                  //!< Keyboard o and O key
0210             P,                  //!< Keyboard p and P key
0211             Q,                  //!< Keyboard q and Q key
0212             R,                  //!< Keyboard r and R key
0213             S,                  //!< Keyboard s and S key
0214             T,                  //!< Keyboard t and T key
0215             U,                  //!< Keyboard u and U key
0216             V,                  //!< Keyboard v and V key
0217             W,                  //!< Keyboard w and W key
0218             X,                  //!< Keyboard x and X key
0219             Y,                  //!< Keyboard y and Y key
0220             Z,                  //!< Keyboard z and Z key
0221             Num1,               //!< Keyboard 1 and ! key
0222             Num2,               //!< Keyboard 2 and @ key
0223             Num3,               //!< Keyboard 3 and # key
0224             Num4,               //!< Keyboard 4 and $ key
0225             Num5,               //!< Keyboard 5 and % key
0226             Num6,               //!< Keyboard 6 and ^ key
0227             Num7,               //!< Keyboard 7 and & key
0228             Num8,               //!< Keyboard 8 and * key
0229             Num9,               //!< Keyboard 9 and ) key
0230             Num0,               //!< Keyboard 0 and ) key
0231             Enter,              //!< Keyboard Enter/Return key
0232             Escape,             //!< Keyboard Escape key
0233             Backspace,          //!< Keyboard Backspace key
0234             Tab,                //!< Keyboard Tab key
0235             Space,              //!< Keyboard Space key
0236             Hyphen,             //!< Keyboard - and _ key
0237             Equal,              //!< Keyboard = and +
0238             LBracket,           //!< Keyboard [ and { key
0239             RBracket,           //!< Keyboard ] and } key
0240             // For US keyboards mapped to key 29 (Microsoft Keyboard Scan Code Specification)
0241             // For Non-US keyboards mapped to key 42 (Microsoft Keyboard Scan Code Specification)
0242             // Typical language mappings: Belg:£µ` FrCa:<>} Dan:*' Dutch:`´ Fren:µ* Ger:'# Ital:§ù LatAm:[}` Nor:*@ Span:ç} Swed:*' Swiss:$£} UK:~# Brazil:}]
0243             Backslash,          //!< Keyboard \ and | key OR various keys for Non-US keyboards
0244             Semicolon,          //!< Keyboard ; and : key
0245             Apostrophe,         //!< Keyboard ' and " key
0246             Grave,              //!< Keyboard ` and ~ key
0247             Comma,              //!< Keyboard , and < key
0248             Period,             //!< Keyboard . and > key
0249             Slash,              //!< Keyboard / and ? key
0250             F1,                 //!< Keyboard F1 key
0251             F2,                 //!< Keyboard F2 key
0252             F3,                 //!< Keyboard F3 key
0253             F4,                 //!< Keyboard F4 key
0254             F5,                 //!< Keyboard F5 key
0255             F6,                 //!< Keyboard F6 key
0256             F7,                 //!< Keyboard F7 key
0257             F8,                 //!< Keyboard F8 key
0258             F9,                 //!< Keyboard F9 key
0259             F10,                //!< Keyboard F10 key
0260             F11,                //!< Keyboard F11 key
0261             F12,                //!< Keyboard F12 key
0262             F13,                //!< Keyboard F13 key
0263             F14,                //!< Keyboard F14 key
0264             F15,                //!< Keyboard F15 key
0265             F16,                //!< Keyboard F16 key
0266             F17,                //!< Keyboard F17 key
0267             F18,                //!< Keyboard F18 key
0268             F19,                //!< Keyboard F19 key
0269             F20,                //!< Keyboard F20 key
0270             F21,                //!< Keyboard F21 key
0271             F22,                //!< Keyboard F22 key
0272             F23,                //!< Keyboard F23 key
0273             F24,                //!< Keyboard F24 key
0274             CapsLock,           //!< Keyboard Caps %Lock key
0275             PrintScreen,        //!< Keyboard Print Screen key
0276             ScrollLock,         //!< Keyboard Scroll %Lock key
0277             Pause,              //!< Keyboard Pause key
0278             Insert,             //!< Keyboard Insert key
0279             Home,               //!< Keyboard Home key
0280             PageUp,             //!< Keyboard Page Up key
0281             Delete,             //!< Keyboard Delete Forward key
0282             End,                //!< Keyboard End key
0283             PageDown,           //!< Keyboard Page Down key
0284             Right,              //!< Keyboard Right Arrow key
0285             Left,               //!< Keyboard Left Arrow key
0286             Down,               //!< Keyboard Down Arrow key
0287             Up,                 //!< Keyboard Up Arrow key
0288             NumLock,            //!< Keypad Num %Lock and Clear key
0289             NumpadDivide,       //!< Keypad / key
0290             NumpadMultiply,     //!< Keypad * key
0291             NumpadMinus,        //!< Keypad - key
0292             NumpadPlus,         //!< Keypad + key
0293             NumpadEqual,        //!< keypad = key
0294             NumpadEnter,        //!< Keypad Enter/Return key
0295             NumpadDecimal,      //!< Keypad . and Delete key
0296             Numpad1,            //!< Keypad 1 and End key
0297             Numpad2,            //!< Keypad 2 and Down Arrow key
0298             Numpad3,            //!< Keypad 3 and Page Down key
0299             Numpad4,            //!< Keypad 4 and Left Arrow key
0300             Numpad5,            //!< Keypad 5 key
0301             Numpad6,            //!< Keypad 6 and Right Arrow key
0302             Numpad7,            //!< Keypad 7 and Home key
0303             Numpad8,            //!< Keypad 8 and Up Arrow key
0304             Numpad9,            //!< Keypad 9 and Page Up key
0305             Numpad0,            //!< Keypad 0 and Insert key
0306             // For US keyboards doesn't exist
0307             // For Non-US keyboards mapped to key 45 (Microsoft Keyboard Scan Code Specification)
0308             // Typical language mappings: Belg:<\> FrCa:«°» Dan:<\> Dutch:]|[ Fren:<> Ger:<|> Ital:<> LatAm:<> Nor:<> Span:<> Swed:<|> Swiss:<\> UK:\| Brazil: \|.
0309             NonUsBackslash,     //!< Keyboard Non-US \ and | key
0310             Application,        //!< Keyboard Application key
0311             Execute,            //!< Keyboard Execute key
0312             ModeChange,         //!< Keyboard Mode Change key
0313             Help,               //!< Keyboard Help key
0314             Menu,               //!< Keyboard Menu key
0315             Select,             //!< Keyboard Select key
0316             Redo,               //!< Keyboard Redo key
0317             Undo,               //!< Keyboard Undo key
0318             Cut,                //!< Keyboard Cut key
0319             Copy,               //!< Keyboard Copy key
0320             Paste,              //!< Keyboard Paste key
0321             VolumeMute,         //!< Keyboard Volume Mute key
0322             VolumeUp,           //!< Keyboard Volume Up key
0323             VolumeDown,         //!< Keyboard Volume Down key
0324             MediaPlayPause,     //!< Keyboard Media Play Pause key
0325             MediaStop,          //!< Keyboard Media Stop key
0326             MediaNextTrack,     //!< Keyboard Media Next Track key
0327             MediaPreviousTrack, //!< Keyboard Media Previous Track key
0328             LControl,           //!< Keyboard Left Control key
0329             LShift,             //!< Keyboard Left Shift key
0330             LAlt,               //!< Keyboard Left Alt key
0331             LSystem,            //!< Keyboard Left System key
0332             RControl,           //!< Keyboard Right Control key
0333             RShift,             //!< Keyboard Right Shift key
0334             RAlt,               //!< Keyboard Right Alt key
0335             RSystem,            //!< Keyboard Right System key
0336             Back,               //!< Keyboard Back key
0337             Forward,            //!< Keyboard Forward key
0338             Refresh,            //!< Keyboard Refresh key
0339             Stop,               //!< Keyboard Stop key
0340             Search,             //!< Keyboard Search key
0341             Favorites,          //!< Keyboard Favorites key
0342             HomePage,           //!< Keyboard Home Page key
0343             LaunchApplication1, //!< Keyboard Launch Application 1 key
0344             LaunchApplication2, //!< Keyboard Launch Application 2 key
0345             LaunchMail,         //!< Keyboard Launch Mail key
0346             LaunchMediaSelect,  //!< Keyboard Launch Media Select key
0347 
0348             ScancodeCount       //!< Keep last -- the total number of scancodes
0349         };
0350 
0351         #if defined(__clang__)
0352             #pragma clang diagnostic pop
0353         #endif
0354     };
0355 
0356     typedef Scan::Scancode Scancode;
0357 
0358     ////////////////////////////////////////////////////////////
0359     /// \brief Check if a key is pressed
0360     ///
0361     /// \param key Key to check
0362     ///
0363     /// \return True if the key is pressed, false otherwise
0364     ///
0365     ////////////////////////////////////////////////////////////
0366     static bool isKeyPressed(Key key);
0367 
0368     ////////////////////////////////////////////////////////////
0369     /// \brief Check if a key is pressed
0370     ///
0371     /// \param code Scancode to check
0372     ///
0373     /// \return True if the physical key is pressed, false otherwise
0374     ///
0375     ////////////////////////////////////////////////////////////
0376     static bool isKeyPressed(Scancode code);
0377 
0378     ////////////////////////////////////////////////////////////
0379     /// \brief Localize a physical key to a logical one
0380     ///
0381     /// \param code Scancode to localize
0382     ///
0383     /// \return The key corresponding to the scancode under the current
0384     ///         keyboard layout used by the operating system, or
0385     ///         sf::Keyboard::Unknown when the scancode cannot be mapped
0386     ///         to a Key.
0387     ///
0388     /// \see delocalize
0389     ///
0390     ////////////////////////////////////////////////////////////
0391     static Key localize(Scancode code);
0392 
0393     ////////////////////////////////////////////////////////////
0394     /// \brief Identify the physical key corresponding to a logical one
0395     ///
0396     /// \param key Key to "delocalize"
0397     ///
0398     /// \return The scancode corresponding to the key under the current
0399     ///         keyboard layout used by the operating system, or
0400     ///         sf::Keyboard::Scan::Unknown when the key cannot be mapped
0401     ///         to a sf::Keyboard::Scancode.
0402     ///
0403     /// \see localize
0404     ///
0405     ////////////////////////////////////////////////////////////
0406     static Scancode delocalize(Key key);
0407 
0408     ////////////////////////////////////////////////////////////
0409     /// \brief Provide a string representation for a given scancode
0410     ///
0411     /// The returned string is a short, non-technical description of
0412     /// the key represented with the given scancode. Most effectively
0413     /// used in user interfaces, as the description for the key takes
0414     /// the users keyboard layout into consideration.
0415     ///
0416     /// \warning The result is OS-dependent: for example, sf::Keyboard::Scan::LSystem
0417     ///          is "Left Meta" on Linux, "Left Windows" on Windows and
0418     ///          "Left Command" on macOS.
0419     ///
0420     /// The current keyboard layout set by the operating system is used to
0421     /// interpret the scancode: for example, sf::Keyboard::Semicolon is
0422     /// mapped to ";" for layout and to "é" for others.
0423     ///
0424     /// \return The localized description of the code
0425     ///
0426     ////////////////////////////////////////////////////////////
0427     static String getDescription(Scancode code);
0428 
0429     ////////////////////////////////////////////////////////////
0430     /// \brief Show or hide the virtual keyboard
0431     ///
0432     /// \warning The virtual keyboard is not supported on all
0433     ///          systems. It will typically be implemented on mobile OSes
0434     ///          (Android, iOS) but not on desktop OSes (Windows, Linux, ...).
0435     ///
0436     /// If the virtual keyboard is not available, this function does
0437     /// nothing.
0438     ///
0439     /// \param visible True to show, false to hide
0440     ///
0441     ////////////////////////////////////////////////////////////
0442     static void setVirtualKeyboardVisible(bool visible);
0443 };
0444 
0445 } // namespace sf
0446 
0447 
0448 #endif // SFML_KEYBOARD_HPP
0449 
0450 
0451 ////////////////////////////////////////////////////////////
0452 /// \class sf::Keyboard
0453 /// \ingroup window
0454 ///
0455 /// sf::Keyboard provides an interface to the state of the
0456 /// keyboard. It only contains static functions (a single
0457 /// keyboard is assumed), so it's not meant to be instantiated.
0458 ///
0459 /// This class allows users to query the keyboard state at any
0460 /// time and directly, without having to deal with a window and
0461 /// its events. Compared to the KeyPressed and KeyReleased events,
0462 /// sf::Keyboard can retrieve the state of a key at any time
0463 /// (you don't need to store and update a boolean on your side
0464 /// in order to know if a key is pressed or released), and you
0465 /// always get the real state of the keyboard, even if keys are
0466 /// pressed or released when your window is out of focus and no
0467 /// event is triggered.
0468 ///
0469 /// Usage example:
0470 /// \code
0471 /// if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
0472 /// {
0473 ///     // move left...
0474 /// }
0475 /// else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
0476 /// {
0477 ///     // move right...
0478 /// }
0479 /// else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
0480 /// {
0481 ///     // quit...
0482 /// }
0483 /// else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Grave))
0484 /// {
0485 ///     // open in-game command line (if it's not already open)
0486 /// }
0487 /// \endcode
0488 ///
0489 /// \see sf::Joystick, sf::Mouse, sf::Touch
0490 ///
0491 ////////////////////////////////////////////////////////////