1 /* 2 * Copyright (c) 2021-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef KEY_OPTION_H 17 #define KEY_OPTION_H 18 19 #include <set> 20 #include "parcel.h" 21 22 namespace OHOS { 23 namespace MMI { 24 enum SubscribePriority { 25 PRIORITY_0 = 0, 26 PRIORITY_100 = 100, 27 }; 28 class KeyOption : public Parcelable { 29 public: 30 KeyOption() = default; 31 32 public: 33 /** 34 * @brief Obtains previous keys. 35 * @return Returns previous keys. 36 * @since 9 37 */ 38 std::set<int32_t> GetPreKeys() const; 39 40 /** 41 * @brief Sets previous keys, that is, the keys that are pressed first in a combination key. 42 * There is no requirement on the sequence of previous keys. 43 * @param preKeys Indicates the previous keys to set. 44 * @return void 45 * @since 9 46 */ 47 void SetPreKeys(const std::set<int32_t>& preKeys); 48 49 /** 50 * @brief Obtains the final key. 51 * @return Returns the final key. 52 * @since 9 53 */ 54 int32_t GetFinalKey() const; 55 56 /** 57 * @brief Sets the final key, that is, the key that is last pressed or released in a combination key. 58 * @param finalKey Indicates the final key. 59 * @return void 60 * @since 9 61 */ 62 void SetFinalKey(int32_t finalKey); 63 64 /** 65 * @brief Checks whether the final key in a combination key is pressed or released. 66 * @return Returns <b>true</b> if the key is pressed; returns <b>false</b> if the key is released. 67 * @since 9 68 */ 69 bool IsFinalKeyDown() const; 70 71 /** 72 * @brief Sets whether the final key in a combination key is pressed or released. 73 * @param pressed Indicates whether the key is pressed. The value <b>true</b> means that the key 74 * is pressed, and the value <b>false</b> means that the key is released. 75 * @return void 76 * @since 9 77 */ 78 void SetFinalKeyDown(bool pressed); 79 80 /** 81 * @brief Obtains the duration when the final key is held down or the maximum duration between 82 * when the key is pressed and when the key is released. 83 * If the final key is pressed, this parameter indicates the duration when the final key is held down. 84 * If the last key is released, this parameter indicates the maximum duration between when the key 85 * is pressed and when the key is released. 86 * @return Returns the duration when the final key is held down or the maximum duration between 87 * when the key is pressed and when the key is released. 88 * @since 9 89 */ 90 int32_t GetFinalKeyDownDuration() const; 91 92 /** 93 * @brief Get the delay time of lifting the last key. When the last key is lifted, the subscription 94 * will be delayed and triggered. 95 * @return Return to the delay time of lifting the last key. 96 * @since 9 97 */ 98 int32_t GetFinalKeyUpDelay() const; 99 100 /** 101 * @brief Sets the duration when the final key is held down or the maximum duration between when 102 * the key is pressed and when the key is released. 103 * If the final key is pressed, this parameter indicates the duration when the final key is held down. 104 * If the last key is released, this parameter indicates the maximum duration between when the key 105 * is pressed and when the key is released. 106 * @param duration Indicates the duration when the final key is held down or the maximum duration 107 * between when the key is pressed and when the key is released. 108 * @return void 109 * @since 9 110 */ 111 void SetFinalKeyDownDuration(int32_t duration); 112 113 /** 114 * @brief Set the delay time for lifting the last key. 115 * @param delay Delay time for lifting the last key. 116 * @return void 117 * @since 9 118 */ 119 void SetFinalKeyUpDelay(int32_t delay); 120 121 bool IsRepeat() const; 122 123 void SetRepeat(bool repeat); 124 125 int32_t GetPriority() const; 126 127 void SetPriority(int32_t priority); 128 129 public: 130 /** 131 * @brief Writes data to a <b>Parcel</b> object. 132 * @param out Indicates the object into which data will be written. 133 * @return Returns <b>true</b> if the data is successfully written; returns <b>false</b> otherwise. 134 * @since 9 135 */ 136 bool WriteToParcel(Parcel &out) const; 137 138 /** 139 * @brief Reads data from a <b>Parcel</b> object. 140 * @param in Indicates the object from which data will be read. 141 * @return Returns <b>true</b> if the data is successfully read; returns <b>false</b> otherwise. 142 * @since 9 143 */ 144 bool ReadFromParcel(Parcel &in); 145 Marshalling(Parcel & out)146 bool Marshalling(Parcel &out) const 147 { 148 return WriteToParcel(out); 149 } 150 Unmarshalling(Parcel & in)151 static KeyOption* Unmarshalling(Parcel &in) 152 { 153 auto keyOption = new (std::nothrow) KeyOption(); 154 if (keyOption && !keyOption->ReadFromParcel(in)) { 155 delete keyOption; 156 keyOption = nullptr; 157 } 158 return keyOption; 159 } 160 161 private: 162 std::set<int32_t> preKeys_ {}; 163 int32_t finalKey_ { -1 }; 164 bool isFinalKeyDown_ { false }; 165 int32_t finalKeyDownDuration_ { 0 }; 166 int32_t finalKeyUpDelay_ { 0 }; 167 bool isRepeat_ { true }; 168 int32_t priority_ = SubscribePriority::PRIORITY_0; 169 }; 170 171 class KeyMonitorOption final : public Parcelable { 172 public: 173 KeyMonitorOption() = default; 174 ~KeyMonitorOption() = default; 175 176 int32_t GetKey() const; 177 int32_t GetAction() const; 178 bool IsRepeat() const; 179 180 void SetKey(int32_t key); 181 void SetAction(int32_t action); 182 void SetRepeat(bool repeat); 183 Marshalling(Parcel & parcel)184 bool Marshalling(Parcel &parcel) const 185 { 186 return (parcel.WriteInt32(key_) && 187 parcel.WriteInt32(action_) && 188 parcel.WriteBool(isRepeat_)); 189 } 190 ReadFromParcel(Parcel & parcel)191 bool ReadFromParcel(Parcel &parcel) 192 { 193 return ( 194 parcel.ReadInt32(key_) && 195 parcel.ReadInt32(action_) && 196 parcel.ReadBool(isRepeat_) 197 ); 198 } 199 Unmarshalling(Parcel & parcel)200 static KeyMonitorOption* Unmarshalling(Parcel &parcel) 201 { 202 auto data = new (std::nothrow) KeyMonitorOption(); 203 if (data && !data->ReadFromParcel(parcel)) { 204 delete data; 205 data = nullptr; 206 } 207 return data; 208 } 209 210 private: 211 int32_t key_ {}; 212 int32_t action_ {}; 213 bool isRepeat_ {}; 214 }; 215 } // namespace MMI 216 } // namespace OHOS 217 #endif // KEY_OPTION_H 218