【android|android 简单按键修改】目的:修改按键映射表,把一个已存在的键映射为自己想要的键,然后在应用程序中捕获它。
说明:本文并没有增加额外的按键。
按键格式:keySCANCODEKEYCODE[FLAGS...]
当扫描码是一个数字,键码定义在你描述的布局文件android.keylayout.xxx,另外可以设置相关的FLAGS:
SHIFT: 当按下,自动加上SHIFT键值
ALT:当按下,自动加上ALT
CAPS:当按下,自动带上CAPS大写
WAKE:当按下,当设备进入睡眠的时候,按下这个键将唤醒,而且发送消息给应用层。
WAKE_DROPPED:当按下,且设备正处于睡眠,设备被唤醒,但是不发送消息给应用层。
开发板上的键盘映射表:
# cat TWL4030_Keypad.kl
# OMAP3EVM keylayoutkey 25MENUWAKE_DROPPED
key 24BACKWAKE_DROPPED
key 49DPAD_UPWAKE_DROPPED
key 37HOMEWAKE
key 36EXPLORERWAKE
key 23VOLUME_UP
key 28DPAD_LEFTWAKE_DROPPED
key 33DPAD_CENTERWAKE_DROPPED
key 18DPAD_RIGHTWAKE_DROPPED
key 103SEARCHWAKE
key 108VOLUME_DOWN
key 48SOFT_LEFTWAKE_DROPPED
key 30DPAD_DOWNWAKE_DROPPED
key 106SOFT_RIGHTWAKE_DROPPED
#key 105POWERWAKE
key 1051WAKE_DROPPED
修改最后一行:
key 1051WAKE_DROPPED
即可响应对按键1的响应。把原来那个对PWER的响应改成对数字1的响应。
测试代码: 上层java应用程序
package com.android.dm3730.keytest;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;
public class DM3730KeyActivity extends Activity {
private static final String Tag = "DM3730_KEYBOARD_TEST";
private TextView tv = null;
private String str = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)DM3730KeyActivity.this.findViewById(R.id.tv);
} @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
str = "按键为:"+ event.getKeyCode();
switch(event.getKeyCode()){
case KeyEvent.KEYCODE_1:{
DisplayToastShort(DM3730KeyActivity.this,str);
tv.setText("已经捕获到按键1,OK");
}
default:{
break;
}
}
return super.onKeyDown(keyCode, event);
} public static void DisplayToastShort(Context context,String s)
{
Toast toast=Toast.makeText(context, s, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP,0, 220);
toast.show();
}
}