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 条评论
登录 后参与评论

相关文章

来自专栏小狼的世界

Docker修改daemon.json后无法启动的问题

最近在整理Docker和Kubernetes中的日志与相关配置,在尝试通过/etc/docker/daemon.json配置Docker的log-driver参...

1351
来自专栏杂七杂八

sublime解决There are no packages available for installation及markdown插件

There are no packages available for installation sublime是一个轻便优秀的代码编写工具,而且其众多插件也为...

2958
来自专栏偏前端工程师的驿站

CentOS6.5菜鸟之旅:中文编辑器忍痛放弃Sublime

一、前言                                 Windows下习惯使用Sublime作为编辑器,谁知道Linux下的Sublime是...

1776
来自专栏FreeBuf

Aws_Public_Ips:获取AWS账户关联的所有IP地址(IPv4IPv6)的工具

Aws_Public_Ips是一个可帮助你获取与AWS账户关联的,所有IP地址(IPv4/IPv6) 的工具。它可以被作为库和CLI使用,并支持以下AWS服务(...

892
来自专栏逸鹏说道

NET跨平台:在Ubuntu下搭建ASP.NET 5开发环境

0x00 写在前面的废话 年底这段时间实在太忙了,各种事情都凑在这个时候,没时间去学习自己感兴趣的东西,所以博客也好就没写了。最近工作上有个小功能要做成Web应...

2523
来自专栏黑白安全

weevely用法

Weevely是一款python编写的webshell管理工具,作为编写语言使用python的一款工具,它最大的优点就在于跨平台,可以在任何安装过python的...

1083
来自专栏Urahara Blog

域渗透-信息收集基础

3185
来自专栏JackeyGao的博客

查看你历史命令的使用率

一个查看历史命令的使用率工具, 因为看到oh-my-zsh项目中的zsh_statsfunction 后有感仿照开发了一个。 本项目不仅支持zsh_histor...

291
来自专栏lgp20151222

debug的一些按钮意义

693
来自专栏逸鹏说道

GitHub实战系列~1.环境部署+创建第一个文件 2015-12-9

安装系列: 软件下载:http://git-scm.com/download/ 环境搭建:(比较简单,看图) ? ? ? ? ? ? ? ? 下面是命令模式,需...

2804

扫码关注云+社区