openMSX
KeyCodeSetting.cc
Go to the documentation of this file.
1 #include "KeyCodeSetting.hh"
2 #include "CommandException.hh"
3 
4 namespace openmsx {
5 
7  std::string_view name_, std::string_view description_,
8  Keys::KeyCode initialValue)
9  : Setting(commandController_, name_, description_,
10  TclObject(Keys::getName(initialValue)), SAVE)
11 {
12  setChecker([](TclObject& newValue) {
13  const auto& str = newValue.getString();
14  if (Keys::getCode(str) == Keys::K_NONE) {
15  throw CommandException("Not a valid key: ", str);
16  }
17  });
18  init();
19 }
20 
21 std::string_view KeyCodeSetting::getTypeString() const
22 {
23  return "key";
24 }
25 
27 {
28  return Keys::getCode(getValue().getString());
29 }
30 
31 } // namespace openmsx
std::string_view getTypeString() const override
Returns a string describing the setting type (integer, string, ..) Could be used in a GUI to pick an ...
void setChecker(std::function< void(TclObject &)> checkFunc_)
Set value-check-callback.
Definition: Setting.hh:152
KeyCodeSetting(CommandController &commandController, std::string_view name, std::string_view description, Keys::KeyCode initialValue)
Keys::KeyCode getKey() const noexcept
KeyCode
Constants that identify keys and key modifiers.
Definition: Keys.hh:25
const TclObject & getValue() const final override
Gets the current value of this setting as a TclObject.
Definition: Setting.hh:135
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
std::string_view getString() const
Definition: TclObject.cc:102
string getName(KeyCode keyCode)
Translate key code to key name.
Definition: Keys.cc:589
KeyCode getCode(string_view name)
Definition: Keys.cc:341