在模拟器中检测到Space Key,但是当我在设备上运行我的应用程序时,我无法检测到相同的事件

时间:2014-09-24 07:21:17

标签: android keyevent

以下是我用来获取键事件的方法,这些方法在模拟器上运行正常,但似乎无法在设备上运行。

以下是我用来获取键事件的方法,这些方法在模拟器上运行正常,但似乎无法在设备上运行。

edTest.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if(keyCode == KeyEvent.KEYCODE_SPACE)
            {
                Log.e("keylistner", "keylistner");
            }
            return false;
        }
    });
    edTest.setKeyListener(new KeyListener() {

        @Override
        public boolean onKeyUp(View view, Editable text, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if(keyCode == KeyEvent.KEYCODE_SPACE)
            {
                Log.e("keylistner", "keylistner");
            }
            return false;
        }

        @Override
        public boolean onKeyOther(View view, Editable text, KeyEvent event) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean onKeyDown(View view, Editable text, int keyCode,
                KeyEvent event) {
            // TODO Auto-generated method stub
            if(keyCode == KeyEvent.KEYCODE_SPACE)
            {
                Log.e("keylistner", "keylistner");
            }
            return false;
        }

        @Override
        public int getInputType() {
            // TODO Auto-generated method stub
            return 1;
        }

        @Override
        public void clearMetaKeyState(View view, Editable content, int states) {
            // TODO Auto-generated method stub

        }
    });
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    if(keyCode == KeyEvent.KEYCODE_SPACE)
    {
        Log.e("onkeydown", "onkeydown");
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    // TODO Auto-generated method stub
    if(event.getKeyCode() ==KeyEvent.KEYCODE_SPACE)
    {
        Log.e("dispatch", "dispatch");
    }
    return super.dispatchKeyEvent(event);
}



@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    if(event.getKeyCode() ==KeyEvent.KEYCODE_SPACE)
    {
        Log.e("onkeyup", "onkeyup");
    }
    return super.onKeyUp(keyCode, event);
}

0 个答案:

没有答案