android 简单按键修改

目的:修改按键映射表,把一个已存在的键映射为自己想要的键,然后在应用程序中捕获它。

说明:本文并没有增加额外的按键。

按键格式:key  SCANCODE  KEYCODE  [FLAGS...]

当扫描码是一个数字,键码定义在你描述的布局文件android.keylayout.xxx,另外可以设置相关的FLAGS:
SHIFT: 当按下,自动加上SHIFT键值
ALT:当按下,自动加上ALT
CAPS:当按下,自动带上CAPS大写
WAKE:当按下,当设备进入睡眠的时候,按下这个键将唤醒,而且发送消息给应用层。

WAKE_DROPPED:当按下,且设备正处于睡眠,设备被唤醒,但是不发送消息给应用层。


开发板上的键盘映射表:

# cat TWL4030_Keypad.kl
# OMAP3EVM keylayout

key 25    MENU              WAKE_DROPPED
key 24    BACK              WAKE_DROPPED
key 49    DPAD_UP           WAKE_DROPPED
key 37    HOME              WAKE
key 36    EXPLORER          WAKE
key 23    VOLUME_UP
key 28    DPAD_LEFT         WAKE_DROPPED
key 33    DPAD_CENTER       WAKE_DROPPED
key 18    DPAD_RIGHT        WAKE_DROPPED
key 103   SEARCH            WAKE
key 108   VOLUME_DOWN
key 48	  SOFT_LEFT         WAKE_DROPPED
key 30    DPAD_DOWN         WAKE_DROPPED
key 106   SOFT_RIGHT        WAKE_DROPPED
#key 105   POWER             WAKE    
key 105   1             WAKE_DROPPED
 
  
修改最后一行:

key 105   1             WAKE_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();
	}
}




你可能感兴趣的:(android,系统开发,android,string,keyboard,search,menu,class)