问题
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