Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDL: Use scancodes for keybindings #14964

Draft
wants to merge 24 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions irr/include/IrrlichtDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "IrrCompileConfig.h"
#include "position2d.h"
#include "SColor.h" // video::ECOLOR_FORMAT
#include <variant>

namespace irr
{
Expand Down Expand Up @@ -345,6 +346,27 @@ class IrrlichtDevice : public virtual IReferenceCounted
{
return video::isDriverSupported(driver);
}

//! Get the scancode of the corresponding keycode.
/**
\param key The keycode to convert.
\return The implementation-dependent scancode for the key (represented by the u32 component) or, if a scancode is not
available, the corresponding Irrlicht keycode (represented by the EKEY_CODE component).
*/
virtual std::variant<u32, EKEY_CODE> getScancodeFromKey(const Keycode &key) const {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the u32 type represent an implementation-defined code for the key, is that correct?
please document this better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. This is now documentated in 9590be1.

if (auto pv = std::get_if<EKEY_CODE>(&key))
return *pv;
return (u32)std::get<wchar_t>(key);
}

//! Get the keycode of the corresponding scancode.
/**
\param scancode The implementation-dependent scancode for the key.
\return The corresponding keycode.
*/
virtual Keycode getKeyFromScancode(const u32 scancode) const {
return Keycode(KEY_UNKNOWN, (wchar_t)scancode);
}
};

} // end namespace irr
27 changes: 27 additions & 0 deletions irr/include/Keycodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in irrlicht.h

#pragma once
#include <variant>

namespace irr
{
Expand Down Expand Up @@ -182,4 +183,30 @@ enum EKEY_CODE
KEY_KEY_CODES_COUNT = 0x100 // this is not a key, but the amount of keycodes there are.
};

// A Keycode is either a character produced by the key or one of Irrlicht's codes (EKEY_CODE)
class Keycode : public std::variant<EKEY_CODE, wchar_t> {
y5nw marked this conversation as resolved.
Show resolved Hide resolved
using super = std::variant<EKEY_CODE, wchar_t>;
public:
Keycode() : Keycode(KEY_KEY_CODES_COUNT, L'\0') {}

Keycode(EKEY_CODE code, wchar_t ch)
{
emplace(code, ch);
}

using super::emplace;
void emplace(EKEY_CODE code, wchar_t ch)
{
if (isValid(code))
emplace<EKEY_CODE>(code);
else
emplace<wchar_t>(ch);
}

static bool isValid(EKEY_CODE code)
{
return code > 0 && code < KEY_KEY_CODES_COUNT;
}
};

} // end namespace irr
Loading
Loading