8 #ifndef _FCITX_UTILS_KEY_H_ 9 #define _FCITX_UTILS_KEY_H_ 23 #include "fcitxutils_export.h" 27 using KeySym = FcitxKeySym;
28 using KeyStates = Flags<KeyState>;
29 using KeyList = std::vector<Key>;
40 class FCITXUTILS_EXPORT
Key {
44 : sym_(sym), states_(states), code_(code) {}
48 explicit Key(
const char *keyString);
52 explicit Key(
const std::string &keyString) :
Key(keyString.c_str()) {}
54 FCITX_INLINE_DEFINE_DEFAULT_DTOR_COPY_AND_MOVE(
Key)
58 return Key(FcitxKey_None, states, code);
63 return sym_ == key.sym_ && states_ == key.states_ && code_ == key.code_;
70 bool check(
const Key &key)
const;
74 bool check(KeySym sym = FcitxKey_None,
76 return check(
Key(sym, states));
93 bool isReleaseOfModifier(
const Key &key)
const;
101 bool isDigit()
const;
132 bool isSimple()
const;
135 bool isModifier()
const;
139 bool isCursorMove()
const;
142 bool isKeyPad()
const;
145 bool hasModifier()
const;
148 bool isVirtual()
const;
156 Key normalize()
const;
165 bool isValid()
const;
167 inline KeySym sym()
const {
return sym_; }
168 inline KeyStates states()
const {
return states_; }
169 inline int code()
const {
return code_; }
172 static KeyStates keySymToStates(KeySym sym);
175 static KeySym keySymFromString(
const std::string &keyString);
181 keySymToString(KeySym sym,
186 static KeySym keySymFromUnicode(uint32_t unicode);
190 static uint32_t keySymToUnicode(KeySym sym);
195 static std::string keySymToUTF8(KeySym sym);
198 static KeyList keyListFromString(
const std::string &str);
201 template <
typename Container>
207 for (
const auto &k : container) {
213 result += k.toString(format);
220 template <
typename Container>
222 return std::find_if(c.begin(), c.end(), [
this](
const Key &toCheck) {
223 return check(toCheck);
230 template <
typename Container>
233 for (
auto &toCheck : c) {
234 if (check(toCheck)) {
239 if (idx == c.size()) {
242 return static_cast<int>(idx);
252 #endif // _FCITX_UTILS_KEY_H_
bool operator!=(const Key &key) const
Check if key is not same;.
bool checkKeyList(const Container &c) const
Check the current key against a key list.
KeyStringFormat
Control the behavior of toString function.
Can be used to parse from a string.
bool operator==(const Key &key) const
Check if key is exactly same.
bool check(KeySym sym=FcitxKey_None, KeyStates states=KeyStates()) const
Check if current key match the sym and states.
int keyListIndex(const Container &c) const
Check the current key against a key list and get the matched key index.
Return the human readable string in localized format.
Helper template class to make easier to use type safe enum flags.
static std::string keyListToString(const Container &container, KeyStringFormat format=KeyStringFormat::Portable)
Convert a key list to string.
Key(const std::string &keyString)
Parse a key from std::string.