Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the long press event of a physical key

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to realize the long press event of the physical key". In the daily operation, I believe that many people have doubts about how to realize the long press event of the physical key. The editor consulted all kinds of materials and sorted out the simple and useful operation method. I hope it will be helpful for you to answer the doubt of "how to realize the long press event of physical keys"! Next, please follow the editor to study!

The core implementation code is as follows:

Some students may say: is there any problem with this realization? It looks reasonable, and it's no problem to use it.

Right! From a functional implementation point of view, this is the simplest, the least amount of code, and can be used immediately. However, you also need to realize that OnButtonPressed () and OnButtonReleased () are called in the interrupt context! Therefore, these two functions must return quickly and do not do too much work. But as a framework, these two interrupt functions must call the corresponding callback function. In this case, it requires that the incoming callback function must be short and not too tedious! Therefore, the implementation deficiency of V0.0.1 version highlights: when the key is pressed, the processing that needs to be done must be simple enough. If it is more complex and tedious, it may cause the system interrupt response not to be timely, and then cause the system PANIC to restart!

So is there any way to solve this problem?

Of course there are ways! Since the problem of version V0.0.1 is related to the interrupt response, we can learn from the principle and scheme in the Linux kernel: the interrupt service program is executed as soon as possible, and the interrupt processing is divided into two parts: the upper part of the interrupt processing and the lower part of the interrupt processing.

Explanation:

After the interrupt occurs, you need to call the interrupt service program to handle the interrupt immediately. If you do this directly in the interrupt service program, then the processing speed must be as fast as possible!

However, there must be some situations where the processing steps are time-consuming and complex, in which case, the processing work to be done can be marked first (interrupt the top half). The specific processing steps are completed after the interrupt service program returns (interrupt the lower half).

Does it feel too abstract to understand?! It doesn't matter, we will explain this scheme to you through the design and implementation of version V0.0.2.

The example in the figure above is the core implementation of DTButton-V0.0.2, which should feel simple now. Hey! Because it is a general framework, the final code implementation has a lot to consider and looks more complex than the example in the figure above.

Now, this framework is a great improvement over the previous version, and also has a dedicated event handling thread! It is precisely because the creation of this thread, so can not be wasted ah, we must make more use of it.

So what else can you do with it?

Think about it, now you can capture the button press event and release event!

So, if the time interval between pressing the event and releasing the event is long, can we "create" a completely new long-press event?

The answer is yes!

After pressing the event, you can make a rough timing through the event processing thread. When the timing exceeds the preset time interval (about 2.5 seconds), the long press event can be triggered, as shown in the callback function call of the long press event.

It is important to note here:

1. The long press event will only be triggered once, that is, the OnButtonLongPressed () callback function will only be called once.

two。 When the release event is triggered, the timing needs to be stopped, that is, if the interval between pressing the event and releasing the event is less than 2.5 seconds, the long press event will not be triggered.

With the above analysis, you can try to do it yourself.

What? Think it's troublesome?

Nothing! I have uploaded the implementation code to the attachment at the end of the article, you can download, use and research, your comments and suggestions are very welcome.

Examples of use are as follows:

1. Define a callback function for keystroke events

two。 Register with one button and use it right out of the box

The running results are as follows:

At this point, the study on "how to realize the long pressing event of physical keys" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report