按键输入有三种:system key 音量键
global key 按下启动某个APP
user key ABCD...
给安卓应用程序定义一个广播接收者,写一个BroadcastReceiver的子类:
public class MyBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Get BroadcastReceiver",Toast.LENGTH_SHORT).show();
Intent intent1 = new Intent(context, MainActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
}
在.xml文件里定义接收的按键是GlobalKey:
<receiver android:name=".MyBroadCastReceiver">
<intent-filter android:priority="20">
<action android:name="android.intent.action.GLOBAL_BUTTON"/>
</intent-filter>
</receiver>
然后使用am命令发广播:am broadcast -a android.intent.action.GLOBAL_BUTTON -n com.example.zhu.zhu_first/.MyBroadCastReceiver
可以使用按键启动APP:
修改global_keys.xml:
<key keyCode="KEYCODE_TV" component=" com.example.zhu.zhu_first/.MyBroadCastReceiver " />
编译安卓系统生成frameworks-res.apk
放到/system/framework/下面
/system只读,重新挂载:
Mount -o remount,rw /system
Cat /proc/mounts 查看挂载情况
虚拟按键驱动:
#include <linux/kernel.h> #include <linux/slab.h> #include <linux/module.h> #include <linux/init.h> #include <linux/hid.h> static struct input_dev *inputdev; static int input_init(void) { int i; inputdev = input_allocate_device(); set_bit(EV_KEY,inputdev->evbit); set_bit(EV_REP,inputdev->evbit); for (i = 0; i < BITS_TO_LONGS(KEY_CNT); i++) inputdev->keybit[i] = ~0UL; inputdev->name = "virtualInput"; inputdev->id.bustype = 1; inputdev->id.product = 2; inputdev->id.version = 1; inputdev->id.vendor = 3; input_register_device(inputdev); return 0; } static void input_exit(void) { input_unregister_device(inputdev); input_free_device(inputdev); } module_init(input_init); module_exit(input_exit); MODULE_LICENSE("GPL");
加载驱动:
然后使用sendevent /dev/input/event5 1 377 1 //按下
sendevent /dev/input/event5 1 377 0 //松开
sendevent /dev/input/event5 0 0 0 //同步
发送AKEYCODE_TV到app
也可以不使用虚拟按键驱动:
直接使用 input keyevent TV 按下AKEYCODE_TV
按键扫描输入与真实输入之间的映射关系通过修改.kl和.kcm两个配置文件来决定
3种配置文件:(通过串口修改安卓系统,然后重启开发板)
1、IDC:input Device configure
/system/usr/idc/Vendor_XXXX_Product_XXXX_Version_XXXX.idc
/system/usr/idc/Vendor_XXXX_Product_XXXX.idc
/system/usr/idc/DEVICE_NAME.idc
/data/system/devices/idc/Vendor_XXXX_Product_XXXX_Version_XXXX.idc
/data/system/devices/idc/Vendor_XXXX_Product_XXXX.idc
/data/system/devices/idc/DEVICE_NAME.idc
2、Keylayout //XXXX : 4位16进制
inputdev->id.product = 2;
inputdev->id.version = 1;
inputdev->id.vendor = 3;
--> Vendor_0003_Product_0002_Version_0001.kl
按照顺序查找这8个配置文件,然后使用。如果没有配置文件,就使用默认的,与linux内核按键值一一对应。创建好配置文件之后一定记住修改访问权限 chmod 777 Vendor_0003_Product_0002_Version_0001.kl
chmod 777 keylayout/
chmod 777 devices/
/system/usr/keylayout/Vendor_XXXX_Product_XXXX_Version_XXXX.kl
/system/usr/keylayout/Vendor_XXXX_Product_XXXX.kl
/system/usr/keylayout/DEVICE_NAME.kl //input_dev->name
/data/system/devices/keylayout/Vendor_XXXX_Product_XXXX_Version_XXXX.kl
/data/system/devices/keylayout/Vendor_XXXX_Product_XXXX.kl
/data/system/devices/keylayout/DEVICE_NAME.kl
/system/usr/keylayout/Generic.kl
/data/system/devices/keylayout/Generic.kl
3、KCM: key character map
/system/usr/keychars/Vendor_XXXX_Product_XXXX_Version_XXXX.kcm
/system/usr/keychars/Vendor_XXXX_Product_XXXX.kcm
/system/usr/keychars/DEVICE_NAME.kcm
/data/system/devices/keychars/Vendor_XXXX_Product_XXXX_Version_XXXX.kcm
/data/system/devices/keychars/Vendor_XXXX_Product_XXXX.kcm
/data/system/devices/keychars/DEVICE_NAME.kcm
/system/usr/keychars/Generic.kcm
/data/system/devices/keychars/Generic.kcm
/system/usr/keychars/Virtual.kcm
/data/system/devices/keychars/Virtual.kcm