Key recognition method, device, equipment and storage medium
By periodically collecting and counting key states and using key-value code queue analysis to achieve key anti-shake and anti-leakage, the problem of low key recognition efficiency in the existing technology is solved, and the key response efficiency and accuracy are improved.
Patent Information
- Application Number
- CN202110419418.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-04-19
- Publication Date
- 2025-09-30
- Estimated Expiration
- 2041-04-19
AI Technical Summary
Existing key recognition solutions have deficiencies in anti-shake and anti-leakage, especially in the non-blocking mode, which cannot effectively recognize keys, resulting in low equipment operation efficiency.
The key status is periodically collected at a preset frequency, the count value is updated through the state counter, and when the count value is within the preset value range, it is written into the key-value code queue, and key recognition is achieved by combining key-value code queue analysis.
It effectively prevents key jitter and missed keys, improves key response efficiency and accuracy, and improves human-computer interaction.
Smart Images

Figure CN113114268B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present invention relate to the field of microcomputer (single-chip microcomputer) technology, and in particular to a key recognition method, device, equipment and storage medium. Background Art
[0002] With the development of microcomputer technology, smart devices with buttons have been widely used. As an important bridge for human-computer interaction, button recognition is particularly important for smart devices.
[0003] Key recognition is the process of identifying a key press. The entire time from a key press to its release is considered a valid key press, requiring a response from the user program. During the key press process, brief jitter may occur due to electromagnetic interference and other factors. Key recognition eliminates this jitter, a process known as de-shake. Inconsistencies between the moment of "press" and the moment of "recognizing a key press" can result in missed key presses, leading to missed key presses. Key recognition aims to prevent these missed key presses, a process known as leak prevention. The key to key recognition lies in both de-shake and leak prevention. De-shake prevents invalid key presses from being added, while leak prevention prevents valid key presses from being omitted.
[0004] Currently, the ways in which user programs obtain key values are divided into blocking and non-blocking. Blocking and non-blocking refer to the state of the user program when it is waiting for the key result (i.e., key recognition result) to be returned. In blocking mode, the current program will be suspended (paused) before waiting for the key result to be returned, and will return (resume operation) after obtaining the key result. In non-blocking mode, the user program will check the key result regularly without pausing and waiting. If the key result cannot be obtained immediately, the program will not be suspended. The blocking mode will cause slow key response and affect the operating efficiency of the device. However, if the non-blocking mode is used in the existing technology, it will not be able to achieve good anti-shake or anti-leakage. Therefore, the existing key recognition solution is not perfect and needs to be improved. Summary of the Invention
[0005] The embodiments of the present invention provide a key recognition method, apparatus, device and storage medium, which can optimize the existing key recognition solution.
[0006] In a first aspect, an embodiment of the present invention provides a key recognition method, comprising:
[0007] Periodically collecting the key status of a key at a preset frequency, wherein the key status includes pressed and released;
[0008] updating the count value of the state counter of the corresponding key according to the collected key state, and writing the key-value code of the corresponding key into a preset key-value code queue when the count value is within a preset value range, wherein the key-value code includes a key value and a key state, and the key value corresponds to the name of the key;
[0009] The key-value codes are sequentially acquired from the preset key-value code queue, and the acquired key-value codes are analyzed to obtain a key recognition result.
[0010] In a second aspect, an embodiment of the present invention provides a key recognition device, comprising:
[0011] A key state acquisition module, configured to periodically acquire the key state of a key at a preset frequency, wherein the key state includes pressed and released;
[0012] A key-value code writing module is used to update the count value of the state counter of the corresponding key according to the collected key state, and when the count value is within a preset value range, write the key-value code of the corresponding key into a preset key-value code queue, wherein the key-value code includes a key value and a key state, and the key value corresponds to the name of the key;
[0013] The key analysis module is used to sequentially obtain key-value codes from the preset key-value code queue and analyze the obtained key-value codes to obtain key recognition results.
[0014] In a third aspect, an embodiment of the present invention provides an intelligent device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, a key recognition method as provided in an embodiment of the present invention is implemented.
[0015] In a fourth aspect, an embodiment of the present invention provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the key recognition method provided in the embodiment of the present invention.
[0016] The key recognition scheme provided in the embodiment of the present invention adopts a preset frequency to periodically collect the key state of the key, wherein the key state includes pressing and popping up; the count value of the state counter of the corresponding key is updated according to the collected key state, and when the count value is within the preset value range, the key value code of the corresponding key is written into the preset key value code queue, wherein the key value code includes the key value and the key state, and the key value corresponds to the name of the key; the key value code is sequentially obtained from the preset key value code queue, and the obtained key value code is analyzed to obtain the key recognition result. By adopting the above technical solution, the key state of the key is sampled and counted multiple times, and the key value code that meets the count value requirement is subsequently processed, which can effectively realize key anti-shake, and the key value code that meets the count value requirement is stored in the queue, waiting for subsequent reading and analysis, which can effectively realize key leakage prevention, and finally realize accurate key recognition, improve key response efficiency and accuracy, and improve human-computer interaction effect. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] Figure 1 A flowchart of a key recognition method provided by an embodiment of the present invention;
[0018] Figure 2 A schematic flow chart of another key recognition method provided by an embodiment of the present invention;
[0019] Figure 3 A schematic diagram of the functional modules of a key recognition method provided by an embodiment of the present invention;
[0020] Figure 4 A schematic diagram of a key value acquisition process provided by an embodiment of the present invention;
[0021] Figure 5 A schematic diagram of a preset key-value code queue provided by an embodiment of the present invention;
[0022] Figure 6 A schematic diagram of a key value storage process provided by an embodiment of the present invention;
[0023] Figure 7 A schematic diagram of a key-value reading process provided by an embodiment of the present invention;
[0024] Figure 8 A schematic diagram of a key-value analysis process provided by an embodiment of the present invention;
[0025] Figure 9 A structural block diagram of a key recognition device provided by an embodiment of the present invention;
[0026] Figure 10 This is a structural block diagram of an intelligent device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0027] The technical solution of the present invention will be further described below with reference to the accompanying drawings and through specific embodiments. It will be understood that the specific embodiments described herein are intended only to illustrate the present invention and are not intended to limit the present invention. It should also be noted that, for ease of description, the accompanying drawings only illustrate portions relevant to the present invention, not all of the structures.
[0028] Before discussing the exemplary embodiments in more detail, it should be mentioned that some exemplary embodiments are described as processes or methods depicted as flow charts. Although the flow charts describe the steps as sequential processes, many of the steps can be implemented in parallel, concurrently, or simultaneously. In addition, the order of the steps can be rearranged. The process can be terminated when its operation is completed, but can also have additional steps not included in the accompanying drawings. The process can correspond to a method, function, procedure, subroutine, subprogram, etc.
[0029] Figure 1 A flowchart of a key recognition method provided in an embodiment of the present invention, the method can be executed by a key recognition device, wherein the device can be implemented by software and / or hardware, and can generally be integrated into a smart device, specifically a microcomputer device, wherein the smart device can be, for example, a mobile phone or tablet computer equipped with keys, or various industrial production equipment, control equipment, measuring equipment or detection equipment, etc. equipped with keys, or other devices equipped with keys, without specific limitation. Optionally, the smart device can include a single-chip microcomputer, and the program for implementing the key recognition method can be stored in the single-chip microcomputer. Figure 1 As shown, the method includes:
[0030] Step 101: Periodically collect the key status of a key using a preset frequency, wherein the key status includes pressed and released.
[0031] For example, a smart device may be configured with multiple buttons. In the embodiments of the present invention, the specific number and location of the buttons are not specifically limited. For example, a common industrial device may be configured with a button panel that may include multiple buttons, such as "up," "down," "left," "right," "confirm," and "cancel."
[0032] For example, when the user's finger or other part presses the button down until the finger is released, the button remains in a pressed state. This state can be detected by the smart device, which can generally be achieved by scanning the electrical level of the button (such as a high level). The button state detected at this time is called a pressed state, hereinafter referred to as pressed; when the user's finger is not pressing the button, or when the user's finger presses and leaves the button, the button is in an unpressed state. This state can be detected by the smart device, which can generally be achieved by scanning the electrical level of the button (such as a low level). The button state detected at this time is called a pop-up state, hereinafter referred to as pop-up.
[0033] For example, the key status of a key can be periodically collected at a preset frequency (that is, once every preset time interval). The preset frequency can be set according to actual needs, and the specific value is not limited. Generally, it is necessary to ensure that the key status can be collected multiple times within the duration of an effective key operation. For example, the time it takes for the user to normally press and lift the key is 200 milliseconds, and the preset frequency can be set to 30 milliseconds each time. Under normal circumstances, the key status of the key can be collected 6 to 7 times as being pressed.
[0034] In the embodiment of the present invention, the keys for which key status collection is required may include all or some of the keys in the smart device, without specific limitation. When the number of keys for which key status collection is required is at least two, key status collection may be performed sequentially for each key, or the key status of multiple keys may be collected in parallel, depending on the actual situation.
[0035] Step 102: Update the count value of the state counter of the corresponding key according to the collected key state, and when the count value is within a preset value range, write the key value code of the corresponding key into a preset key value code queue, wherein the key value code includes a key value and a key state, and the key value corresponds to the name of the key.
[0036] For example, a corresponding state counter can be set for each key to continuously count the key state of a single key to a certain extent. During the pressing process of a key, due to electromagnetic interference or other reasons, a brief interruption of jitter may occur. That is, due to interference, the user's original pressing of the key is mistakenly recognized as a bounce when the key is not released. When the interference disappears, it is recognized as a press again. After the user releases the key, it is recognized as a bounce again. In the prior art, due to the lack of anti-shake processing, the user's single key operation is mistakenly recognized as two. In the present application, the count value of the state counter of the corresponding key can be updated according to the periodically collected key state. For example, for key A, the count value of the corresponding state counter is a. The key state of key A is periodically collected, and a is updated each time the key state is collected to achieve continuous statistics of the key state of key A. Whether the key state of key A is sufficiently stable is determined based on whether a is within a preset value range. If it is sufficiently stable, it can be used for subsequent analysis. For example, if a is within the preset value range, the key value code corresponding to key A is written into the preset key value code queue for subsequent analysis.
[0037] There are no specific restrictions on how the state counter updates its count value. Presses and releases can be counted separately or collectively. For example, if the currently captured key press state is pressed, the count value increases by 1. If the currently captured key release state is released, the count value remains unchanged or decreases by 1. For example, if the currently captured key press state is pressed, the count value increases by 1; if the currently captured key release state is released, the count value decreases by 1. For example, if the currently captured key press state is pressed, the count value increases by 1; if the currently captured key release state is released, the count value decreases by 1.
[0038] For example, the preset numerical range can be set according to actual needs. Generally, different preset numerical ranges can be set for pressing and lifting, and there is no specific limitation.
[0039] For example, a key-value code includes a key value and a key state. The key value corresponds to the key name, meaning that different keys have different corresponding key values. The key-value code can be obtained by encoding the key name and key state, and the specific encoding method is not limited.
[0040] For example, the preset key-value code queue can be implemented by a key-value code cache. Writing a key-value code can be referred to as storing in the cache, and reading a key-value code can be referred to as reading out of the cache. The embodiment of the present invention does not limit the storage space size of the preset key-value code queue, and it can be set according to actual conditions. The key-value codes can be written into the preset key-value code queue in chronological order so that the timing of key state changes can be accurately determined when the key is subsequently read.
[0041] Step 103: Obtain key-value codes from the preset key-value code queue in sequence, and analyze the obtained key-value codes to obtain a key recognition result.
[0042] In an embodiment of the present invention, the key-value code is stored in a preset key-value code queue. Even if the user program fails to process it in time, the key status will not be lost and key recognition will not be missed. This allows the user program to perform key recognition in a non-blocking manner, thereby improving the operating efficiency of the device.
[0043] For example, key-value codes can be sequentially obtained from a preset key-value code queue. For example, one or more key-value codes can be read from the preset key-value code queue in chronological order of their storage, and the obtained key-value codes can be analyzed to obtain a key recognition result. The specific analysis process is not limited. Generally, logical operations and / or judgments can be performed on the key-value codes read historically and the key-value codes currently obtained to obtain a final key recognition result.
[0044] The key recognition method provided in the embodiment of the present invention adopts a preset frequency to periodically collect the key state of the key, wherein the key state includes pressing and popping up; the count value of the state counter of the corresponding key is updated according to the collected key state, and when the count value is within the preset value range, the key value code of the corresponding key is written into the preset key value code queue, wherein the key value code includes the key value and the key state, and the key value corresponds to the name of the key; the key value code is sequentially obtained from the preset key value code queue, and the obtained key value code is analyzed to obtain the key recognition result. By adopting the above technical solution, the key state of the key is sampled and counted multiple times, and the key value code that meets the count value requirement is subsequently processed, which can effectively realize key anti-shake, and the key value code that meets the count value requirement is stored in the queue, waiting for subsequent reading and analysis, which can effectively realize key leakage prevention, and finally realize accurate key recognition, improve key response efficiency and accuracy, and improve human-computer interaction effect.
[0045] In some embodiments, updating the count value of the corresponding key's state counter based on the collected key state and writing the key code of the corresponding key into a preset key code queue when the count value is within a preset range includes: if the currently collected key state is pressed, if the current count value of the corresponding key's state counter is less than a preset number of anti-shake cycles, then incrementing the current count value by 1; if the currently collected key state is released, if the current count value of the corresponding key's state counter is less than the preset number of anti-shake cycles, then decrementing the current count value by 1. This configuration provides accurate statistics on key presses and releases, effectively implementing key anti-shake. The preset number of anti-shake cycles can be set based on actual conditions. For example, if the duration from a user pressing a key to releasing it is 200 milliseconds, the preset frequency can be set to 30 milliseconds per cycle, and the preset number of anti-shake cycles can be set to 6. If jitter occurs at 100 milliseconds, the count value cannot reach 6 and the key code will not be entered into the preset key code queue, meaning it will not be recognized, thus achieving anti-shake.
[0046] Furthermore, after the current count value is increased by 1, if the count value is equal to the preset anti-shake cycle number, the key value code of the corresponding key state as pressed is written into the preset key value code queue; after the current count value is reduced by 1, if the count value is equal to 0, the key value code of the corresponding key state as bounced is written into the preset key value code queue. The advantage of this setting is that the key value code can be written into the preset key value code queue in a timely and accurate manner, which facilitates more efficient key analysis. As in the above example, when the jitter disappears, the count value continues to be updated, and after reaching 6, it is put into the preset key value code queue to ensure that this key operation is only recognized once.
[0047] In some embodiments, if the key state is collected sequentially for a single key, optionally, when the currently collected key state is pressed, if the current count value of the state counter of the corresponding key is greater than or equal to the preset anti-shake cycle number, the key state of the next key is collected; when the currently collected key state is popped up, if the current count value of the state counter of the corresponding key is greater than or equal to the preset anti-shake cycle number, the key state of the next key is collected.
[0048] Optionally, after adding 1 to the current count value, if the count value is not equal to the preset anti-shake cycle number, the key state of the next key is collected; after subtracting 1 from the current count value, if the count value is not equal to 0, the key state of the next key is collected.
[0049] In some embodiments, the key-value code of the corresponding button is written into the preset key-value code queue, including: judging whether there is remaining space in the preset key-value code queue, if so, writing the key-value code of the corresponding button into the current storage position in the preset key-value code queue, and adding 1 to the current storage position; if the current storage position overflows, setting the current storage position to 0. Correspondingly, the key-value code is obtained from the preset key-value code queue in sequence, including: judging whether there is an unread key-value code in the preset key-value code queue, if so, taking out the key-value code corresponding to the current read position, and adding 1 to the current read position; if the current read position overflows, setting the current read position to 0. The advantage of this setting is that, through the above-mentioned overflow processing method, the storage space of the preset key-value code queue can be saved, and circular storage and reading can be realized. Among them, the current storage position can be understood as a pointer, pointing to the position where the key-value code can be stored at present; the current read position can also be understood as a pointer, pointing to the position where the key-value code can be read at present. Optionally, whether there is remaining space can be determined based on the current storage position, the current read position and the storage space size of the preset key-value code queue, for example, based on whether the difference between the current storage position and the current read position is greater than the storage space size. If it is greater, it does not exist; if it is less than or equal to, it exists.
[0050] In some embodiments, the key-value code is encoded using bits, with one bit used to represent the key state and the remaining bits used to represent the key value, and each of the remaining bits corresponds to a key. For example, the "pressed" and "released" states of each key are represented by a number, with the highest bits 1 and 0 of the number representing "pressed" and "released" respectively, and each of the remaining bits representing a key. When the number of keys is less than 8, a 1-byte number is used for representation; when the number of keys is less than 16, a 2-byte number is used; when the number of keys is less than 32, a 4-byte number is used, and so on. A 1-byte key-value encoding such as: 0x81 represents key #1 pressed, 0x01 represents key #1 released; 0x82 represents key #2 pressed, 0x02 represents key #2 released; 0x84 represents key #3 pressed, 0x04 represents key #3 released; 0x88 represents key #4 pressed, 0x08 represents key #4 released; 0x90 represents key #5 pressed, 0x10 represents key #5 released; and so on.
[0051] In some embodiments, the key-value code obtained is analyzed to obtain a key recognition result, including: performing a logical operation based on the currently obtained key value, the current key value and the last key value, and determining the key recognition result based on the operation result, wherein the currently obtained key value is the key value in the currently obtained key-value code, and the current key value is obtained by performing a bitwise OR operation on the last key value and the currently obtained key value. If the currently obtained key value is the first key value obtained, then the last key value is the same as the currently obtained key value or is 0. If the currently obtained key value is not the first key value obtained, then the last key value is the same as the new current key value calculated after the last key value was obtained. The advantage of this setting is that it takes into account the timing of the key state change, dynamically analyzes the key value change, and obtains an accurate and effective key analysis result. Among them, when determining the key recognition result based on the operation result, it is generally necessary to refer to the key state in the currently obtained key-value code, that is, to determine the key recognition result based on the operation result and the key state in the currently obtained key-value code.
[0052] In some embodiments, at least two keys are allowed to be pressed simultaneously; the logic operation is performed based on the currently acquired key value, the current key value and the last key value, and the key recognition result is determined based on the operation result, including: if the key value in the currently acquired key value code is valid, judging whether the current key value is greater than the last key value, if so, recording the current key value as the last key value, if not, the current key value remains unchanged; judging whether the key state in the currently acquired key value code is bounced, if it is bounced, performing a bitwise AND operation on the current key value and the currently acquired key value after bitwise inversion to obtain a new current key value; if the new current key value is not equal to the last key value, and the new current key value is 0, then the last key value is determined to be a valid key value, and the valid key value is read by the user program and a corresponding key response is performed after the reading is successful. The advantage of this setting is that it takes into account the situation of combined keys, that is, allowing at least two keys to be pressed and lifted at the same time, thereby realizing richer key operations. For the situation of combined keys, the above analysis method can accurately analyze the valid key value, that is, accurately identify the user's true key operation intention.
[0053] The validity of the key value in the currently obtained key-value code can be understood as successfully obtaining the key value in the key-value code. If the key value is 0 or empty, it means that it has not been successfully obtained and the key value is invalid.
[0054] For key combinations, the current key value is obtained by performing a bitwise OR operation on the last key value and the currently acquired key value. For example, if the last key value is 001 and the currently acquired key value is 010, the OR operation will result in 011. This means that the current key value is greater than the last key value, indicating that two keys may have been pressed simultaneously. In this case, the current key value can be temporarily recorded as the last key value to be used later to determine whether the current key value has been cleared, thereby determining whether the last key value is a valid key value.
[0055] In some embodiments, after determining whether the key state in the currently acquired key value is a pop-up, the method further includes: if not, performing a bitwise OR operation on the current key value and the currently acquired key value to obtain a new current key value. If the key state is not a pop-up, that is, the key state is pressed, a bitwise OR operation is required before performing subsequent determination.
[0056] In some embodiments, after obtaining the new current key value, the method further includes: if the new current key value is equal to the previous key value, or the new current key value is not 0, continuing to obtain the next key value code from the preset key value code queue.
[0057] Figure 2 A flowchart of another key recognition method provided by an embodiment of the present invention includes the following steps:
[0058] Step 201: periodically collect the key status of each key in sequence using a preset frequency.
[0059] The button status includes pressed and released.
[0060] Step 202: When the currently acquired key state is pressed, if the current count value of the state counter of the corresponding key is less than the preset anti-shake cycle number, the current count value is increased by 1; when the currently acquired key state is popped up, if the current count value of the state counter of the corresponding key is less than the preset anti-shake cycle number, the current count value is reduced by 1.
[0061] Step 203: After adding 1 to the current count value, if the count value is equal to the preset anti-shake cycle number, write the key-value code indicating that the key state of the corresponding key is pressed into the preset key-value code queue; after subtracting 1 from the current count value, if the count value is equal to 0, write the key-value code indicating that the key state of the corresponding key is popped up into the preset key-value code queue.
[0062] The key-value code includes the key value and key status. The key value corresponds to the key name. The key-value code is encoded using bits, with one bit representing the key status and the remaining bits representing the key value. Each of the remaining bits corresponds to a key.
[0063] Step 204: Obtain key-value codes from the preset key-value code queue in sequence, perform logical operations based on the currently obtained key value, the current key value, and the previous key value, and determine the key recognition result based on the operation result and the key state in the currently obtained key-value code.
[0064] The key recognition method provided by the embodiment of the present invention periodically samples and counts the key status of each key, and subsequently processes the key-value codes that meet the counting value requirements, which can effectively achieve key anti-shake, and stores the key-value codes that meet the counting value requirements in a queue, and reads the key-value codes from the queue in sequence, which can effectively achieve key leakage prevention, and performs logical operations based on the currently obtained key value, the current key value and the last key value, and determines the key recognition result based on the operation result and the key status in the currently obtained key-value code, effectively improving the key response efficiency and accuracy, and improving the human-computer interaction effect.
[0065] Figure 3 A schematic diagram of the functional modules of a key recognition method provided by an embodiment of the present invention is shown in FIG. Figure 3 The three functional modules shown include key value acquisition, key value access and key value analysis. The key value acquisition functional module can implement regular scanning of the key level status and output the changed key value. The key value access functional module can implement a key value code cache library, providing an entry and exit interface for key value access. The key value analysis functional module can implement a comprehensive analysis based on one or more groups of key values and key states to obtain a valid key value. The key value acquisition functional module is connected to the key value access module, and the key value acquisition functional module stores the obtained key value code into the key value code cache library through the entry interface provided by the key value access module. The key value access functional module is connected to the key value analysis functional module, and the key value analysis functional module takes out the key value code through the exit interface of the key value access functional module, and obtains the valid key value through a comprehensive analysis of the key value and key state.
[0066] The entire process of the key recognition method is divided into three parts and further introduced in detail below.
[0067] Figure 4 A schematic diagram of a key value acquisition process provided by an embodiment of the present invention is as follows Figure 4 As shown in the figure, in the key value acquisition function module, an independent loop schedule periodically scans multiple key level states, and outputs the key value codes of "pressed" and "released" according to the cumulative number of cycles of the continuous state. The specific implementation process is as follows:
[0068] Step 401: Key-value encoding, setting the number of keys K and the number of preset anti-shake cycles N.
[0069] The key-value encoding process can be found above and will not be further described here. The key count K (K > 0) can be understood as the preset maximum number of keys, that is, the total number of keys that require key recognition, typically all keys on the same key panel in the device. The preset anti-shake cycle count N (N > 1) can also be referred to as the maximum anti-shake cycle count.
[0070] Step 402: Schedule a delay wait of T milliseconds.
[0071] The process of obtaining the key status is an independent repetitive cycle. After one cycle is completed, it waits for T (T>0) milliseconds (ms) before executing the next cycle. The delay T can be set according to the actual anti-shake time requirements and can be understood as the inverse of the preset frequency.
[0072] Step 403: Assign an initial value of 1 to the key code k.
[0073] Starting from the first key and ending at the Kth key, the key status of each key is acquired in a loop, and then the corresponding key value code is obtained. That is, the key status is scanned in sequence starting from the first key every T milliseconds.
[0074] Step 404: Get the status of key k.
[0075] Step 405 , determine whether the state of key k is pressed. If so, execute step 406 ; otherwise, execute step 410 .
[0076] Step 406 , determine whether the k-key pressing state counter c is less than N. If so, execute step 407 ; otherwise, execute step 414 .
[0077] Step 407: c is incremented by 1, c=c+1.
[0078] Step 408 , determine whether c is equal to N. If so, execute step 409 ; otherwise, execute step 414 .
[0079] Step 409: Press the key-value code on the k-key to store it in the database.
[0080] By executing this step, the key-value code of the "pressed" state of key k can be successfully obtained, and the storage interface in the key access function module is called to store the key-value code in the key-value code cache.
[0081] Step 410 , determine whether c is less than N. If so, execute step 411 ; otherwise, execute step 414 .
[0082] Step 411: c is decremented by 1, c=c-1.
[0083] Step 412: Determine whether c is equal to 0. If so, execute step 413; otherwise, execute step 414.
[0084] Step 413: The key-value code of the pop-up button k is stored in the database.
[0085] By executing this step, the key-value code of the "pop-up" state of key k can be successfully obtained, and the storage interface in the key access function module is called to store the key-value code in the key-value code cache.
[0086] Step 414: The key code k is incremented by 1, k=k+1.
[0087] Step 415: Determine whether k is greater than K. If so, execute step 402; otherwise, execute step 404.
[0088] By adopting the above process, valid key-value codes can be accurately stored in the database, preparing for the subsequent accurate analysis of valid key values.
[0089] Figure 5 A schematic diagram of a preset key-value code queue provided by an embodiment of the present invention, Figure 6 A schematic diagram of a key value storage process provided by an embodiment of the present invention, Figure 7 A schematic diagram of a key value reading process provided by an embodiment of the present invention, referring to Figure 5 、 Figure 6 and Figure 7 , the key-value access function module includes the key-value code cache information definition, key-value code entry process and key-value code exit process.
[0090] The preset key-value code queue, also known as the key-value code cache library, can be recorded as a cache (BUFFER). The key-value code cache library information includes the space size S, the position starting from 0 and ending at S-1, the storage position H, and the reading position T. Each space temporarily stores key-value code data.
[0091] refer to Figure 6 , BUFFER is implemented by the storage interface, and the key-value code storage implementation process is as follows:
[0092] Step 601: Determine whether there is space in the cache. If so, execute step 602; otherwise, end the process.
[0093] You can use the following expression to determine whether there is space:
[0094] ((H>=T)&&((HT)>=S))||((H<T)&&((S+H-T)> =S))
[0095] If the above expression is satisfied, it is considered that there is space; if not, it is considered that there is no space.
[0096] Step 602: H position cache fills in key value code E.
[0097] The key-value code E is filled in the space corresponding to the storage location H in the key-value code cache, which can be recorded as BUFFER[H]=E.
[0098] Step 603: The H position is incremented by 1.
[0099] It can be expressed as H=H+1.
[0100] Step 604: Determine whether the H position overflows. If so, execute step 605; otherwise, end the process.
[0101] For example, whether the storage location H overflows is determined by determining whether the storage location H is larger than the cache space size S, that is, when H>=S, step 605 is executed.
[0102] Step 605: Set the H position to 0.
[0103] It can be recorded as H=0, that is, the key-value code is stored from the beginning space of the cache.
[0104] refer to Figure 7 , reading the key-value code from the BUFFER is implemented by the outbound interface. The key-value outbound implementation process is as follows:
[0105] Step 701: Determine whether there is new data in the cache. If so, execute step 702; otherwise, end the process.
[0106] For example, whether there is new data in the key-value code cache (which can be understood as unread key-value code) can be determined by whether the storage position H is equal to the read position T, that is, whether H == T is determined. If so, step 702 is performed to retrieve the key-value code. Otherwise, the outbound process is terminated and the empty key value is retrieved.
[0107] Step 702: Take out the cache key code E at position T.
[0108] Step 703: The T position is incremented by 1.
[0109] Step 704: Determine whether the T position overflows. If so, execute step 705; otherwise, end the process.
[0110] Exemplarily, whether the read position T overflows is determined by checking whether the read position T is larger than the cache space size S. If so, step S205 is performed; otherwise, the outbound process is terminated.
[0111] Step 705: Set the T position to 0.
[0112] Exemplarily, the read position T is set to 0, and the key value is read from the beginning space of the cache library.
[0113] Figure 8 A schematic diagram of a key value analysis process provided by an embodiment of the present invention, referring to Figure 8 In the key value analysis function module, the key value code is retrieved through the key value access function module outbound interface for comprehensive analysis. The key value "bitwise OR", "bitwise inversion" and "bitwise AND" operations are combined to output the valid key value. The current key value C and the previous key value L are preset in the entire comprehensive analysis process. The key value analysis process is as follows:
[0114] Step 801: Initialize the valid key value Q to be returned to be a null value.
[0115] That is, initialize Q=0, which is used to return a null value 0 when the process ends abnormally.
[0116] Step 802: The key-value code is taken out of the library, and the key value R and status M are taken out.
[0117] The key-value code is obtained through the outbound interface in the key-value access function module. The key-value code includes information such as the key value R and the status M. R and M are taken out from the key-value code.
[0118] Step 803: Determine whether the key value R is valid. If so, execute step 804; otherwise, end the process.
[0119] For example, whether the key value is valid can be determined by whether the key value R is not equal to 0, that is, whether R!=0 is satisfied. If so, it is considered valid and step 804 is continued. Otherwise, the entire process ends and an empty key value 0 is returned, that is, Q=0.
[0120] Step 804 , determine whether C is greater than L. If so, execute step 805 ; otherwise, execute step 806 .
[0121] Exemplarily, determine whether the current key value C is greater than the previous key value L. If so, proceed to step 805 to record the current key value as the previous key value. Otherwise, skip step 805 and proceed directly to step 806 to determine the key value status.
[0122] Step 805: Record C as L.
[0123] Step 806 , determine whether M is greater than 0. If so, execute step 807 ; otherwise, execute step 808 .
[0124] For example, if C is not greater than L, C can remain unchanged. Whether the key state M is "pressed" can be determined by whether M is greater than 0. If it is "pressed", step 807 is performed, otherwise it is "up", step 808 is performed.
[0125] Step 807: C is bitwise ORed with R to obtain a new C.
[0126] Exemplarily, the current key value C is bitwise ORed with the key value R, and the result is assigned to the current key value C, C=C|R, which is equivalent to bitwise ORing the current key value and the currently acquired key value R, and using the result as the new current key value described above.
[0127] Step 808: C is bitwise ANDed with the inverted value of R to obtain a new C.
[0128] For example, the key value R is first bitwise "inverted", then bitwise "AND"ed with the current key value C, and the result is assigned to the current key value C, C = C&(~R), which is equivalent to performing a bitwise AND operation on the current key value and the currently acquired key value R after bitwise inversion, and using the result as the new current key value described above.
[0129] Step 809 , determine whether C and L are not equal. If so, execute step 810 ; otherwise, return to execute step 802 .
[0130] Exemplarily, it is determined whether the current key value C (ie, the new current key value) is not equal to the previous key value L, that is, whether C!=L is satisfied. If so, step 810 is performed; otherwise, step 802 is performed and the above process is repeated.
[0131] Step 810 , determine whether C is 0. If so, execute step 811 ; otherwise, return to execute step 802 .
[0132] Exemplarily, it is determined whether the current key value C (ie, the new current key value) is 0, that is, whether C == 0 is satisfied. If so, proceed to step 811; otherwise, proceed to step 802 and repeat the above process.
[0133] Step 811: Obtain the valid key value Q as L.
[0134] Exemplarily, the last key value L is the valid key value Q, the valid key value Q is obtained, the process ends and the valid key value Q is output for reading by the user program.
[0135] To facilitate understanding of the above key-value analysis process, let's take a simple example. Assume that a key-value code is represented by 4 bits. Assume that the key value corresponding to button 1 is 001. The key-value code for pressing button 1 is 1001, and the key-value code for releasing button 1 is 0001. When the user presses button 1 normally and then releases it, the key-value codes 1001 and 0001 can be obtained in sequence during the key-value acquisition phase and placed in the preset key-value code queue. They are then read in sequence during the key-value code extraction process. The specific details are not detailed here.
[0136] First, initialize the valid key value Q to 0 and take out the key code 1001. At this time, the key value R is 001, the state M is 1, and the key value R is valid. Since it is the first read, the last key value L is 0, the current key value C = L | R = 001, C is not greater than L, then the current key value is C = 001, M is greater than 0, the new current key value C = C | R = 001, C and L are not equal, C is not 0, so the next key code needs to be taken out. Among them, if it is assumed that the last key value L and R are the same during the first read, then C is equal to L, and the next key code needs to be taken out.
[0137] The next key value code is 0001. At this time, the key value R is 001, the state M is 0, and the key value R is valid. Since it is not the first reading, the last key value L is the new current key value 001 calculated after the last key value code 1010 was obtained, that is, L=001. The current key value C=L|R=001 this time, C is not greater than L, then the current key value is C=001, M is not greater than 0, the new current key value C=C&(~R)=000, C is not equal to L, C is 0, therefore, the last key value L=001 is taken as the valid key value, that is, Q=001, indicating that the pressing operation of key No. 1 has been successfully recognized, and the user program reads and responds to the pressing operation.
[0138] Figure 9 This is a structural block diagram of a key recognition device provided by an embodiment of the present invention. The device can be implemented by software and / or hardware, and can generally be integrated into a smart device. It can perform key recognition by executing a key recognition method. Figure 9 As shown, the device includes:
[0139] The key state acquisition module 901 is used to periodically acquire the key state of a key using a preset frequency, wherein the key state includes pressed and released;
[0140] The key-value code writing module 902 is configured to update the count value of the state counter of the corresponding key according to the collected key state, and write the key-value code of the corresponding key into a preset key-value code queue when the count value is within a preset value range, wherein the key-value code includes a key value and a key state, and the key value corresponds to the name of the key;
[0141] The key analysis module 903 is used to sequentially obtain key-value codes from the preset key-value code queue and analyze the obtained key-value codes to obtain a key recognition result.
[0142] The key recognition device provided in the embodiment of the present invention uses a preset frequency to periodically collect the key state of a key, wherein the key state includes pressing and popping up; the count value of the state counter of the corresponding key is updated according to the collected key state, and when the count value is within the preset value range, the key value code of the corresponding key is written into a preset key value code queue, wherein the key value code includes a key value and a key state, and the key value corresponds to the name of the key; the key value code is sequentially obtained from the preset key value code queue, and the obtained key value code is analyzed to obtain the key recognition result. By adopting the above technical solution, the key state of the key is sampled and counted multiple times, and the key value code that meets the count value requirement is subsequently processed, which can effectively realize key anti-shake, and the key value code that meets the count value requirement is stored in the queue, waiting for subsequent reading and analysis, which can effectively realize key leakage prevention, and finally realize accurate key recognition, improve key response efficiency and accuracy, and improve human-computer interaction effect.
[0143] Optionally, the key-value code writing module is specifically used to:
[0144] When the currently acquired key state is pressed, if the current count value of the state counter of the corresponding key is less than the preset anti-shake cycle number, the current count value is increased by 1; when the currently acquired key state is released, if the current count value of the state counter of the corresponding key is less than the preset anti-shake cycle number, the current count value is decreased by 1;
[0145] After adding 1 to the current count value, if the count value is equal to the preset anti-shake cycle number, the key value code indicating that the key state of the corresponding key is pressed is written into the preset key value code queue; after subtracting 1 from the current count value, if the count value is equal to 0, the key value code indicating that the key state of the corresponding key is popped up is written into the preset key value code queue.
[0146] Optionally, writing the key-value code of the corresponding button into a preset key-value code queue includes:
[0147] Determine whether there is any remaining space in the preset key-value code queue. If so, write the key-value code of the corresponding button into the current storage position in the preset key-value code queue and add 1 to the current storage position. If the current storage position overflows, set the current storage position to 0.
[0148] Accordingly, the step of sequentially obtaining key-value codes from the preset key-value code queue includes:
[0149] Determine whether there is an unread key-value code in the preset key-value code queue. If so, take out the key-value code corresponding to the current read position and add 1 to the current read position. If the current read position overflows, set the current read position to 0.
[0150] Optionally, the key-value code is encoded using bits, one bit is used to represent a key state, and the remaining bits are used to represent key values, and each of the remaining bits corresponds to a key;
[0151] The key value code is analyzed to obtain a key recognition result, including:
[0152] A logical operation is performed based on the currently acquired key value, the current key value and the last key value, and the key recognition result is determined based on the operation result, wherein the currently acquired key value is the key value in the currently acquired key value code, and the current key value is obtained by performing a bitwise OR operation on the last key value and the currently acquired key value. If the currently acquired key value is the first acquired key value, the last key value is the same as the currently acquired key value or is 0. If the currently acquired key value is not the first acquired key value, the last key value is the same as the new current key value calculated after the last key value was acquired.
[0153] Optionally, at least two keys are allowed to be pressed simultaneously; performing a logical operation based on the currently acquired key value, the current key value, and the previous key value, and determining a key recognition result based on the operation result, includes:
[0154] If the key value in the currently acquired key-value code is valid, determine whether the current key value is greater than the previous key value. If so, record the current key value as the previous key value; if not, leave the current key value unchanged.
[0155] Determine whether the key state in the currently acquired key value code is pop-up. If it is pop-up, perform a bitwise AND operation on the current key value and the currently acquired key value after bitwise inversion to obtain a new current key value.
[0156] If the new current key value is not equal to the last key value, and the new current key value is 0, the last key value is determined as a valid key value, which is read by the user program and performs a corresponding key response after successful reading.
[0157] Optionally, after determining whether the key state in the currently acquired key value is pop-up, the method further includes:
[0158] If it is not a pop-up, the current key value and the currently obtained key value are bitwise ORed to obtain a new current key value.
[0159] Optionally, the key analysis module is used to: after obtaining a new current key value, if the new current key value is equal to the previous key value, or the new current key value is not 0, continue to obtain the next key value code from the preset key value code queue.
[0160] An embodiment of the present invention provides a smart device, into which the key recognition device provided by the embodiment of the present invention can be integrated. Figure 10 This is a block diagram of a smart device according to an embodiment of the present invention. The smart device 1000 may include a memory 1001, a processor 1002, and a computer program stored in the memory 1001 and executable by the processor 1002. When the processor 1002 executes the computer program, the key recognition method according to an embodiment of the present invention is implemented.
[0161] An embodiment of the present invention further provides a storage medium containing computer-executable instructions. When the computer-executable instructions are executed by a computer processor, the computer-executable instructions are used to perform the key recognition method provided by any embodiment of the present invention. For example, the method may include:
[0162] Periodically collecting the key status of a key at a preset frequency, wherein the key status includes pressed and released;
[0163] updating the count value of the state counter of the corresponding key according to the collected key state, and writing the key-value code of the corresponding key into a preset key-value code queue when the count value is within a preset value range, wherein the key-value code includes a key value and a key state, and the key value corresponds to the name of the key;
[0164] The key-value codes are sequentially acquired from the preset key-value code queue, and the acquired key-value codes are analyzed to obtain a key recognition result.
[0165] Storage medium - any of various types of memory devices or storage devices. The term "storage medium" is intended to include: installation media, such as CD-ROMs, floppy disks, or tape drives; computer system memory or random access memory, such as DRAM, DDRRAM, SRAM, EDORAM, Rambus RAM, etc.; non-volatile memory, such as flash memory, magnetic media (such as hard disks or optical storage); registers or other similar types of memory elements, etc. Storage media may also include other types of memory or combinations thereof. In addition, the storage medium may be located in the first computer system in which the program is executed, or may be located in a different second computer system that is connected to the first computer system via a network (such as the Internet). The second computer system can provide program instructions to the first computer for execution. The term "storage medium" may include two or more storage media that can reside in different locations (e.g., in different computer systems connected via a network). The storage medium can store program instructions (e.g., embodied as a computer program) that can be executed by one or more processors.
[0166] Of course, the computer executable instructions of the storage medium provided by the embodiment of the present invention are not limited to the key recognition operations described above, but can also execute related operations in the key recognition method provided by any embodiment of the present invention.
[0167] The key recognition apparatus, device, and storage medium provided in the above embodiments can execute the key recognition method provided in any embodiment of the present invention, and have the corresponding functional modules and beneficial effects of executing the method. For technical details not fully described in the above embodiments, please refer to the key recognition method provided in any embodiment of the present invention.
[0168] Note that the above are only preferred embodiments of the present invention and the technical principles employed. Those skilled in the art will understand that the present invention is not limited to the specific embodiments described herein, and that various obvious changes, readjustments, and substitutions can be made by those skilled in the art without departing from the scope of protection of the present invention. Therefore, although the present invention has been described in detail through the above embodiments, the present invention is not limited to the above embodiments and may include many other equivalent embodiments without departing from the concept of the present invention. The scope of the present invention is determined by the scope of the appended claims.
Claims
1. A key recognition method, characterized in that: include: Periodically collecting the key status of a key at a preset frequency, wherein the key status includes pressed and released; updating the count value of the state counter of the corresponding key according to the collected key state, and writing the key-value code of the corresponding key into a preset key-value code queue when the count value is within a preset value range, wherein the key-value code includes a key value and a key state, and the key value corresponds to the name of the key; Obtain key-value codes from the preset key-value code queue in sequence, and analyze the obtained key-value codes to obtain a key recognition result; The key-value code is encoded using bits, one bit is used to represent the key state, and the remaining bits are used to represent the key value, and each of the remaining bits corresponds to a key; The key value code is analyzed to obtain a key recognition result, including: A logical operation is performed based on the currently acquired key value, the current key value and the last key value, and the key recognition result is determined based on the operation result, wherein the currently acquired key value is the key value in the currently acquired key value code, and the current key value is obtained by performing a bitwise OR operation on the last key value and the currently acquired key value. If the currently acquired key value is the first acquired key value, the last key value is the same as the currently acquired key value or is 0. If the currently acquired key value is not the first acquired key value, the last key value is the same as the new current key value calculated after the last key value was acquired. The key recognition result is specifically determined based on the operation result and the key status in the currently acquired key value code.
2. The method according to claim 1, characterized in that The updating of the count value of the state counter of the corresponding key according to the collected key state, and writing the key-value code of the corresponding key into the preset key-value code queue when the count value is within a preset value range, includes: When the currently acquired key state is pressed, if the current count value of the state counter of the corresponding key is less than the preset anti-shake cycle number, the current count value is increased by 1; when the currently acquired key state is released, if the current count value of the state counter of the corresponding key is less than the preset anti-shake cycle number, the current count value is decreased by 1; After adding 1 to the current count value, if the count value is equal to the preset anti-shake cycle number, the key value code indicating that the key state of the corresponding key is pressed is written into the preset key value code queue; after subtracting 1 from the current count value, if the count value is equal to 0, the key value code indicating that the key state of the corresponding key is popped up is written into the preset key value code queue.
3. The method according to claim 1, characterized in that Writing the key-value code of the corresponding button into the preset key-value code queue includes: Determine whether there is any remaining space in the preset key-value code queue. If so, write the key-value code of the corresponding button into the current storage position in the preset key-value code queue and add 1 to the current storage position. If the current storage position overflows, set the current storage position to 0. Accordingly, the step of sequentially obtaining key-value codes from the preset key-value code queue includes: Determine whether there is an unread key-value code in the preset key-value code queue. If so, take out the key-value code corresponding to the current read position and add 1 to the current read position. If the current read position overflows, set the current read position to 0.
4. The method according to claim 1, wherein At least two keys are allowed to be pressed simultaneously; performing a logical operation based on the currently acquired key value, the current key value, and the last key value, and determining a key recognition result based on the operation result, including: If the key value in the currently acquired key-value code is valid, determine whether the current key value is greater than the previous key value. If so, record the current key value as the previous key value; if not, leave the current key value unchanged. Determine whether the key state in the currently acquired key value code is pop-up. If it is pop-up, perform a bitwise AND operation on the current key value and the currently acquired key value after bitwise inversion to obtain a new current key value. If the new current key value is not equal to the last key value, and the new current key value is 0, the last key value is determined as a valid key value, which is read by the user program and performs a corresponding key response after successful reading.
5. The method according to claim 4, characterized in that After determining whether the key state in the currently acquired key value is a pop-up key, the method further includes: If it is not a pop-up, the current key value and the currently obtained key value are bitwise ORed to obtain a new current key value.
6. The method according to claim 4 or 5, characterized in that After getting the new current key value, it also includes: If the new current key value is equal to the previous key value, or the new current key value is not 0, then continue to obtain the next key value code from the preset key value code queue.
7. A key recognition device, characterized in that: include: A key state acquisition module, configured to periodically acquire the key state of a key at a preset frequency, wherein the key state includes pressed and released; A key-value code writing module is used to update the count value of the state counter of the corresponding key according to the collected key state, and when the count value is within a preset value range, write the key-value code of the corresponding key into a preset key-value code queue, wherein the key-value code includes a key value and a key state, and the key value corresponds to the name of the key; A key analysis module is used to sequentially obtain key-value codes from the preset key-value code queue and analyze the obtained key-value codes to obtain a key recognition result; The key-value code is encoded using bits, one bit is used to represent the key state, and the remaining bits are used to represent the key value, and each of the remaining bits corresponds to a key; The key value code is analyzed to obtain a key recognition result, including: A logical operation is performed based on the currently acquired key value, the current key value and the last key value, and the key recognition result is determined based on the operation result, wherein the currently acquired key value is the key value in the currently acquired key value code, and the current key value is obtained by performing a bitwise OR operation on the last key value and the currently acquired key value. If the currently acquired key value is the first acquired key value, the last key value is the same as the currently acquired key value or is 0. If the currently acquired key value is not the first acquired key value, the last key value is the same as the new current key value calculated after the last key value was acquired. The key recognition result is specifically determined based on the operation result and the key status in the currently acquired key value code.
8. An intelligent device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the method according to any one of claims 1 to 6 is implemented.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 6 is implemented.