8 #ifndef _FCITX_UTILS_KEY_H_ 9 #define _FCITX_UTILS_KEY_H_ 21 #include <fcitx-utils/fcitxutils_export.h> 24 #include <fcitx-utils/macros.h> 28 using KeySym = FcitxKeySym;
29 using KeyStates = Flags<KeyState>;
30 using KeyList = std::vector<Key>;
41 class FCITXUTILS_EXPORT
Key {
45 : sym_(sym), states_(states), code_(code) {}
49 explicit Key(
const char *keyString);
53 explicit Key(
const std::string &keyString) :
Key(keyString.c_str()) {}
55 FCITX_INLINE_DEFINE_DEFAULT_DTOR_COPY_AND_MOVE(
Key)
59 return Key(FcitxKey_None, states, code);
64 return sym_ == key.sym_ && states_ == key.states_ && code_ == key.code_;
71 bool check(
const Key &key)
const;
75 bool check(KeySym sym = FcitxKey_None,
77 return check(
Key(sym, states));
94 bool isReleaseOfModifier(
const Key &key)
const;
102 bool isDigit()
const;
133 bool isSimple()
const;
136 bool isModifier()
const;
140 bool isCursorMove()
const;
143 bool isKeyPad()
const;
146 bool hasModifier()
const;
149 bool isVirtual()
const;
157 Key normalize()
const;
166 bool isValid()
const;
168 inline KeySym sym()
const {
return sym_; }
169 inline KeyStates states()
const {
return states_; }
170 inline int code()
const {
return code_; }
173 static KeyStates keySymToStates(KeySym sym);
176 static KeySym keySymFromString(
const std::string &keyString);
182 keySymToString(KeySym sym,
187 static KeySym keySymFromUnicode(uint32_t unicode);
191 static uint32_t keySymToUnicode(KeySym sym);
196 static std::string keySymToUTF8(KeySym sym);
199 static KeyList keyListFromString(
const std::string &str);
202 template <
typename Container>
208 for (
const auto &k : container) {
214 result += k.toString(format);
221 template <
typename Container>
223 return std::find_if(c.begin(), c.end(), [
this](
const Key &toCheck) {
224 return check(toCheck);
231 template <
typename Container>
234 for (
auto &toCheck : c) {
235 if (check(toCheck)) {
240 if (idx == c.size()) {
243 return static_cast<int>(idx);
253 #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.