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

简介: 函数原型: public boolean onKeyDown(int keyCode, KeyEvent event); 第一个参数是用户按下键时,当前所接收到的按键代号; 第二个参数是按键事件的对象。

函数原型:

public boolean onKeyDown(int keyCode, KeyEvent event);

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

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

 

使用方法:

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

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

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

  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 下面还很多...

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

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

 

 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     }

 

如果您认为这篇文章还不错或者有所收获,您可以通过扫描一下下面的支付宝二维码 打赏我一杯咖啡【物质支持】,也可以点击右下角的【推荐】按钮【精神支持】,因为这两种支持都是我继续写作,分享的最大动力


img_12e3f54d4d0f70f0eb14f20548e3d781.png
目录
相关文章
|
2月前
|
编解码 异构计算
无影云电脑产品使用黑神话悟空之是否可以想主播们玩的那样手机屏幕上有虚拟按键
这段内容主要讨论了无影云电脑在使用过程中遇到的五个问题及其解答,包括是否能在手机屏幕上显示虚拟按键、解决服务器网络异常导致的连接问题、处理活动前购买的云电脑无法开启电竞模式的情况、解决游戏崩溃问题,以及处理打开游戏时报错的问题。文中提供了详细的解决步骤和参考链接。
|
6月前
|
Android开发
安卓手机快速过检测完成某某学习
安卓手机快速过检测完成某某学习
53 0
|
JavaScript
js手机端长按选中文字执行事件
js手机端长按选中文字执行事件
|
JavaScript
vue监听手机键盘搜索事件
vue监听手机键盘搜索事件
|
算法 数据安全/隐私保护 计算机视觉
基于Aidlux平台实现手机摄像头实时Canny检测
基于Aidlux平台实现手机摄像头实时Canny检测
|
JavaScript
vant/vue手机端长按事件以及禁止长按弹出菜单
vant/vue手机端长按事件以及禁止长按弹出菜单
518 0
|
机器学习/深度学习 人工智能 安全
告别捅嗓子?AI手机程序通过声音检测新冠,准确率已达到89%
大数据文摘出品 不知道大家对做核酸怎么看,反正文摘菌的喉咙已经起茧了
230 0
告别捅嗓子?AI手机程序通过声音检测新冠,准确率已达到89%
|
安全 算法 关系型数据库
APP手机端安全漏洞检测服务的详细介绍
关于APP漏洞检测,分为两个层面的安全检测,包括手机应用层,以及APP代码层,与网站的漏洞检测基本上差不多,目前越来越多的手机应用都存在着漏洞,关于如何对APP进行漏洞检测,我们详细的介绍一下.
423 0
APP手机端安全漏洞检测服务的详细介绍
|
PHP Android开发
【PHP】安卓手机监听物理返回按键
【PHP】安卓手机监听物理返回按键
112 0
|
开发者
uni-app 长按事件 事件目标元素 手机运行uni-app
uni-app 长按事件 事件目标元素 手机运行uni-app