首页 技术 正文
技术 2022年11月23日
0 收藏 583 点赞 4,284 浏览 2179 个字

按键与KEYCODE使用

一、手机常见按键:

  1)HOME 主屏幕键

  2) MENU 菜单键

  3) BACK 返回键

  4) VOLUME_UP 音量加键

  5) VOLUME_DOWN 音量减键

  6) RecentApps 最近使用app

  7) POWER 电源键

  8) Dpad 上下左右键

  9) ……

二、按键API说明:

返回值 方法名 描述
boolean pressBace() 模拟短按返回back键
boolean pressDPadCenter() 模拟轨迹球中点按键
boolean pressDPadDown() 模拟轨迹球向下按键
boolean pressDPadLeft() 模拟轨迹球向左按键
boolean pressDPadRight() 模拟轨迹球向右按键
boolean pressDPadUp() 模拟轨迹球向上按键
boolean pressDelete() 模拟短按删除delete按键
boolean pressEnter() 模拟短按回车键
boolean pressHome() 模拟短按home键
boolean pressKeyCode(int keyCode, int metaState) 模拟短按键盘代码keyCode
boolean pressKeyCode(int keyCode) 模拟短按键盘代码keyCode
boolean pressMenu() 模拟短按menu键
boolean pressRecentApps() 模拟短按最近使用程序
boolean pressSearch() 模拟短按搜索键

举例:

package com.uiautomatortest;import android.os.Bundle;
import android.os.RemoteException;import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;public class Test extends UiAutomatorTestCase { public void testHome(){ UiDevice.getInstance().pressHome();
sleep(2000);
} public void testMenu(){ UiDevice.getInstance().pressMenu();
sleep(2000);
} public void testRecent() throws RemoteException{ UiDevice.getInstance().pressRecentApps();
sleep(2000);
}}

Test.java

三、KEYCODE 键盘映射码:

  1)KeyEvent 按键事件

  2)META KEY

    辅助功能键:ALT、SHIFT、CAPS_LOCK

激活状态 metaState
base META_key未被激活 0
caps Shift或Caps Lock被激活 1
fn Alt被激活 2
caps_fn Alt、Shift或Caps Lock同时被激活 3

举例:

package com.uiautomatortest;import android.os.Bundle;
import android.os.RemoteException;
import android.view.KeyEvent;import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;public class Test extends UiAutomatorTestCase { public void testHome(){ UiDevice.getInstance().pressHome();
sleep(2000);
} public void testMenu(){ UiDevice.getInstance().pressMenu();
sleep(2000);
} public void testRecent() throws RemoteException{ UiDevice.getInstance().pressRecentApps();
sleep(2000);
} public void testKeyCode(){ UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_A); //小写a
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_B); //小写b
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_C); //小写c UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_A,1); //大写A
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_B,1); //大写B
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_C,1); //大写C }}

Test.java

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848