捕获两次物理按钮点击

发布于 2024-09-29 06:02:40 字数 80 浏览 5 评论 0原文

如何捕获任何物理按钮(包括光学按钮)的两次点击?

就像 QuickDesk 通过单击两次主页按钮所做的那样。

谢谢

How would I capture two clicks of any of the physical buttons (including the optical button)?

Something like what QuickDesk does with the two clicks of the Home button.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

臻嫒无言 2024-10-06 06:02:40

只是一个想法:
如果Android中没有API可以处理这个问题,那么你可以尝试推迟关于单/双/三等的结论。通过在覆盖的 hadrware 按钮处理程序中设置一个小的超时(例如大约 300 毫秒)来单击,同时计算对该特定按钮的调用次数,在计时器滴答时检查它,您就得到了它。


编辑:

这是我的一些想法。请稍微调整一下以获得更优化的性能。

private Timer mDoubleClickTimer;
private boolean possibleDoubleClick = false;

@Override
public boolean onKeyUp(int keyCode, KeyEvent event){
    if(keyCode==KeyEvent.KEYCODE_MENU){ //or whatever key you want to check for double-clicks
        if(mDoubleClickTimer!=null) {mDoubleClickTimer.cancel();}
        if(!possibleDoubleClick){
            possibleDoubleClick = true;
            mDoubleClickTimer = new Timer();
            mDoubleClickTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    //single click detected
                    //handle it here
                    possibleDoubleClick = false;
                }
            },300);
        }else{
            //double click detected
            //handle it here
            possibleDoubleClick = false;
        }
//... other key processing if you need it
return false;
}

Just an idea:
If there is no API already in Android to handle that then you could try deferring the conclusion about the single/double/triple/etc. click by setting a small timeout (say around ~300ms) in that overridden hadrware button handler and in the meantime count the number of calls made to that specific button, check it when the timer ticks and you've got it.


Edit:

Here is something from the top of my head. Do tweak it a bit for more optimized performace.

private Timer mDoubleClickTimer;
private boolean possibleDoubleClick = false;

@Override
public boolean onKeyUp(int keyCode, KeyEvent event){
    if(keyCode==KeyEvent.KEYCODE_MENU){ //or whatever key you want to check for double-clicks
        if(mDoubleClickTimer!=null) {mDoubleClickTimer.cancel();}
        if(!possibleDoubleClick){
            possibleDoubleClick = true;
            mDoubleClickTimer = new Timer();
            mDoubleClickTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    //single click detected
                    //handle it here
                    possibleDoubleClick = false;
                }
            },300);
        }else{
            //double click detected
            //handle it here
            possibleDoubleClick = false;
        }
//... other key processing if you need it
return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文