关于Android按键处理

在dispatchKeyEvent(KeyEvent event)中按键的按下抬起都会进行操作,在处理完一个按键操作时无论return true 还是 false ,onKeyDown(int keyCode, KeyEvent event)和onKeyUp(int keyCode, KeyEvent event)都不会再进行按键处理:

1、第一种情况及log信息:

public void DEBUG(String str) {
        Log.d(TAG, str);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        DEBUG(" onKeyDown " + keyCode );
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        DEBUG("  onKeyLongPress " + keyCode );
        return super.onKeyLongPress(keyCode, event);
    }
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        DEBUG("  dispatchKeyEvent 1 key = " + event.getKeyCode() + " event =  " + event.getAction());
        if(event.getAction()==KeyEvent.ACTION_DOWN){
            DEBUG("  dispatchKeyEvent 2 ACTION_DOWN" + event.getKeyCode() );
            return super.dispatchKeyEvent(event);
        }
        if(event.getAction()==KeyEvent.ACTION_UP){
            DEBUG("  dispatchKeyEvent 3 ACTION_UP" + event.getKeyCode() );
            return super.dispatchKeyEvent(event);
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        DEBUG(" onKeyUp " +  keyCode);
        return super.onKeyUp(keyCode, event);
    }

log信息:
D/ testMainActivity (  853):   dispatchKeyEvent 1 key = 23 event =  0
D/ testMainActivity (  853):   dispatchKeyEvent 2 ACTION_DOWN23
D/ testMainActivity (  853):  onKeyDown 23
D/ testMainActivity (  853):   dispatchKeyEvent 1 key = 23 event =  1
D/ testMainActivity (  853):   dispatchKeyEvent 3 ACTION_UP23
D/ testMainActivity (  853):  onKeyUp 23

2、第二种情况及log信息:

public void DEBUG(String str) {
        Log.d(TAG, str);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        DEBUG(" onKeyDown " + keyCode );
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        DEBUG("  onKeyLongPress " + keyCode );
        return super.onKeyLongPress(keyCode, event);
    }
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        DEBUG("  dispatchKeyEvent 1 key = " + event.getKeyCode() + " event =  " + event.getAction());
        if(event.getAction()==KeyEvent.ACTION_DOWN){
            DEBUG("  dispatchKeyEvent 2 ACTION_DOWN" + event.getKeyCode() );
            return true;
        }
        if(event.getAction()==KeyEvent.ACTION_UP){
            DEBUG("  dispatchKeyEvent 3 ACTION_UP" + event.getKeyCode() );
            return false;
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        DEBUG(" onKeyUp " +  keyCode);
        return super.onKeyUp(keyCode, event);
    }













log信息:
D/ testMainActivity (  711):   dispatchKeyEvent 1 key = 23 event =  0
D/ testMainActivity (  711):   dispatchKeyEvent 2 ACTION_DOWN23
D/ testMainActivity (  711):   dispatchKeyEvent 1 key = 23 event =  1
D/ testMainActivity (  711):   dispatchKeyEvent 3 ACTION_UP23

综上所述:Android系统按键操作最先是在dispatchKeyEvent中进行处理的,然后才是onKeyDown(int keyCode, KeyEvent event)和onKeyUp(int keyCode, KeyEvent event)。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值