zoukankan      html  css  js  c++  java
  • adb模拟按键与输入

    作者:如果从下文中没有获取到你想要的答案,可以留言给我,我们可以一起探究~~

    在 adb shell 里有个很实用的命令叫 input,通过它可以做一些有趣的事情。

    input 命令的完整 help 信息如下:

    Usage: input [<source>] <command> [<arg>...]
    
    The sources are:
          mouse
          keyboard
          joystick
          touchnavigation
          touchpad
          trackball
          stylus
          dpad
          gesture
          touchscreen
          gamepad
    
    The commands and default sources are:
          text <string> (Default: touchscreen)
          keyevent [--longpress] <key code number or name> ... (Default: keyboard)
          tap <x> <y> (Default: touchscreen)
          swipe <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen)
          press (Default: trackball)
          roll <dx> <dy> (Default: trackball)

    比如使用 adb shell input keyevent <keycode> 命令,不同的 keycode 能实现不同的功能,完整的 keycode 列表详见 KeyEvent,摘引部分我觉得有意思的如下:

    keycode含义
    3 HOME 键
    4 返回键
    5 打开拨号应用
    6 挂断电话
    24 增加音量
    25 降低音量
    26 电源键
    27 拍照(需要在相机应用里)
    64 打开浏览器
    82 菜单键
    85 播放/暂停
    86 停止播放
    87 播放下一首
    88 播放上一首
    122 移动光标到行首或列表顶部
    123 移动光标到行末或列表底部
    126 恢复播放
    127 暂停播放
    164 静音
    176 打开系统设置
    187 切换应用
    207 打开联系人
    208 打开日历
    209 打开音乐
    210 打开计算器
    220 降低屏幕亮度
    221 提高屏幕亮度
    223 系统休眠
    224 点亮屏幕
    231 打开语音助手
    276 如果没有 wakelock 则让系统休眠

    下面是 input 命令的一些用法举例。

    电源键

    命令:

    adb shell input keyevent 26

    执行效果相当于按电源键。

    菜单键

    命令:

    adb shell input keyevent 82

    HOME 键

    命令:

    adb shell input keyevent 3

    返回键

    命令:

    adb shell input keyevent 4

    音量控制

    增加音量:

    adb shell input keyevent 24

    降低音量:

    adb shell input keyevent 25

    静音:

    adb shell input keyevent 164

    媒体控制

    播放/暂停:

    adb shell input keyevent 85

    停止播放:

    adb shell input keyevent 86

    播放下一首:

    adb shell input keyevent 87

    播放上一首:

    adb shell input keyevent 88

    恢复播放:

    adb shell input keyevent 126

    暂停播放:

    adb shell input keyevent 127

    点亮/熄灭屏幕

    可以通过上文讲述过的模拟电源键来切换点亮和熄灭屏幕,但如果明确地想要点亮或者熄灭屏幕,那可以使用如下方法。

    点亮屏幕:

    adb shell input keyevent 224

    熄灭屏幕:

    adb shell input keyevent 223

    滑动解锁

    如果锁屏没有密码,是通过滑动手势解锁,那么可以通过 input swipe 来解锁。

    命令(参数以机型 Nexus 5,向上滑动手势解锁举例):

    adb shell input swipe 300 1000 300 500

    参数 300 1000 300 500 分别表示起始点x坐标 起始点y坐标 结束点x坐标 结束点y坐标

    输入文本

    在焦点处于某文本框时,可以通过 input 命令来输入文本。

    命令:

    adb shell input text hello

    现在 hello 出现在文本框了。

  • 相关阅读:
    Android Widget小组件开发(一)——Android实现时钟Widget组件的步骤开发,这些知识也是不可缺少的!
    java基础——static keyword小节
    iOS UI08_UITableView
    Android 6.0 开发人员对系统权限的使用与练习(Permissions Best Practices)
    Android简单实现BroadCastReceiver广播机制
    UI组件之TextView及其子类(一)TextView和EditText
    BZOJ 3112 [Zjoi2013]防守战线 线性规划
    反射 + 配置文件 实现IOC容器
    android:异步任务asyncTask介绍及异步任务下载图片(带进度条)
    【LeetCode-面试算法经典-Java实现】【058-Length of Last Word (最后一个单词的长度)】
  • 原文地址:https://www.cnblogs.com/lybolg/p/7308745.html
Copyright © 2011-2022 走看看