自定义键盘快捷键

时间:2023-02-16
本文介绍了自定义键盘快捷键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我希望我的应用支持键盘快捷键.许多设备,例如 Asus Transformer 都有带有 Ctrl 键的外部键盘(在 API 级别 11 上可用).我编写了一些代码,以检查 Ctrl 键是否在模拟器中有效:

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.

我有两个问题:
1. 它可以在像 Asus Transformer 这样的设备上运行吗?
2.为什么Ctrl键在模拟器中不起作用?

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

我试图找到问题的答案或解决方案,但 Google 和 SO 都没有帮助.

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

推荐答案

1.它可以在像 Asus Transformer 这样的设备上运行吗?

我用物理键盘(Asus 的标准扩展坞)检查了我的 Asus Transformer TF101,并且您的代码运行良好.我认为您可以毫无问题地使用它.

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.为什么 CTRL 键在模拟器中不起作用?

查看可以映射的按钮列表(对于使用 emulator -keyset 的示例)我认为当前版本的模拟器不支持直接 Ctrl 键映射,所以你在这里不走运.

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.

这篇关于自定义键盘快捷键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何覆盖 &lt;ENTER&gt;Android中虚拟键盘的按键行为 下一篇:Iphone Development - 如何显示数字键盘?

相关文章

最新文章