FileDocCategorySizeDatePackage
Input.javaAPI DocAndroid 1.5 API4496Wed May 06 22:41:54 BST 2009com.android.commands.input

Input

public class Input extends Object
Command that sends key events to the device, either by their keycode, or by desired character output.

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)
Command-line entry point.

param
args The command-line arguments

        (new Input()).run(args);
    
private voidrun(java.lang.String[] args)

        if (args.length < 1) {
            showUsage();
            return;
        }

        String command = args[0];

        if (command.equals("text")) {
            sendText(args[1]);
        } else if (command.equals("keyevent")) {
            sendKeyEvent(args[1]);
        } else if (command.equals("motionevent")) {
            System.err.println("Error: motionevent not yet supported.");
            return;
        }
        else {
            System.err.println("Error: Unknown command: " + command);
            showUsage();
            return;
        }
    
private voidsendKeyEvent(java.lang.String event)
Send a single key event.

param
event is a string representing the keycode of the key event you want to execute.

        int eventCode = Integer.parseInt(event);
        long now = SystemClock.uptimeMillis();
        Log.i("SendKeyEvent", event);
        try {
            KeyEvent down = new KeyEvent(now, now, KeyEvent.ACTION_DOWN, eventCode, 0);
            KeyEvent up = new KeyEvent(now, now, KeyEvent.ACTION_UP, eventCode, 0);
            (IWindowManager.Stub
                .asInterface(ServiceManager.getService("window")))
                .injectKeyEvent(down, true);
            (IWindowManager.Stub
                .asInterface(ServiceManager.getService("window")))
                .injectKeyEvent(up, true);
        } catch (RemoteException e) {
            Log.i("Input", "DeadOjbectException");
        }
    
private voidsendMotionEvent(long downTime, int action, float x, float y, float pressure, float size)

    
private voidsendText(java.lang.String text)
Convert the characters of string text into key event's and send to device.

param
text is a string of characters you want to input to the device.


        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 mKeyCharacterMap = KeyCharacterMap.
            load(KeyCharacterMap.BUILT_IN_KEYBOARD);

        KeyEvent[] events = mKeyCharacterMap.getEvents(chars);

        for(int i = 0; i < events.length; i++) {
            KeyEvent event = events[i];
            Log.i("SendKeyEvent", Integer.toString(event.getKeyCode()));
            try {
                (IWindowManager.Stub
                    .asInterface(ServiceManager.getService("window")))
                    .injectKeyEvent(event, true);
            } catch (RemoteException e) {
                Log.i("Input", "DeadOjbectException");
            }
        }
    
private voidshowUsage()

        System.err.println("usage: input [text|keyevent]");
        System.err.println("       input text <string>");
        System.err.println("       input keyevent <event_code>");