前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >andriod 手机按键检测事件 onKeyDown() 简述

andriod 手机按键检测事件 onKeyDown() 简述

作者头像
林冠宏-指尖下的幽灵
发布2018-01-03 09:39:09
1.2K0
发布2018-01-03 09:39:09
举报

函数原型:

public boolean onKeyDown(int keyCode, KeyEvent event);

第一个参数是用户按下键时,当前所接收到的按键代号;

第二个参数是按键事件的对象。

使用方法:

      如果要使用这个方法。直接在主 acivity 中重写即可,一般使用开关语句 switch 来把keyCode 和 (event.按键类型) 对比来执行对应的操作。

下面我们来看下event 的按键属性都有哪些。

      在上面的函数中,按住control 加 鼠标左键点击 KeyEvent,就可以点进去看到很多东西。

代码语言:javascript
复制
  1 public class KeyEvent extends InputEvent implements Parcelable {
  2     /** Key code constant: Unknown key code. */
  3     public static final int KEYCODE_UNKNOWN         = 0;
  4     /** Key code constant: Soft Left key.
  5      * Usually situated below the display on phones and used as a multi-function
  6      * feature key for selecting a software defined function shown on the bottom left
  7      * of the display. */
  8     public static final int KEYCODE_SOFT_LEFT       = 1;
  9     /** Key code constant: Soft Right key.
 10      * Usually situated below the display on phones and used as a multi-function
 11      * feature key for selecting a software defined function shown on the bottom right
 12      * of the display. */
 13     public static final int KEYCODE_SOFT_RIGHT      = 2;
 14     /** Key code constant: Home key.
 15      * This key is handled by the framework and is never delivered to applications. */
 16     public static final int KEYCODE_HOME            = 3;
 17     /** Key code constant: Back key. */
 18     public static final int KEYCODE_BACK            = 4;
 19     /** Key code constant: Call key. */
 20     public static final int KEYCODE_CALL            = 5;
 21     /** Key code constant: End Call key. */
 22     public static final int KEYCODE_ENDCALL         = 6;
 23     /** Key code constant: '0' key. */
 24     public static final int KEYCODE_0               = 7;
 25     /** Key code constant: '1' key. */
 26     public static final int KEYCODE_1               = 8;
 27     /** Key code constant: '2' key. */
 28     public static final int KEYCODE_2               = 9;
 29     /** Key code constant: '3' key. */
 30     public static final int KEYCODE_3               = 10;
 31     /** Key code constant: '4' key. */
 32     public static final int KEYCODE_4               = 11;
 33     /** Key code constant: '5' key. */
 34     public static final int KEYCODE_5               = 12;
 35     /** Key code constant: '6' key. */
 36     public static final int KEYCODE_6               = 13;
 37     /** Key code constant: '7' key. */
 38     public static final int KEYCODE_7               = 14;
 39     /** Key code constant: '8' key. */
 40     public static final int KEYCODE_8               = 15;
 41     /** Key code constant: '9' key. */
 42     public static final int KEYCODE_9               = 16;
 43     /** Key code constant: '*' key. */
 44     public static final int KEYCODE_STAR            = 17;
 45     /** Key code constant: '#' key. */
 46     public static final int KEYCODE_POUND           = 18;
 47     /** Key code constant: Directional Pad Up key.
 48      * May also be synthesized from trackball motions. */
 49     public static final int KEYCODE_DPAD_UP         = 19;
 50     /** Key code constant: Directional Pad Down key.
 51      * May also be synthesized from trackball motions. */
 52     public static final int KEYCODE_DPAD_DOWN       = 20;
 53     /** Key code constant: Directional Pad Left key.
 54      * May also be synthesized from trackball motions. */
 55     public static final int KEYCODE_DPAD_LEFT       = 21;
 56     /** Key code constant: Directional Pad Right key.
 57      * May also be synthesized from trackball motions. */
 58     public static final int KEYCODE_DPAD_RIGHT      = 22;
 59     /** Key code constant: Directional Pad Center key.
 60      * May also be synthesized from trackball motions. */
 61     public static final int KEYCODE_DPAD_CENTER     = 23;
 62     /** Key code constant: Volume Up key.
 63      * Adjusts the speaker volume up. */
 64     public static final int KEYCODE_VOLUME_UP       = 24;
 65     /** Key code constant: Volume Down key.
 66      * Adjusts the speaker volume down. */
 67     public static final int KEYCODE_VOLUME_DOWN     = 25;
 68     /** Key code constant: Power key. */
 69     public static final int KEYCODE_POWER           = 26;
 70     /** Key code constant: Camera key.
 71      * Used to launch a camera application or take pictures. */
 72     public static final int KEYCODE_CAMERA          = 27;
 73     /** Key code constant: Clear key. */
 74     public static final int KEYCODE_CLEAR           = 28;
 75     /** Key code constant: 'A' key. */
 76     public static final int KEYCODE_A               = 29;
 77     /** Key code constant: 'B' key. */
 78     public static final int KEYCODE_B               = 30;
 79     /** Key code constant: 'C' key. */
 80     public static final int KEYCODE_C               = 31;
 81     /** Key code constant: 'D' key. */
 82     public static final int KEYCODE_D               = 32;
 83     /** Key code constant: 'E' key. */
 84     public static final int KEYCODE_E               = 33;
 85     /** Key code constant: 'F' key. */
 86     public static final int KEYCODE_F               = 34;
 87     /** Key code constant: 'G' key. */
 88     public static final int KEYCODE_G               = 35;
 89     /** Key code constant: 'H' key. */
 90     public static final int KEYCODE_H               = 36;
 91     /** Key code constant: 'I' key. */
 92     public static final int KEYCODE_I               = 37;
 93     /** Key code constant: 'J' key. */
 94     public static final int KEYCODE_J               = 38;
 95     /** Key code constant: 'K' key. */
 96     public static final int KEYCODE_K               = 39;
 97     /** Key code constant: 'L' key. */
 98     public static final int KEYCODE_L               = 40;
 99     /** Key code constant: 'M' key. */
100     public static final int KEYCODE_M               = 41;
101     /** Key code constant: 'N' key. */
102     public static final int KEYCODE_N               = 42;
103     /** Key code constant: 'O' key. */
104     public static final int KEYCODE_O               = 43;
105     /** Key code constant: 'P' key. */
106     public static final int KEYCODE_P               = 44;
107     /** Key code constant: 'Q' key. */
108     public static final int KEYCODE_Q               = 45;
109     /** Key code constant: 'R' key. */
110     public static final int KEYCODE_R               = 46;
111     /** Key code constant: 'S' key. */
112     public static final int KEYCODE_S               = 47;
113     /** Key code constant: 'T' key. */
114     public static final int KEYCODE_T               = 48;
115     /** Key code constant: 'U' key. */
116     public static final int KEYCODE_U               = 49;
117     /** Key code constant: 'V' key. */
118     public static final int KEYCODE_V               = 50;
119     /** Key code constant: 'W' key. */
120     public static final int KEYCODE_W               = 51;
121     /** Key code constant: 'X' key. */
122     public static final int KEYCODE_X               = 52;
123     /** Key code constant: 'Y' key. */
124     public static final int KEYCODE_Y               = 53;
125     /** Key code constant: 'Z' key. */
126     public static final int KEYCODE_Z               = 54;
127     /** Key code constant: ',' key. */
128     public static final int KEYCODE_COMMA           = 55;
129     /** Key code constant: '.' key. */
130     public static final int KEYCODE_PERIOD          = 56;
131     /** Key code constant: Left Alt modifier key. */
132     public static final int KEYCODE_ALT_LEFT        = 57;
133     /** Key code constant: Right Alt modifier key. */
134     public static final int KEYCODE_ALT_RIGHT       = 58;
135     /** Key code constant: Left Shift modifier key. */
136     public static final int KEYCODE_SHIFT_LEFT      = 59;
137     /** Key code constant: Right Shift modifier key. */
138     public static final int KEYCODE_SHIFT_RIGHT     = 60;
139     /** Key code constant: Tab key. */
140     public static final int KEYCODE_TAB             = 61;
141     /** Key code constant: Space key. */
142     public static final int KEYCODE_SPACE           = 62;
143     /** Key code constant: Symbol modifier key.
144      * Used to enter alternate symbols. */
145     public static final int KEYCODE_SYM             = 63;
146     /** Key code constant: Explorer special function key.
147      * Used to launch a browser application. */
148     public static final int KEYCODE_EXPLORER        = 64;
149     /** Key code constant: Envelope special function key.
150      * Used to launch a mail application. */
151     public static final int KEYCODE_ENVELOPE        = 65;
152     /** Key code constant: Enter key. */
153     public static final int KEYCODE_ENTER           = 66;
154     /** Key code constant: Backspace key.
155      * Deletes characters before the insertion point, unlike {@link #KEYCODE_FORWARD_DEL}. */
156     public static final int KEYCODE_DEL             = 67;
157     /** Key code constant: '`' (backtick) key. */
158     public static final int KEYCODE_GRAVE           = 68;
159     /** Key code constant: '-'. */
160     public static final int KEYCODE_MINUS           = 69;
161     /** Key code constant: '=' key. */
162     public static final int KEYCODE_EQUALS          = 70;
163     /** Key code constant: '[' key. */
164     public static final int KEYCODE_LEFT_BRACKET    = 71;
165     /** Key code constant: ']' key. */
166     public static final int KEYCODE_RIGHT_BRACKET   = 72;
167 .
168 .
169 .
170 .
171 .
172 下面还很多...

根据英语的提示,我们可以很容易地识别出,这些按键的类型,下面举个例子;

代码语言:javascript
复制
1 @Override
2  2     public boolean onKeyDown(int keyCode, KeyEvent event) {
3  3         // TODO Auto-generated method stub
4  4         
5  5         if(keyCode==KeyEvent.KEYCODE_BACK){//back,返回键
6  6             Toast.makeText(this,"你按了返回键",LENGTH_LONG).show();
7 33         }
8 35         return super.onKeyDown(keyCode, event);
9 36     }

上面是简单的一个按键时间。一般要处理很多事件的时候,用 switch - case

代码语言:javascript
复制
 1 @Override
 2     public boolean onKeyDown(int keyCode, KeyEvent event) {
 3         // TODO Auto-generated method stub
 4         if(event.getRepeatCount()==0) {//限制重复次数,防止用户过多点击
 5             switch(keyCode) {
 6                 case KeyEvent.KEYCODE_SEARCH:
 7 
 8                     break;
 9                 case KeyEvent.KEYCODE_CAMERA:
10 
11                     break;
12                 case KeyEvent.KEYCODE_DPAD_CENTER:
13 
14                     break;
15 
16                 default:
17                     break;
18             }
19         }
20         return super.onKeyDown(keyCode, event);
21     }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-04-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档