1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.commands.input; 18 19import android.hardware.input.InputManager; 20import android.os.SystemClock; 21import android.util.Log; 22import android.view.InputDevice; 23import android.view.KeyCharacterMap; 24import android.view.KeyEvent; 25import android.view.MotionEvent; 26 27/** 28 * Command that sends key events to the device, either by their keycode, or by 29 * desired character output. 30 */ 31 32public class Input { 33 private static final String TAG = "Input"; 34 35 /** 36 * Command-line entry point. 37 * 38 * @param args The command-line arguments 39 */ 40 public static void main(String[] args) { 41 (new Input()).run(args); 42 } 43 44 private void run(String[] args) { 45 if (args.length < 1) { 46 showUsage(); 47 return; 48 } 49 50 String command = args[0]; 51 52 try { 53 if (command.equals("text")) { 54 if (args.length == 2) { 55 sendText(args[1]); 56 return; 57 } 58 } else if (command.equals("keyevent")) { 59 if (args.length == 2) { 60 int keyCode = KeyEvent.keyCodeFromString(args[1]); 61 if (keyCode == KeyEvent.KEYCODE_UNKNOWN) { 62 keyCode = KeyEvent.keyCodeFromString("KEYCODE_" + args[1]); 63 } 64 sendKeyEvent(keyCode); 65 return; 66 } 67 } else if (command.equals("tap")) { 68 if (args.length == 3) { 69 sendTap(Float.parseFloat(args[1]), Float.parseFloat(args[2])); 70 return; 71 } 72 } else if (command.equals("swipe")) { 73 if (args.length == 5) { 74 sendSwipe(Float.parseFloat(args[1]), Float.parseFloat(args[2]), 75 Float.parseFloat(args[3]), Float.parseFloat(args[4])); 76 return; 77 } 78 } else { 79 System.err.println("Error: Unknown command: " + command); 80 showUsage(); 81 return; 82 } 83 } catch (NumberFormatException ex) { 84 } 85 System.err.println("Error: Invalid arguments for command: " + command); 86 showUsage(); 87 } 88 89 /** 90 * Convert the characters of string text into key event's and send to 91 * device. 92 * 93 * @param text is a string of characters you want to input to the device. 94 */ 95 private void sendText(String text) { 96 97 StringBuffer buff = new StringBuffer(text); 98 99 boolean escapeFlag = false; 100 for (int i=0; i<buff.length(); i++) { 101 if (escapeFlag) { 102 escapeFlag = false; 103 if (buff.charAt(i) == 's') { 104 buff.setCharAt(i, ' '); 105 buff.deleteCharAt(--i); 106 } 107 } 108 if (buff.charAt(i) == '%') { 109 escapeFlag = true; 110 } 111 } 112 113 char[] chars = buff.toString().toCharArray(); 114 115 KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD); 116 KeyEvent[] events = kcm.getEvents(chars); 117 for(int i = 0; i < events.length; i++) { 118 injectKeyEvent(events[i]); 119 } 120 } 121 122 private void sendKeyEvent(int keyCode) { 123 long now = SystemClock.uptimeMillis(); 124 injectKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_DOWN, keyCode, 0, 0, 125 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD)); 126 injectKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_UP, keyCode, 0, 0, 127 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD)); 128 } 129 130 private void sendTap(float x, float y) { 131 long now = SystemClock.uptimeMillis(); 132 injectPointerEvent(MotionEvent.obtain(now, now, MotionEvent.ACTION_DOWN, x, y, 0)); 133 injectPointerEvent(MotionEvent.obtain(now, now, MotionEvent.ACTION_UP, x, y, 0)); 134 } 135 136 private void sendSwipe(float x1, float y1, float x2, float y2) { 137 final int NUM_STEPS = 11; 138 long now = SystemClock.uptimeMillis(); 139 injectPointerEvent(MotionEvent.obtain(now, now, MotionEvent.ACTION_DOWN, x1, y1, 0)); 140 for (int i = 1; i < NUM_STEPS; i++) { 141 float alpha = (float)i / NUM_STEPS; 142 injectPointerEvent(MotionEvent.obtain(now, now, MotionEvent.ACTION_MOVE, 143 lerp(x1, x2, alpha), lerp(y1, y2, alpha), 0)); 144 } 145 injectPointerEvent(MotionEvent.obtain(now, now, MotionEvent.ACTION_UP, x2, y2, 0)); 146 } 147 148 private void injectKeyEvent(KeyEvent event) { 149 Log.i(TAG, "InjectKeyEvent: " + event); 150 InputManager.getInstance().injectInputEvent(event, 151 InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH); 152 } 153 154 private void injectPointerEvent(MotionEvent event) { 155 event.setSource(InputDevice.SOURCE_TOUCHSCREEN); 156 Log.i("Input", "InjectPointerEvent: " + event); 157 InputManager.getInstance().injectInputEvent(event, 158 InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH); 159 } 160 161 private static final float lerp(float a, float b, float alpha) { 162 return (b - a) * alpha + a; 163 } 164 165 private void showUsage() { 166 System.err.println("usage: input ..."); 167 System.err.println(" input text <string>"); 168 System.err.println(" input keyevent <key code number or name>"); 169 System.err.println(" input tap <x> <y>"); 170 System.err.println(" input swipe <x1> <y1> <x2> <y2>"); 171 } 172} 173