文章出处:http://blog.csdn.net/shift_wwx
前言:这一阵子碰到android的input命令,但是在网上搜了半天,并没有很详细的解释,所以,我就自己做个总结,方便以后查找。
input命令对于开发的工程来说很是方便的,连接上串口,就可以通过命令做自己想做的事情,避免了在平台上的一些复杂的操作。
input命令定义在frameworks\base\cmds\input\src\com\android\commands\input目录下。
source code如下:
package com.android.commands.input; import android.hardware.input.InputManager; import android.os.SystemClock; import android.util.Log; import android.view.InputDevice; import android.view.KeyCharacterMap; import android.view.KeyEvent; import android.view.MotionEvent; /** * Command that sends key events to the device, either by their keycode, or by * desired character output. */ public class Input { private static final String TAG = "Input"; /** * Command-line entry point. * * @param args The command-line arguments */ public static void main(String[] args) { (new Input()).run(args); } private void run(String[] args) { if (args.length < 1) { showUsage(); return; } String command = args[0]; try { if (command.equals("text")) { if (args.length == 2) { sendText(args[1]); return; } } else if (command.equals("keyevent")) { if (args.length == 2) { int keyCode = KeyEvent.keyCodeFromString(args[1]); if (keyCode == KeyEvent.KEYCODE_UNKNOWN) { keyCode = KeyEvent.keyCodeFromString("KEYCODE_" + args[1]); } sendKeyEvent(keyCode); return; } } else if (command.equals("tap")) { if (args.length == 3) { sendTap(InputDevice.SOURCE_TOUCHSCREEN, Float.parseFloat(args[1]), Float.parseFloat(args[2])); return; } } else if (command.equals("swipe")) { if (args.length == 5) { sendSwipe(InputDevice.SOURCE_TOUCHSCREEN, Float.parseFloat(args[1]), Float.parseFloat(args[2]), Float.parseFloat(args[3]), Float.parseFloat(args[4])); return; } } else if (command.equals("touchscreen") || command.equals("touchpad")) { // determine input source int inputSource = InputDevice.SOURCE_TOUCHSCREEN; if (command.equals("touchpad")) { inputSource = InputDevice.SOURCE_TOUCHPAD; } // determine subcommand if (args.length > 1) { String subcommand = args[1]; if (subcommand.equals("tap")) { if (args.length == 4) { sendTap(inputSource, Float.parseFloat(args[2]), Float.parseFloat(args[3])); return; } } else if (subcommand.equals("swipe")) { if (args.length == 6) { sendSwipe(inputSource, Float.parseFloat(args[2]), Float.parseFloat(args[3]), Float.parseFloat(args[4]), Float.parseFloat(args[5])); return; } } } } else if (command.equals("trackball")) { // determine subcommand if (args.length > 1) { String subcommand = args[1]; if (subcommand.equals("press")) { sendTap(InputDevice.SOURCE_TRACKBALL, 0.0f, 0.0f); return; } else if (subcommand.equals("roll")) { if (args.length == 4) { sendMove(InputDevice.SOURCE_TRACKBALL, Float.parseFloat(args[2]), Float.parseFloat(args[3])); return; } } } } else { System.err.println("Error: Unknown command: " + command); showUsage(); return; } } catch (NumberFormatException ex) { } System.err.println("Error: Invalid arguments for command: " + command); showUsage(); }
这里贴的不是完整的code,如果敢兴趣就可以查看研究。
在串口终端输入input命令就会出现input命令的相关用法,
usage: input ... input text <string> input keyevent <key code number or name> input [touchscreen|touchpad] tap <x> <y> input [touchscreen|touchpad] swipe <x1> <y1> <x2> <y2> input trackball press input trackball roll <dx> <dy>
显示的这部分实现同样在input.java中:
private void showUsage() { System.err.println("usage: input ..."); System.err.println(" input text <string>"); System.err.println(" input keyevent <key code number or name>"); System.err.println(" input [touchscreen|touchpad] tap <x> <y>"); System.err.println(" input [touchscreen|touchpad] swipe <x1> <y1> <x2> <y2>"); System.err.println(" input trackball press"); System.err.println(" input trackball roll <dx> <dy>"); }
我们这里以input text为例子总结一下:
从上面run的函数中可以得知,针对text是这样处理的:
if (command.equals("text")) { if (args.length == 2) { sendText(args[1]); return; } }
会调用sendText这个函数:
private void sendText(String text) { StringBuffer buff = new StringBuffer(text); boolean escapeFlag = false; for (int i=0; i<buff.length(); i++) { if (escapeFlag) { escapeFlag = false; if (buff.charAt(i) == 's') { buff.setCharAt(i, ' '); buff.deleteCharAt(--i); } } if (buff.charAt(i) == '%') { escapeFlag = true; } } char[] chars = buff.toString().toCharArray(); KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD); KeyEvent[] events = kcm.getEvents(chars); for(int i = 0; i < events.length; i++) { injectKeyEvent(events[i]); } }
最终会调用injectKeyEvent函数:
private void injectKeyEvent(KeyEvent event) { Log.i(TAG, "injectKeyEvent: " + event); InputManager.getInstance().injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH); }
这里的 InputManger显然从import中可以得知是:
import android.hardware.input.InputManager;
去framework/base/core/java/android.hardware.input中去查找这个文件,并得知injectInputEvent函数:
public boolean injectInputEvent(InputEvent event, int mode) { if (event == null) { throw new IllegalArgumentException("event must not be null"); } if (mode != INJECT_INPUT_EVENT_MODE_ASYNC && mode != INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH && mode != INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT) { throw new IllegalArgumentException("mode is invalid"); } try { return mIm.injectInputEvent(event, mode); } catch (RemoteException ex) { return false; } }
return mIm.injectInputEvent(event, mode);是需要调用service中的接口,然后回去调用native的接口,一直到底层。
具体实现过程,以后会补充上~~
这样,就可以在编辑框中通过input命令输入text,而不需要一个一个去点击。
顺便说一下小技巧:
1. input text text之后,文本框上会出现text字符串,但是实际上想输入test,只需要input keyevent KEYCODE_DEL或者input keyevent 67就可以退格了。