Custom keyboard shortcuts
Asked Answered
A

1

7

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.

Alcala answered 3/9, 2012 at 15:23 Comment(2)
there is no CTRL key on most devices (at least not on most soft keyboards)Sokoto
But what about hardware keyboards with CTRL key?Alcala
K
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.

Knop answered 2/11, 2012 at 20:38 Comment(1)
Thanks for the reply! That's good news. I think that it's a good idea to add option to change the modifier key in order to provide support for everyone who has keyboard for Android device.Alcala

© 2022 - 2024 — McMap. All rights reserved.