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.os.RemoteException; 20import android.os.ServiceManager; 21import android.os.SystemClock; 22import android.util.Log; 23import android.view.IWindowManager; 24import android.view.KeyCharacterMap; 25import android.view.KeyEvent; 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 34 /** 35 * Command-line entry point. 36 * 37 * @param args The command-line arguments 38 */ 39 public static void main(String[] args) { 40 (new Input()).run(args); 41 } 42 43 private void run(String[] args) { 44 if (args.length < 1) { 45 showUsage(); 46 return; 47 } 48 49 String command = args[0]; 50 51 if (command.equals("text")) { 52 sendText(args[1]); 53 } else if (command.equals("keyevent")) { 54 sendKeyEvent(args[1]); 55 } else if (command.equals("motionevent")) { 56 System.err.println("Error: motionevent not yet supported."); 57 return; 58 } 59 else { 60 System.err.println("Error: Unknown command: " + command); 61 showUsage(); 62 return; 63 } 64 } 65 66 /** 67 * Convert the characters of string text into key event's and send to 68 * device. 69 * 70 * @param text is a string of characters you want to input to the device. 71 */ 72 73 private void sendText(String text) { 74 75 StringBuffer buff = new StringBuffer(text); 76 77 boolean escapeFlag = false; 78 for (int i=0; i<buff.length(); i++) { 79 if (escapeFlag) { 80 escapeFlag = false; 81 if (buff.charAt(i) == 's') { 82 buff.setCharAt(i, ' '); 83 buff.deleteCharAt(--i); 84 } 85 } 86 if (buff.charAt(i) == '%') { 87 escapeFlag = true; 88 } 89 } 90 91 char[] chars = buff.toString().toCharArray(); 92 93 KeyCharacterMap mKeyCharacterMap = KeyCharacterMap. 94 load(KeyCharacterMap.BUILT_IN_KEYBOARD); 95 96 KeyEvent[] events = mKeyCharacterMap.getEvents(chars); 97 98 for(int i = 0; i < events.length; i++) { 99 KeyEvent event = events[i]; 100 Log.i("SendKeyEvent", Integer.toString(event.getKeyCode())); 101 try { 102 (IWindowManager.Stub 103 .asInterface(ServiceManager.getService("window"))) 104 .injectKeyEvent(event, true); 105 } catch (RemoteException e) { 106 Log.i("Input", "DeadOjbectException"); 107 } 108 } 109 } 110 111 /** 112 * Send a single key event. 113 * 114 * @param event is a string representing the keycode of the key event you 115 * want to execute. 116 */ 117 private void sendKeyEvent(String event) { 118 int eventCode = Integer.parseInt(event); 119 long now = SystemClock.uptimeMillis(); 120 Log.i("SendKeyEvent", event); 121 try { 122 KeyEvent down = new KeyEvent(now, now, KeyEvent.ACTION_DOWN, eventCode, 0); 123 KeyEvent up = new KeyEvent(now, now, KeyEvent.ACTION_UP, eventCode, 0); 124 (IWindowManager.Stub 125 .asInterface(ServiceManager.getService("window"))) 126 .injectKeyEvent(down, true); 127 (IWindowManager.Stub 128 .asInterface(ServiceManager.getService("window"))) 129 .injectKeyEvent(up, true); 130 } catch (RemoteException e) { 131 Log.i("Input", "DeadOjbectException"); 132 } 133 } 134 135 private void sendMotionEvent(long downTime, int action, float x, float y, 136 float pressure, float size) { 137 } 138 139 private void showUsage() { 140 System.err.println("usage: input [text|keyevent]"); 141 System.err.println(" input text <string>"); 142 System.err.println(" input keyevent <event_code>"); 143 } 144} 145