开发者

Capture two physical button clicks

开发者 https://www.devze.com 2023-01-22 17:05 出处:网络
How would I capture two clicks of any of the physical buttons (including the optical button)? Something like what QuickDe开发者_如何学Csk does with the two clicks of the Home button.

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

Something like what QuickDe开发者_如何学Csk does with the two clicks of the Home button.

Thanks


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号