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
openmsx::CommandException
Definition: CommandException.hh:8
openmsx::KeyCodeSetting::KeyCodeSetting
KeyCodeSetting(CommandController &commandController, std::string_view name, std::string_view description, Keys::KeyCode initialValue)
Definition: KeyCodeSetting.cc:6
openmsx::CommandController
Definition: CommandController.hh:17
openmsx::Setting::getValue
const TclObject & getValue() const final override
Gets the current value of this setting as a TclObject.
Definition: Setting.hh:135
openmsx::Setting
Definition: Setting.hh:119
openmsx::KeyCodeSetting::getKey
Keys::KeyCode getKey() const noexcept
Definition: KeyCodeSetting.cc:26
openmsx::Keys::getCode
KeyCode getCode(string_view name)
Translate key name to key code.
Definition: Keys.cc:341
openmsx::Keys::K_NONE
@ K_NONE
Definition: Keys.hh:29
openmsx::Keys::getName
string getName(KeyCode keyCode)
Translate key code to key name.
Definition: Keys.cc:740
KeyCodeSetting.hh
openmsx::Setting::init
void init()
Definition: Setting.cc:54
openmsx::KeyCodeSetting::getTypeString
std::string_view getTypeString() const override
Returns a string describing the setting type (integer, string, ..) Could be used in a GUI to pick an ...
Definition: KeyCodeSetting.cc:21
openmsx::TclObject::getString
std::string_view getString() const
Definition: TclObject.cc:102
openmsx::TclObject
Definition: TclObject.hh:21
openmsx::Setting::setChecker
void setChecker(std::function< void(TclObject &)> checkFunc_)
Set value-check-callback.
Definition: Setting.hh:152
openmsx::Keys::KeyCode
KeyCode
Constants that identify keys and key modifiers.
Definition: Keys.hh:26
CommandException.hh
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5