android虚拟按键

android标准键盘是有 HOME,MENU,BACK,SEARCH等,但是同时要保持工业设计。nexus one没有用普通按键用触摸屏实现,

显示屏是800X480,但是在电容触摸屏是8xx*480的就是比800要大的地方就变成了虚拟按键,模拟了android标准按键。

1    方案

要实现虚拟按键,在android里面是靠两层协助实现,底层要把虚拟按键在比显示屏多出的地方规定好虚拟按键的位置大小以及键值等,给上层

一文件接口。上层java层启动一个服务来读取这一区域的按键响应,这样就是大体的架构。具体实现如下:

1.1    底层虚拟按键功能实现方案

简而言之,就是在内核中把虚拟按键的所有信息通过sys文件系统的方式给上层,sys文件系统的路径是约定好的所以代码如下实现。给出信息的

协议格式是一段连续的字符串,每个按键有六项分别用冒号分割,按键按键之间也是用冒号分割,六项按顺序分别是:

键类型:键值:按键区域中心x坐标:按键区域中心y坐标:按键区域宽:按键区域高

arch/arm/mach-msm/board-mahimahi.c

 
  1. <SPAN style="COLOR: #993333">static</SPAN> ssize_t mahimahi_virtual_keys_show<SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #993333">struct</SPAN> kobject <SPAN style="COLOR: #339933">*</SPAN>kobj<SPAN style="COLOR: #339933">,</SPAN>  
  2.                    <SPAN style="COLOR: #993333">struct</SPAN> kobj_attribute <SPAN style="COLOR: #339933">*</SPAN>attr<SPAN style="COLOR: #339933">,</SPAN> <SPAN style="COLOR: #993333">char</SPAN> <SPAN style="COLOR: #339933">*</SPAN>buf<SPAN style="COLOR: #009900">)</SPAN>  
  3. <SPAN style="COLOR: #009900">{</SPAN>  
  4.     <SPAN style="COLOR: #b1b100">if</SPAN> <SPAN style="COLOR: #009900">(</SPAN>system_rev <SPAN style="COLOR: #339933">&</SPAN>gt<SPAN style="COLOR: #339933">;</SPAN> <SPAN style="COLOR: #0000dd">2</SPAN><SPAN style="COLOR: #009900">)</SPAN> <SPAN style="COLOR: #009900">{</SPAN>  
  5.         <SPAN style="COLOR: #808080; FONT-STYLE: italic">/* center: x: back: 55, menu: 172, home: 298, search 412, y: 835 */</SPAN>  
  6.         <SPAN style="COLOR: #b1b100">return</SPAN> sprintf<SPAN style="COLOR: #009900">(</SPAN>buf<SPAN style="COLOR: #339933">,</SPAN>  
  7.             __stringify<SPAN style="COLOR: #009900">(</SPAN>EV_KEY<SPAN style="COLOR: #009900">)</SPAN> <SPAN style="COLOR: #ff0000">":"</SPAN> __stringify<SPAN style="COLOR: #009900">(</SPAN>KEY_BACK<SPAN style="COLOR: #009900">)</SPAN>  <SPAN style="COLOR: #ff0000">":55:835:90:55"</SPAN>  
  8.            <SPAN style="COLOR: #ff0000">":"</SPAN> __stringify<SPAN style="COLOR: #009900">(</SPAN>EV_KEY<SPAN style="COLOR: #009900">)</SPAN> <SPAN style="COLOR: #ff0000">":"</SPAN> __stringify<SPAN style="COLOR: #009900">(</SPAN>KEY_MENU<SPAN style="COLOR: #009900">)</SPAN>   <SPAN style="COLOR: #ff0000">":172:835:125:55"</SPAN>  
  9.            <SPAN style="COLOR: #ff0000">":"</SPAN> __stringify<SPAN style="COLOR: #009900">(</SPAN>EV_KEY<SPAN style="COLOR: #009900">)</SPAN> <SPAN style="COLOR: #ff0000">":"</SPAN> __stringify<SPAN style="COLOR: #009900">(</SPAN>KEY_HOME<SPAN style="COLOR: #009900">)</SPAN>   <SPAN style="COLOR: #ff0000">":298:835:115:55"</SPAN>  
  10.            <SPAN style="COLOR: #ff0000">":"</SPAN> __stringify<SPAN style="COLOR: #009900">(</SPAN>EV_KEY<SPAN style="COLOR: #009900">)</SPAN> <SPAN style="COLOR: #ff0000">":"</SPAN> __stringify<SPAN style="COLOR: #009900">(</SPAN>KEY_SEARCH<SPAN style="COLOR: #009900">)</SPAN> <SPAN style="COLOR: #ff0000">":412:835:95:55"</SPAN>  
  11.            <SPAN style="COLOR: #ff0000">"<SPAN style="FONT-WEIGHT: bold; COLOR: #000099">/n</SPAN>"</SPAN><SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN>  
  12.     <SPAN style="COLOR: #009900">}</SPAN> <SPAN style="COLOR: #b1b100">else</SPAN> <SPAN style="COLOR: #009900">{</SPAN>  
  13.         <SPAN style="COLOR: #808080; FONT-STYLE: italic">/* center: x: home: 55, menu: 185, back: 305, search 425, y: 835 */</SPAN>  
  14.         <SPAN style="COLOR: #b1b100">return</SPAN> sprintf<SPAN style="COLOR: #009900">(</SPAN>buf<SPAN style="COLOR: #339933">,</SPAN>  
  15.             __stringify<SPAN style="COLOR: #009900">(</SPAN>EV_KEY<SPAN style="COLOR: #009900">)</SPAN> <SPAN style="COLOR: #ff0000">":"</SPAN> __stringify<SPAN style="COLOR: #009900">(</SPAN>KEY_HOME<SPAN style="COLOR: #009900">)</SPAN>  <SPAN style="COLOR: #ff0000">":55:835:70:55"</SPAN>  
  16.            <SPAN style="COLOR: #ff0000">":"</SPAN> __stringify<SPAN style="COLOR: #009900">(</SPAN>EV_KEY<SPAN style="COLOR: #009900">)</SPAN> <SPAN style="COLOR: #ff0000">":"</SPAN> __stringify<SPAN style="COLOR: #009900">(</SPAN>KEY_MENU<SPAN style="COLOR: #009900">)</SPAN>   <SPAN style="COLOR: #ff0000">":185:835:100:55"</SPAN>  
  17.            <SPAN style="COLOR: #ff0000">":"</SPAN> __stringify<SPAN style="COLOR: #009900">(</SPAN>EV_KEY<SPAN style="COLOR: #009900">)</SPAN> <SPAN style="COLOR: #ff0000">":"</SPAN> __stringify<SPAN style="COLOR: #009900">(</SPAN>KEY_BACK<SPAN style="COLOR: #009900">)</SPAN>   <SPAN style="COLOR: #ff0000">":305:835:70:55"</SPAN>  
  18.            <SPAN style="COLOR: #ff0000">":"</SPAN> __stringify<SPAN style="COLOR: #009900">(</SPAN>EV_KEY<SPAN style="COLOR: #009900">)</SPAN> <SPAN style="COLOR: #ff0000">":"</SPAN> __stringify<SPAN style="COLOR: #009900">(</SPAN>KEY_SEARCH<SPAN style="COLOR: #009900">)</SPAN> <SPAN style="COLOR: #ff0000">":425:835:70:55"</SPAN>  
  19.            <SPAN style="COLOR: #ff0000">"<SPAN style="FONT-WEIGHT: bold; COLOR: #000099">/n</SPAN>"</SPAN><SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN>  
  20.     <SPAN style="COLOR: #009900">}</SPAN>  
  21. <SPAN style="COLOR: #009900">}</SPAN>  
  22.    
  23. <SPAN style="COLOR: #993333">static</SPAN> <SPAN style="COLOR: #993333">struct</SPAN> kobj_attribute mahimahi_virtual_keys_attr <SPAN style="COLOR: #339933">=</SPAN> <SPAN style="COLOR: #009900">{</SPAN>  
  24.     .<SPAN style="COLOR: #202020">attr</SPAN> <SPAN style="COLOR: #339933">=</SPAN> <SPAN style="COLOR: #009900">{</SPAN>  
  25.         .<SPAN style="COLOR: #202020">name</SPAN> <SPAN style="COLOR: #339933">=</SPAN> <SPAN style="COLOR: #ff0000">"virtualkeys.synaptics-rmi-touchscreen"</SPAN><SPAN style="COLOR: #339933">,</SPAN>  
  26.         .<SPAN style="COLOR: #202020">mode</SPAN> <SPAN style="COLOR: #339933">=</SPAN> S_IRUGO<SPAN style="COLOR: #339933">,</SPAN>  
  27.     <SPAN style="COLOR: #009900">}</SPAN><SPAN style="COLOR: #339933">,</SPAN>  
  28.     .<SPAN style="COLOR: #202020">show</SPAN> <SPAN style="COLOR: #339933">=</SPAN> <SPAN style="COLOR: #339933">&</SPAN>amp<SPAN style="COLOR: #339933">;</SPAN>mahimahi_virtual_keys_show<SPAN style="COLOR: #339933">,</SPAN>  
  29. <SPAN style="COLOR: #009900">}</SPAN><SPAN style="COLOR: #339933">;</SPAN>  
  30.    
  31. <SPAN style="COLOR: #993333">static</SPAN> <SPAN style="COLOR: #993333">struct</SPAN> attribute <SPAN style="COLOR: #339933">*</SPAN>mahimahi_properties_attrs<SPAN style="COLOR: #009900">[</SPAN><SPAN style="COLOR: #009900">]</SPAN> <SPAN style="COLOR: #339933">=</SPAN> <SPAN style="COLOR: #009900">{</SPAN>  
  32.     <SPAN style="COLOR: #339933">&</SPAN>amp<SPAN style="COLOR: #339933">;</SPAN>mahimahi_virtual_keys_attr.<SPAN style="COLOR: #202020">attr</SPAN><SPAN style="COLOR: #339933">,</SPAN>  
  33.     NULL  
  34. <SPAN style="COLOR: #009900">}</SPAN><SPAN style="COLOR: #339933">;</SPAN>  
  35.    
  36. <SPAN style="COLOR: #993333">static</SPAN> <SPAN style="COLOR: #993333">struct</SPAN> attribute_group mahimahi_properties_attr_group <SPAN style="COLOR: #339933">=</SPAN> <SPAN style="COLOR: #009900">{</SPAN>  
  37.     .<SPAN style="COLOR: #202020">attrs</SPAN> <SPAN style="COLOR: #339933">=</SPAN> mahimahi_properties_attrs<SPAN style="COLOR: #339933">,</SPAN>  
  38. <SPAN style="COLOR: #009900">}</SPAN><SPAN style="COLOR: #339933">;</SPAN>  
  39.    
  40. <SPAN style="COLOR: #993333">struct</SPAN> kobject <SPAN style="COLOR: #339933">*</SPAN>properties_kobj<SPAN style="COLOR: #339933">;</SPAN>  
  41.    
  42. properties_kobj <SPAN style="COLOR: #339933">=</SPAN> kobject_create_and_add<SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #ff0000">"board_properties"</SPAN><SPAN style="COLOR: #339933">,</SPAN> NULL<SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN>  
  43. <SPAN style="COLOR: #b1b100">if</SPAN> <SPAN style="COLOR: #009900">(</SPAN>properties_kobj<SPAN style="COLOR: #009900">)</SPAN>  
  44.     ret <SPAN style="COLOR: #339933">=</SPAN> sysfs_create_group<SPAN style="COLOR: #009900">(</SPAN>properties_kobj<SPAN style="COLOR: #339933">,</SPAN>  
  45.                      <SPAN style="COLOR: #339933">&</SPAN>amp<SPAN style="COLOR: #339933">;</SPAN>mahimahi_properties_attr_group<SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN>  
  46. <SPAN style="COLOR: #b1b100">if</SPAN> <SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #339933">!</SPAN>properties_kobj <SPAN style="COLOR: #339933">||</SPAN> ret<SPAN style="COLOR: #009900">)</SPAN>  
  47.     pr_err<SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #ff0000">"failed to create board_properties<SPAN style="FONT-WEIGHT: bold; COLOR: #000099">/n</SPAN>"</SPAN><SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN>  
 
<SPAN style="COLOR: #339933" sizcache="5" sizset="2"><P>在Android系统中,触摸屏设备发送的是RawInputEvent(原始输入事件),而按键发送的是KeyEvent(按键事件)。KeyEvent直接发送给应用程序层,RawInputEvent在Android的Java框架中被转换成MotionEvent发送给应用程序层。</P><P>在Android系统中虚拟按键的实现方法是:在某种情况下,将RawInputEvent转换成KeyEvent。</P><P>frameworks/base/services/Java/com/android/server目录中的InputDevice.Java文件负责处理虚拟按键的主要文件。</P><P>虚拟按键的处理相对简单,需要根据以下文件对虚拟按键的内容进行配置:</P><PRE><OL class=dp-xml><LI class=alt><SPAN><SPAN>/sys/board_properties/virtualkeys.{devicename}&nbsp;</SPAN></SPAN></LI></OL></PRE>
<P>在InputDevice.Java文件中通过readVirtualKeys,对进行消息的转化。根据配置文件将RawInputEvent转换成按键相关的内容。</P>
<P>virtualkeys.{devicename}是虚拟按键的适配文件,需要在目标文件系统的/sys/board_ properties/目录中。</P>
<P>虚拟按键配置文件的格式如下所示:</P>
<PRE><OL class=dp-xml><LI class=alt><SPAN><SPAN>0x1:扫描码:X:Y:W:H:0x1:&nbsp;……&nbsp;</SPAN></SPAN></LI></OL></PRE>
<P>例如,在MSM的mahimahi平台上查看虚拟按键的配置文件如下所示:</P>
<PRE><OL class=dp-xml><LI class=alt><SPAN><SPAN>#&nbsp;cat&nbsp;/sys/board_properties/virtualkeys.synaptics-rmi-touchscreen &nbsp;</SPAN></SPAN><LI><SPAN>0x01:158:55:835:90:55:0x01:139:172:835:125:55:<BR>0x01:102:298:835:115:55:0x01:217:412:835:95:55&nbsp;</SPAN></LI></OL></PRE>
<P>由此可见,其中定义了4个区域的虚拟按键,它们的Y坐标相同,可见4个按键的矩形区域位于水平的一排。其转换的扫描码分别为158,139,102,217,分别对应于BACK(返回),MENU(菜单),HOME(主界面),SEARCH(搜索)这4个按键。</P>
<P>另外一个系统的虚拟按键的配置文件如下所示:</P>
<PRE><OL class=dp-xml><LI class=alt><SPAN><SPAN>$&nbsp;cat&nbsp;/sys/board_properties/virtualkeys.qtouch-touchscreen &nbsp;</SPAN></SPAN><LI><SPAN>0x01:139:90:936:116:104:0x01:102:252:936:<BR>116:104:0x01:158:402:936:116:104&nbsp;</SPAN></LI></OL></PRE>
<P>其转换的扫描码分别为:139,102,158,分别对应于MENU(菜单),HOME(主界面),BACK(返回)这3个按键。</P>
</SPAN>
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值