有时我们希望程序在后台运行时能接收到按键输入消息,我们在底层修改添加广播消息就可以实现这样的功能,具体过程如下所示:
1.在lichee/linux-3.0/include/linux/input.h文件可以查看底层驱动的按键编码
#define KEY_F1 59
#define KEY_F2 60
#define KEY_F3 61
#define KEY_F4 62
#define KEY_F5 63
#define KEY_F6 64
#define KEY_F7 65
#define KEY_F8 66
#define KEY_F9 67
#define KEY_F10 68
2.在android4.0/frameworks/base/core/java/android/view/KeyEvent.java文件可以查看上层驱动的按键编码
/** Key code constant: F1 key. */
public static final int KEYCODE_F1 = 131;
/** Key code constant: F2 key. */
public static final int KEYCODE_F2 = 132;
/** Key code constant: F3 key. */
public static final int KEYCODE_F3 = 133;
/** Key code constant: F4 key. */
public static final int KEYCODE_F4 = 134;
/** Key code constant: F5 key. */
public static final int KEYCODE_F5 = 135;
/** Key code constant: F6 key. */
public static final int KEYCODE_F6 = 136;
/** Key code constant: F7 key. */
public static final int KEYCODE_F7 = 137;
/** Key code constant: F8 key. */
public static final int KEYCODE_F8 = 138;
/** Key code constant: F9 key. */
public static final int KEYCODE_F9 = 139;
/** Key code constant: F10 key. */
public static final int KEYCODE_F10 = 140;
3.在/android4.0/frameworks/base/data/keyboards/Generic.kl文件里面有底层到上层的映射,将这个文件定制到自己的系统android4.0/out/target/product/crane-m1003h6/system/usr/keylayout目录下就可以了在上层收到USB键盘F1-F10的消息了。
key 59 F1
key 60 F2
key 61 F3
key 62 F4
key 63 F5
key 64 F6
key 65 F7
key 66 F8
key 67 F9
key 68 F10
4.在/android4.0/frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindow.java文件的onKeyDown,onKeyUp函数截获F1-F10的消息,用自己定义的广播消息广播出去应用就可以在后台或者前台都可以接收到按键按下或者释放的广播消息了。
case KeyEvent.KEYCODE_F1: {
//Log.e("zkliu","---------------------------test F1---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F1_KEYDOWN"));
return true;
}
case KeyEvent.KEYCODE_F2: {
//Log.e("zkliu","---------------------------test F2---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F2_KEYDOWN"));
return true;
}
case KeyEvent.KEYCODE_F3: {
//Log.e("zkliu","---------------------------test F3---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F3_KEYDOWN"));
return true;
}
case KeyEvent.KEYCODE_F4: {
//Log.e("zkliu","---------------------------test F4---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F4_KEYDOWN"));
return true;
}
case KeyEvent.KEYCODE_F5: {
//Log.e("zkliu","---------------------------test F5---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F5_KEYDOWN"));
return true;
}
case KeyEvent.KEYCODE_F6: {
//Log.e("zkliu","---------------------------test F6---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F6_KEYDOWN"));
return true;
}
case KeyEvent.KEYCODE_F7: {
//Log.e("zkliu","---------------------------test F7---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F7_KEYDOWN"));
return true;
}
case KeyEvent.KEYCODE_F8: {
//Log.e("zkliu","---------------------------test F8---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F8_KEYDOWN"));
return true;
}
case KeyEvent.KEYCODE_F9: {
//Log.e("zkliu","---------------------------test F9---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F9_KEYDOWN"));
return true;
}
case KeyEvent.KEYCODE_F10: {
//Log.e("zkliu","---------------------------test F10---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F10_KEYDOWN"));
return true;
}
case KeyEvent.KEYCODE_F1: {
//Log.e("zkliu","---------------------------test F1---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F1_KEYUP"));
return true;
}
case KeyEvent.KEYCODE_F2: {
//Log.e("zkliu","---------------------------test F2---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F2_KEYUP"));
return true;
}
case KeyEvent.KEYCODE_F3: {
//Log.e("zkliu","---------------------------test F3---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F3_KEYUP"));
return true;
}
case KeyEvent.KEYCODE_F4: {
//Log.e("zkliu","---------------------------test F4---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F4_KEYUP"));
return true;
}
case KeyEvent.KEYCODE_F5: {
//Log.e("zkliu","---------------------------test F5---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F5_KEYUP"));
return true;
}
case KeyEvent.KEYCODE_F6: {
//Log.e("zkliu","---------------------------test F6---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F6_KEYUP"));
return true;
}
case KeyEvent.KEYCODE_F7: {
//Log.e("zkliu","---------------------------test F7---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F7_KEYUP"));
return true;
}
case KeyEvent.KEYCODE_F8: {
//Log.e("zkliu","---------------------------test F8---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F8_KEYUP"));
return true;
}
case KeyEvent.KEYCODE_F9: {
//Log.e("zkliu","---------------------------test F9---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F9_KEYUP"));
return true;
}
case KeyEvent.KEYCODE_F10: {
//Log.e("zkliu","---------------------------test F10---------------------------");
getContext().sendBroadcast(new Intent("com.android.action.F10_KEYUP"));
return true;
}
应用程序注册接收相应的广播消息即可接收到键盘输入消息。
完整的底层系统源代码以及测试程序可以发邮件给:[email protected]索取。