您的位置:

Android Keycode详解

一、什么是Android Keycode

Android Keycode(按键代码)是一种描述在安卓设备上触摸屏幕或按下物理按键的操作。在 Android 中, KeyEvent 就是用来描述按键操作的类。KeyEvent 类会保存一个按键动作(如:ACTION_DOWN, ACTION_MULTIPLE, ACTION_UP)和按键码(键值)。

Android定义了一个整型变量叫作KeyCode。按下每个不同的键都会产生不同的KeyCode,KeyCode可以用于定义哪些按键是用于OSD控制按钮、哪些按键是用于输入文本或者控制系统的快捷键。

二、常用的Android Keycode

1. 常用字母数字键

/**
 * 0键 - 9键 (数字键盘)
 */
public static final int KEYCODE_0             = 7;
public static final int KEYCODE_1             = 8;
public static final int KEYCODE_2             = 9;
public static final int KEYCODE_3             = 10;
public static final int KEYCODE_4             = 11;
public static final int KEYCODE_5             = 12;
public static final int KEYCODE_6             = 13;
public static final int KEYCODE_7             = 14;
public static final int KEYCODE_8             = 15;
public static final int KEYCODE_9             = 16;

/**
 * A键 - Z键 (字母键盘)
 */
public static final int KEYCODE_A             = 29;
public static final int KEYCODE_B             = 30;
public static final int KEYCODE_C             = 31;
public static final int KEYCODE_D             = 32;
public static final int KEYCODE_E             = 33;
public static final int KEYCODE_F             = 34;
public static final int KEYCODE_G             = 35;
public static final int KEYCODE_H             = 36;
public static final int KEYCODE_I             = 37;
public static final int KEYCODE_J             = 38;
public static final int KEYCODE_K             = 39;
public static final int KEYCODE_L             = 40;
public static final int KEYCODE_M             = 41;
public static final int KEYCODE_N             = 42;
public static final int KEYCODE_O             = 43;
public static final int KEYCODE_P             = 44;
public static final int KEYCODE_Q             = 45;
public static final int KEYCODE_R             = 46;
public static final int KEYCODE_S             = 47;
public static final int KEYCODE_T             = 48;
public static final int KEYCODE_U             = 49;
public static final int KEYCODE_V             = 50;
public static final int KEYCODE_W             = 51;
public static final int KEYCODE_X             = 52;
public static final int KEYCODE_Y             = 53;
public static final int KEYCODE_Z             = 54;

2. 功能键

功能键位于键盘的左下角,主要控制输入的操作,比如回车、删除等。

public static final int KEYCODE_ENTER           = 66; //回车
public static final int KEYCODE_DEL             = 67; //删除
public static final int KEYCODE_TAB             = 61; //Tab

/**
 * 方向键
 */
public static final int KEYCODE_DPAD_UP         = 19; // 上箭头
public static final int KEYCODE_DPAD_DOWN       = 20; // 下箭头
public static final int KEYCODE_DPAD_LEFT       = 21; // 左箭头
public static final int KEYCODE_DPAD_RIGHT      = 22; // 右箭头

3. 控制键

如Home按钮、Menu按钮、返回按钮、音量键、亮度键等控制设备的按键。

/**
 * 控制键
 */
public static final int KEYCODE_BACK            = 4;  //返回键(左下角)
public static final int KEYCODE_HOME            = 3;  //Home键(中间大圆钮)
public static final int KEYCODE_APP_SWITCH      = 187; //任务键(最近打开的应用列表键) 
public static final int KEYCODE_VOLUME_UP       = 24; //音量增加
public static final int KEYCODE_VOLUME_DOWN     = 25; //音量减少
public static final int KEYCODE_BRIGHTNESS_UP   = 220;//增加亮度
public static final int KEYCODE_BRIGHTNESS_DOWN = 221;//降低亮度

三、Android Keycode的使用场景

1. 监听键盘事件

我们可以使用 setOnKeyListener() 监听按键事件。

EditText editText = findViewById(R.id.et_input);
editText.setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_ENTER:
                //执行回车操作
                break;
            case KeyEvent.KEYCODE_DEL:
                //执行删除操作
                break;
        }
        return false;
    }
});

2. 控制音量和亮度

我们可以使用 Keycode 配合AudioManagerSettings.System控制音量和亮度。比如,下面这个例子显示了如何控制音量。

AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);

下面这个例子展示了如何使用Keycode和API改变屏幕亮度。

int oldBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 125);
int newBrightness = oldBrightness + 10;
if (newBrightness > 255) {
    newBrightness = 255;
} else if (newBrightness < 0) {
    newBrightness = 0;
}
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, newBrightness);
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = newBrightness / 255.0f;
getWindow().setAttributes(layoutParams);

3. 控制按键绑定

我们还可以绑定按键到系统事件。例如,我们可以设置KEYCODE_VOLUME_UP按键控制音量增加。

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
        // 增加音量
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

四、总结

在 Android 开发中,Keycode 功能强大。可以用来控制设备的按键事件、调节音量和亮度、绑定按键到系统事件等。掌握 Keycode 的基本用法,可以帮助我们更好地开发出满足用户需求的应用。