Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom keyboard shortcuts

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.

like image 585
Krzysztof Miemiec Avatar asked Sep 03 '12 15:09

Krzysztof Miemiec


1 Answers

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.

like image 92
vArDo Avatar answered Sep 18 '22 04:09

vArDo