openMSX
KeyCodeSetting.cc
Go to the documentation of this file.
1 #include "KeyCodeSetting.hh"
2 #include "CommandException.hh"
3 
4 namespace openmsx {
5 
7  string_view name_, 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 
22 {
23  return "key";
24 }
25 
27 {
28  return Keys::getCode(getValue().getString());
29 }
30 
31 } // namespace openmsx
void setChecker(std::function< void(TclObject &)> checkFunc_)
Set value-check-callback.
Definition: Setting.hh:151
string_view getString() const
Definition: TclObject.cc:102
KeyCode
Constants that identify keys and key modifiers.
Definition: Keys.hh:26
const TclObject & getValue() const final override
Gets the current value of this setting as a TclObject.
Definition: Setting.hh:134
Keys::KeyCode getKey() const
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
KeyCodeSetting(CommandController &commandController, string_view name, string_view description, Keys::KeyCode initialValue)
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:16
string getName(KeyCode keyCode)
Translate key code to key name.
Definition: Keys.cc:589
string_view getTypeString() const override
Returns a string describing the setting type (integer, string, ..) Could be used in a GUI to pick an ...
KeyCode getCode(string_view name)
Translate key name to key code.
Definition: Keys.cc:341