Custom keyboard shortcuts

Deadly 提交于 2019-12-09 17:40:55

问题


I want my app to support keyboard shortcuts. Many devices, such as Asus Transformer have external keyboard which has Ctrl key (available on API Level 11). I've made some code, to check if Ctrl key works in emulator:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if(Build.VERSION.SDK_INT>10 &&
        event.getAction()==KeyEvent.ACTION_DOWN &&
        event.isCtrlPressed()){
    String actionType="NONE";
    final int keyCode = event.getKeyCode();
    switch(keyCode){
    case KeyEvent.KEYCODE_C:
        actionType = "COPY";
        break;
    case KeyEvent.KEYCODE_V:
        actionType = "PASTE";
        break;
    case KeyEvent.KEYCODE_X:
        actionType = "CUT";
        break;
    case KeyEvent.KEYCODE_R:
        actionType = "REFRESH";
        break;
    case KeyEvent.KEYCODE_A:
        actionType = "SELECT ALL";
        break;
    }
    Toast.makeText(ctx, actionType, Toast.LENGTH_SHORT).show();
    return true;
    }
    return super.dispatchKeyEvent(event);
}

Unfortunately it doesn't work in emulator and I can't check it on real hardware.

I have two questions:
1. Would it work on device like Asus Transformer?
2. Why Ctrl key does not work in emulator?

EDIT: I tried to find an answer or solution to my problem, but neither Google nor SO helped.


回答1:


1. Would it work on device like Asus Transformer?

I've checked on my Asus Transformer TF101 with physical keyboard (standard docking station from Asus) and your code works fine. I think you can use it without problems.

2. Why CTRL key does not work in emulator?

Looking at the list of buttons that can be mapped (for example using emulator -keyset) I don't think that current version of emulator supports direct Ctrl key mapping, so you're out of luck here.



来源:https://stackoverflow.com/questions/12250563/custom-keyboard-shortcuts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!