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> 29 using KeySym = FcitxKeySym;
30 using KeyStates = Flags<KeyState>;
31 using KeyList = std::vector<Key>;
42 class FCITXUTILS_EXPORT
Key {
46 : sym_(sym), states_(states), code_(code) {}
50 explicit Key(
const char *keyString);
54 explicit Key(
const std::string &keyString) :
Key(keyString.c_str()) {}
56 FCITX_INLINE_DEFINE_DEFAULT_DTOR_COPY_AND_MOVE(
Key)
60 return Key(FcitxKey_None, states, code);
65 return sym_ == key.sym_ && states_ == key.states_ && code_ == key.code_;
72 bool check(
const Key &key)
const;
76 bool check(KeySym sym = FcitxKey_None,
78 return check(
Key(sym, states));
95 bool isReleaseOfModifier(
const Key &key)
const;
103 bool isDigit()
const;
134 bool isSimple()
const;
137 bool isModifier()
const;
141 bool isCursorMove()
const;
144 bool isKeyPad()
const;
147 bool hasModifier()
const;
150 bool isVirtual()
const;
158 Key normalize()
const;
167 bool isValid()
const;
169 inline KeySym sym()
const {
return sym_; }
170 inline KeyStates states()
const {
return states_; }
171 inline int code()
const {
return code_; }
174 static KeyStates keySymToStates(KeySym sym);
177 static KeySym keySymFromString(
const std::string &keyString);
183 keySymToString(KeySym sym,
188 static KeySym keySymFromUnicode(uint32_t unicode);
192 static uint32_t keySymToUnicode(KeySym sym);
197 static std::string keySymToUTF8(KeySym sym);
200 static KeyList keyListFromString(
const std::string &str);
203 template <
typename Container>
209 for (
const auto &k : container) {
215 result += k.toString(format);
222 template <
typename Container>
224 return std::ranges::find_if(c, [
this](
const Key &toCheck) {
225 return check(toCheck);
226 }) != std::ranges::end(c);
232 template <
typename Container>
235 for (
auto &toCheck : c) {
236 if (check(toCheck)) {
241 if (idx == c.size()) {
244 return static_cast<int>(idx);
254 #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.