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

函数原型:

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     }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏糊一笑

react脚手架改造(react/react-router/redux/eslint/karam/immutable/es6/webpack/Redux DevTools)

公司突然组织需要重新搭建一个基于node的论坛系统,前端采用react,上网找了一些脚手架,或多或少不能满足自己的需求,最终在基于YeoMan的react脚手架...

3255
来自专栏技术博客

SWFTools PDF转换为SWF

 在iText 制作PDF这篇博文中只是简单的介绍了如何制作PDF,为了能让PDF在Web页面中显示,我还需要通过SWFTools工具将PDF文件转换为SWF文...

1222
来自专栏沃趣科技

ASM 翻译系列第三十四弹:ASM磁盘组重要属性介绍

原作者:Bane Radulovic 译者: 邱大龙 审核: 魏兴华 DBGeeK社区联合出品 原文链接:http://asmsupportguy....

3356
来自专栏ionic3+

【技巧】ionic3视频上传

有人问到视频上传这个问题,那我还是写一下吧,其实基本参考《ionic3多文件上传》这文章也行,不过对于单文件上传就不用那么复杂了,步骤如下:

782
来自专栏数据和云

Oracle 12.2 新特性 | PDB不同字符集变更深入解析

从12c版本12.1开始,就有了新特性——Pluggable Database,相比较之前通过schema的管理方式实现多用户管理,Pluggable Data...

3309
来自专栏云端漫步

harbor源码分析之构建工具make(五)

make是一个构建工具,现在前端的构建工具有很多.像gulp,grunt等等,它们常被用在前端项目中.在后端领域常使用make来做构建这件事情.

1500
来自专栏程序猿

Burp Suite第十五节:BurpSuite全局参数设置和使用

在Burp Suite中,存在一些粗粒度的设置,这些设置选项,一旦设置了将会对Burp Suite的整体产生效果,这就是Burp Suite中Op...

4758
来自专栏Golang语言社区

gRPC服务发现&负载均衡

构建高可用、高性能的通信服务,通常采用服务注册与发现、负载均衡和容错处理等机制实现。根据负载均衡实现所在的位置不同,通常可分为以下三种解决方案:

5002
来自专栏mySoul

localStorage sessionStorage

大文档 https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity

1593
来自专栏coding for love

在线商城项目17-登录态保持

前面我们实现了登入登出,一切看上去是没有什么问题,但是如果我们试着在登录情况下刷新一下页面。

881

扫码关注云+社区