2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.commands.monkey;
19 import android.app.IActivityManager;
20 import android.os.RemoteException;
21 import android.view.IWindowManager;
22 import android.view.KeyEvent;
26 public class MonkeyKeyEvent extends MonkeyEvent {
27 private long mDownTime = -1;
28 private int mMetaState = -1;
29 private int mAction = -1;
30 private int mKeyCode = -1;
31 private int mScancode = -1;
32 private int mRepeatCount = -1;
33 private int mDeviceId = -1;
34 private long mEventTime = -1;
36 public MonkeyKeyEvent(int action, int keycode) {
37 super(EVENT_TYPE_KEY);
42 public MonkeyKeyEvent(long downTime, long eventTime, int action,
43 int code, int repeat, int metaState,
44 int device, int scancode) {
45 super(EVENT_TYPE_KEY);
49 mMetaState = metaState;
51 mRepeatCount = repeat;
54 mEventTime = eventTime;
57 public int getKeyCode() {
61 public int getAction() {
65 public long getDownTime() {
69 public long getEventTime() {
73 public void setDownTime(long downTime) {
77 public void setEventTime(long eventTime) {
78 mEventTime = eventTime;
82 * @return the key event
84 private KeyEvent getEvent() {
86 return new KeyEvent(mAction, mKeyCode);
90 return new KeyEvent(mDownTime, mEventTime, mAction,
91 mKeyCode, mRepeatCount, mMetaState, mDeviceId, mScancode);
95 public boolean isThrottlable() {
96 return (getAction() == KeyEvent.ACTION_UP);
100 public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) {
103 if (mAction == KeyEvent.ACTION_UP) {
106 note = "ACTION_DOWN";
110 System.out.println(":SendKey (" + note + "): "
112 + MonkeySourceRandom.getKeyName(mKeyCode));
113 } catch (ArrayIndexOutOfBoundsException e) {
114 System.out.println(":SendKey (ACTION_UP): "
115 + mKeyCode + " // Unknown key event");
121 if (!iwm.injectKeyEvent(getEvent(), false)) {
122 return MonkeyEvent.INJECT_FAIL;
124 } catch (RemoteException ex) {
125 return MonkeyEvent.INJECT_ERROR_REMOTE_EXCEPTION;
128 return MonkeyEvent.INJECT_SUCCESS;