mirror of
https://github.com/SFML/SFML.git
synced 2024-12-01 15:51:04 +08:00
Convert scancodes into a scoped enumeration
This commit is contained in:
parent
17a8e9e2c2
commit
9f08d67dcc
@ -164,20 +164,8 @@ enum Key
|
|||||||
/// keyboard can be used as reference for the physical position of the keys.
|
/// keyboard can be used as reference for the physical position of the keys.
|
||||||
///
|
///
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
struct Scan
|
enum class Scan
|
||||||
{
|
{
|
||||||
// TODO: replace with enum class in SFML 3.
|
|
||||||
// Clang warns us rightfully that Scancode names shadow Key names.
|
|
||||||
// A safer solution would be to use a C++11 scoped enumeration (enum class),
|
|
||||||
// but it is not possible in SFML 2 which uses C++03.
|
|
||||||
// For now, we just ignore those warnings.
|
|
||||||
#if defined(__clang__)
|
|
||||||
#pragma clang diagnostic push
|
|
||||||
#pragma clang diagnostic ignored "-Wshadow"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
enum Scancode
|
|
||||||
{
|
|
||||||
Unknown = -1, //!< Represents any scancode not present in this enum
|
Unknown = -1, //!< Represents any scancode not present in this enum
|
||||||
A = 0, //!< Keyboard a and A key
|
A = 0, //!< Keyboard a and A key
|
||||||
B, //!< Keyboard b and B key
|
B, //!< Keyboard b and B key
|
||||||
@ -333,14 +321,9 @@ struct Scan
|
|||||||
LaunchMediaSelect, //!< Keyboard Launch Media Select key
|
LaunchMediaSelect, //!< Keyboard Launch Media Select key
|
||||||
|
|
||||||
ScancodeCount //!< Keep last -- the total number of scancodes
|
ScancodeCount //!< Keep last -- the total number of scancodes
|
||||||
};
|
|
||||||
|
|
||||||
#if defined(__clang__)
|
|
||||||
#pragma clang diagnostic pop
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
using Scancode = Scan::Scancode;
|
using Scancode = Scan;
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
/// \brief Check if a key is pressed
|
/// \brief Check if a key is pressed
|
||||||
|
@ -286,9 +286,9 @@ private:
|
|||||||
// Member data
|
// Member data
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
IOHIDManagerRef m_manager{}; ///< Underlying HID Manager
|
IOHIDManagerRef m_manager{}; ///< Underlying HID Manager
|
||||||
IOHIDElements m_keys[Keyboard::Scan::ScancodeCount]; ///< All the keys on any connected keyboard
|
IOHIDElements m_keys[static_cast<std::size_t>(Keyboard::Scan::ScancodeCount)]; ///< All the keys on any connected keyboard
|
||||||
Keyboard::Scancode m_keyToScancodeMapping[Keyboard::KeyCount]; ///< Mapping from Key to Scancode
|
Keyboard::Scancode m_keyToScancodeMapping[Keyboard::KeyCount]; ///< Mapping from Key to Scancode
|
||||||
Keyboard::Key m_scancodeToKeyMapping[Keyboard::Scan::ScancodeCount]; ///< Mapping from Scancode to Key
|
Keyboard::Key m_scancodeToKeyMapping[static_cast<std::size_t>(Keyboard::Scan::ScancodeCount)]; ///< Mapping from Scancode to Key
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
/// m_keys' index corresponds to sf::Keyboard::Scancode enum.
|
/// m_keys' index corresponds to sf::Keyboard::Scancode enum.
|
||||||
|
@ -561,7 +561,7 @@ bool HIDInputManager::isKeyPressed(Keyboard::Key key)
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
bool HIDInputManager::isKeyPressed(Keyboard::Scancode code)
|
bool HIDInputManager::isKeyPressed(Keyboard::Scancode code)
|
||||||
{
|
{
|
||||||
return (code != Keyboard::Scan::Unknown) && isPressed(m_keys[code]);
|
return (code != Keyboard::Scan::Unknown) && isPressed(m_keys[static_cast<std::size_t>(code)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -571,7 +571,7 @@ Keyboard::Key HIDInputManager::localize(Keyboard::Scancode code)
|
|||||||
if (code == Keyboard::Scan::Unknown)
|
if (code == Keyboard::Scan::Unknown)
|
||||||
return Keyboard::Unknown;
|
return Keyboard::Unknown;
|
||||||
|
|
||||||
return m_scancodeToKeyMapping[code];
|
return m_scancodeToKeyMapping[static_cast<std::size_t>(code)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -794,7 +794,7 @@ void HIDInputManager::loadKey(IOHIDElementRef key)
|
|||||||
if (code != Keyboard::Scan::Unknown)
|
if (code != Keyboard::Scan::Unknown)
|
||||||
{
|
{
|
||||||
CFRetain(key);
|
CFRetain(key);
|
||||||
m_keys[code].push_back(key);
|
m_keys[static_cast<std::size_t>(code)].push_back(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -823,7 +823,7 @@ void HIDInputManager::buildMappings()
|
|||||||
|
|
||||||
// For each scancode having a IOHIDElement, we translate the corresponding
|
// For each scancode having a IOHIDElement, we translate the corresponding
|
||||||
// virtual code to a localized Key.
|
// virtual code to a localized Key.
|
||||||
for (int i = 0; i < Keyboard::Scan::ScancodeCount; ++i)
|
for (int i = 0; i < static_cast<int>(Keyboard::Scan::ScancodeCount); ++i)
|
||||||
{
|
{
|
||||||
Keyboard::Scancode scan = static_cast<Keyboard::Scancode>(i);
|
Keyboard::Scancode scan = static_cast<Keyboard::Scancode>(i);
|
||||||
std::uint8_t virtualCode = scanToVirtualCode(scan);
|
std::uint8_t virtualCode = scanToVirtualCode(scan);
|
||||||
@ -898,7 +898,7 @@ void HIDInputManager::buildMappings()
|
|||||||
// Register the bi-mapping
|
// Register the bi-mapping
|
||||||
if (m_keyToScancodeMapping[code] == Keyboard::Scan::Unknown)
|
if (m_keyToScancodeMapping[code] == Keyboard::Scan::Unknown)
|
||||||
m_keyToScancodeMapping[code] = scan;
|
m_keyToScancodeMapping[code] = scan;
|
||||||
m_scancodeToKeyMapping[scan] = code;
|
m_scancodeToKeyMapping[static_cast<std::size_t>(scan)] = code;
|
||||||
}
|
}
|
||||||
|
|
||||||
CFRelease(tis);
|
CFRelease(tis);
|
||||||
|
@ -46,7 +46,7 @@ namespace
|
|||||||
|
|
||||||
const KeyCode nullKeyCode = 0;
|
const KeyCode nullKeyCode = 0;
|
||||||
const int maxKeyCode = 256;
|
const int maxKeyCode = 256;
|
||||||
KeyCode scancodeToKeycode[sf::Keyboard::Scan::ScancodeCount]; ///< Mapping of SFML scancode to X11 KeyCode
|
KeyCode scancodeToKeycode[static_cast<std::size_t>(sf::Keyboard::Scan::ScancodeCount)]; ///< Mapping of SFML scancode to X11 KeyCode
|
||||||
sf::Keyboard::Scancode keycodeToScancode[maxKeyCode]; ///< Mapping of X11 KeyCode to SFML scancode
|
sf::Keyboard::Scancode keycodeToScancode[maxKeyCode]; ///< Mapping of X11 KeyCode to SFML scancode
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
@ -491,7 +491,7 @@ void ensureMapping()
|
|||||||
scancode = mappedScancode->second;
|
scancode = mappedScancode->second;
|
||||||
|
|
||||||
if (scancode != sf::Keyboard::Scan::Unknown)
|
if (scancode != sf::Keyboard::Scan::Unknown)
|
||||||
scancodeToKeycode[scancode] = static_cast<KeyCode>(keycode);
|
scancodeToKeycode[static_cast<std::size_t>(scancode)] = static_cast<KeyCode>(keycode);
|
||||||
|
|
||||||
keycodeToScancode[keycode] = scancode;
|
keycodeToScancode[keycode] = scancode;
|
||||||
}
|
}
|
||||||
@ -506,8 +506,9 @@ void ensureMapping()
|
|||||||
{
|
{
|
||||||
scancode = translateKeyCode(display, static_cast<KeyCode>(keycode));
|
scancode = translateKeyCode(display, static_cast<KeyCode>(keycode));
|
||||||
|
|
||||||
if (scancode != sf::Keyboard::Scan::Unknown && scancodeToKeycode[scancode] == nullKeyCode)
|
if (scancode != sf::Keyboard::Scan::Unknown &&
|
||||||
scancodeToKeycode[scancode] = static_cast<KeyCode>(keycode);
|
scancodeToKeycode[static_cast<std::size_t>(scancode)] == nullKeyCode)
|
||||||
|
scancodeToKeycode[static_cast<std::size_t>(scancode)] = static_cast<KeyCode>(keycode);
|
||||||
|
|
||||||
keycodeToScancode[keycode] = scancode;
|
keycodeToScancode[keycode] = scancode;
|
||||||
}
|
}
|
||||||
@ -525,7 +526,7 @@ KeyCode scancodeToKeyCode(sf::Keyboard::Scancode code)
|
|||||||
ensureMapping();
|
ensureMapping();
|
||||||
|
|
||||||
if (code != sf::Keyboard::Scan::Unknown)
|
if (code != sf::Keyboard::Scan::Unknown)
|
||||||
return scancodeToKeycode[code];
|
return scancodeToKeycode[static_cast<std::size_t>(code)];
|
||||||
|
|
||||||
return nullKeyCode;
|
return nullKeyCode;
|
||||||
}
|
}
|
||||||
@ -560,7 +561,7 @@ KeyCode keyToKeyCode(sf::Keyboard::Key key)
|
|||||||
|
|
||||||
// Fallback for when XKeysymToKeycode cannot tell the KeyCode for XK_Alt_R
|
// Fallback for when XKeysymToKeycode cannot tell the KeyCode for XK_Alt_R
|
||||||
if (key == sf::Keyboard::RAlt)
|
if (key == sf::Keyboard::RAlt)
|
||||||
return scancodeToKeycode[sf::Keyboard::Scan::RAlt];
|
return scancodeToKeycode[static_cast<std::size_t>(sf::Keyboard::Scan::RAlt)];
|
||||||
|
|
||||||
return nullKeyCode;
|
return nullKeyCode;
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ namespace sf::priv
|
|||||||
{
|
{
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
Keyboard::Scancode InputImpl::m_keyToScancodeMapping[Keyboard::KeyCount]; ///< Mapping from Key to Scancode
|
Keyboard::Scancode InputImpl::m_keyToScancodeMapping[Keyboard::KeyCount]; ///< Mapping from Key to Scancode
|
||||||
Keyboard::Key InputImpl::m_scancodeToKeyMapping[Keyboard::Scan::ScancodeCount]; ///< Mapping from Scancode to Key
|
Keyboard::Key InputImpl::m_scancodeToKeyMapping[static_cast<std::size_t>(Keyboard::Scan::ScancodeCount)]; ///< Mapping from Scancode to Key
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
Keyboard::Key virtualKeyToSfKey(UINT virtualKey)
|
Keyboard::Key virtualKeyToSfKey(UINT virtualKey)
|
||||||
@ -541,14 +541,14 @@ void InputImpl::ensureMappings()
|
|||||||
key = Keyboard::Unknown;
|
key = Keyboard::Unknown;
|
||||||
|
|
||||||
// Phase 2: Translate scancode to virtual code to key names
|
// Phase 2: Translate scancode to virtual code to key names
|
||||||
for (int i = 0; i < Keyboard::Scan::ScancodeCount; ++i)
|
for (int i = 0; i < static_cast<int>(Keyboard::Scan::ScancodeCount); ++i)
|
||||||
{
|
{
|
||||||
Keyboard::Scancode scan = static_cast<Keyboard::Scancode>(i);
|
Keyboard::Scancode scan = static_cast<Keyboard::Scancode>(i);
|
||||||
UINT virtualKey = sfScanToVirtualKey(scan);
|
UINT virtualKey = sfScanToVirtualKey(scan);
|
||||||
Keyboard::Key key = virtualKeyToSfKey(virtualKey);
|
Keyboard::Key key = virtualKeyToSfKey(virtualKey);
|
||||||
if (key != Keyboard::Unknown && m_keyToScancodeMapping[key] == Keyboard::Scan::Unknown)
|
if (key != Keyboard::Unknown && m_keyToScancodeMapping[key] == Keyboard::Scan::Unknown)
|
||||||
m_keyToScancodeMapping[key] = scan;
|
m_keyToScancodeMapping[key] = scan;
|
||||||
m_scancodeToKeyMapping[scan] = key;
|
m_scancodeToKeyMapping[static_cast<std::size_t>(scan)] = key;
|
||||||
}
|
}
|
||||||
|
|
||||||
isMappingInitialized = true;
|
isMappingInitialized = true;
|
||||||
@ -576,7 +576,7 @@ Keyboard::Key InputImpl::localize(Keyboard::Scancode code)
|
|||||||
|
|
||||||
ensureMappings();
|
ensureMappings();
|
||||||
|
|
||||||
return m_scancodeToKeyMapping[code];
|
return m_scancodeToKeyMapping[static_cast<std::size_t>(code)];
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
|
@ -30,6 +30,8 @@
|
|||||||
#include <SFML/Window/Keyboard.hpp>
|
#include <SFML/Window/Keyboard.hpp>
|
||||||
#include <SFML/Window/Mouse.hpp>
|
#include <SFML/Window/Mouse.hpp>
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
|
||||||
namespace sf::priv
|
namespace sf::priv
|
||||||
{
|
{
|
||||||
@ -185,7 +187,7 @@ private:
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
// NOLINTBEGIN(readability-identifier-naming)
|
// NOLINTBEGIN(readability-identifier-naming)
|
||||||
static Keyboard::Scancode m_keyToScancodeMapping[Keyboard::KeyCount]; ///< Mapping from Key to Scancode
|
static Keyboard::Scancode m_keyToScancodeMapping[Keyboard::KeyCount]; ///< Mapping from Key to Scancode
|
||||||
static Keyboard::Key m_scancodeToKeyMapping[Keyboard::Scan::ScancodeCount]; ///< Mapping from Scancode to Key
|
static Keyboard::Key m_scancodeToKeyMapping[static_cast<std::size_t>(Keyboard::Scan::ScancodeCount)]; ///< Mapping from Scancode to Key
|
||||||
// NOLINTEND(readability-identifier-naming)
|
// NOLINTEND(readability-identifier-naming)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user