package io.appium.android.bootstrap.handler;

import io.appium.android.bootstrap.AndroidCommand;
import io.appium.android.bootstrap.AndroidCommandResult;
import io.appium.android.bootstrap.CommandHandler;

import java.util.Hashtable;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.uiautomator.core.UiDevice;

/**
 * This handler is used to PressKeyCode.
 * 
 */
public class PressKeyCode extends CommandHandler {
  public Integer keyCode;
  public Integer metaState;

  /*
   * @param command The {@link AndroidCommand} used for this handler.
   * 
   * @return {@link AndroidCommandResult}
   * 
   * @throws JSONException
   * 
   * @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.
   * bootstrap.AndroidCommand)
   */
  @Override
  public AndroidCommandResult execute(final AndroidCommand command)
      throws JSONException {
    try {
      final Hashtable<String, Object> params = command.params();
      keyCode = (Integer) params.get("keycode");

      if (params.get("metastate") != JSONObject.NULL) {
        metaState = (Integer) params.get("metastate");
        UiDevice.getInstance().pressKeyCode(keyCode, metaState);
      } else {
        UiDevice.getInstance().pressKeyCode(keyCode);
      }

      return getSuccessResult(true);
    } catch (final Exception e) {
      return getErrorResult(e.getMessage());
    }
  }
}

@@ -382,18 +382,17 @@
 Code.%0A *
- 
 %0A
+
  */%0Apubl
@@ -568,25 +568,24 @@
 andler.%0A   *
- 
 %0A   * @retur
@@ -615,25 +615,24 @@
 Result%7D%0A   *
- 
 %0A   * @throw
@@ -651,17 +651,16 @@
 ion%0A   *
- 
 %0A   * @s
@@ -963,50 +963,321 @@
     
-keyCode = (Integer) params.get(%22k
+Object kc = params.get(%22keycode%22);%0A      if (kc instanceof Integer) %7B%0A        keyCode = (Integer) kc;%0A      %7D else if (kc instanceof String) %7B%0A        keyCode = Integer.parseInt((String) kc);%0A      %7D else %7B%0A        throw new IllegalArgumentException(%22K
 eycode
-%22);
+ of type %22 + kc.getClass() + %22not supported.%22);%0A      %7D
 %0A%0A  

Allow String keycode