Example usage for android.view KeyEvent keyCodeToString

List of usage examples for android.view KeyEvent keyCodeToString

Introduction

In this page you can find the example usage for android.view KeyEvent keyCodeToString.

Prototype

public static String keyCodeToString(int keyCode) 

Source Link

Document

Returns a string that represents the symbolic name of the specified keycode such as "KEYCODE_A", "KEYCODE_DPAD_UP", or an equivalent numeric constant such as "1001" if unknown.

Usage

From source file:com.vladstirbu.cordova.Gamepad.java

/**
 * @param cordova The context of the main Activity.
 * @param webView The associated CordovaWebView.
 *//*from   ww w  .ja va 2 s . c  o  m*/
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    super.initialize(cordova, webView);

    this.map.put("KEYCODE_BUTTON_A", 0);
    this.map.put("KEYCODE_BUTTON_B", 1);
    this.map.put("KEYCODE_BUTTON_Y", 3);
    this.map.put("KEYCODE_BUTTON_X", 2);
    this.map.put("KEYCODE_BUTTON_L1", 4);
    this.map.put("KEYCODE_BUTTON_R1", 5);
    this.map.put("KEYCODE_BUTTON_L2", 6);
    this.map.put("KEYCODE_BUTTON_R2", 7);
    this.map.put("KEYCODE_SPACE", 8);
    this.map.put("KEYCODE_SELECT", 8);
    this.map.put("KEYCODE_ENTER", 9);
    this.map.put("KEYCODE_START", 9);
    this.map.put("KEYCODE_BUTTON_THUMBL", 10);
    this.map.put("KEYCODE_BUTTON_THUMBR", 11);
    this.map.put("KEYCODE_DPAD_UP", 12);
    this.map.put("KEYCODE_DPAD_DOWN", 13);
    this.map.put("KEYCODE_DPAD_LEFT", 14);
    this.map.put("KEYCODE_DPAD_RIGHT", 15);
    this.map.put("KEYCODE_BACK", 16);
    this.map.put("KEYCODE_BUTTON_MODE", 16);

    this.webView.setFocusable(true);
    this.webView.setFocusableInTouchMode(true);
    this.webView.requestFocus();

    this.webView.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            //Log.v("Keyboards", String.valueOf(InputDevice.getDeviceIds().length));
            //Log.v("Input", InputDevice.getDevice(1).getName());
            //Log.v("Input", String.valueOf(InputDevice.getDevice(1).getSources()));

            //Log.v("Device id", String.valueOf(event.getDeviceId()));
            //Log.v("Source id", String.valueOf(event.getSource()));
            //Log.v("Input device", String.valueOf(InputDevice.getDevice(event.getDeviceId()).getName()));
            Log.v("KEY", String.valueOf(event.getScanCode()));
            Log.v("KEY", KeyEvent.keyCodeToString(keyCode));
            //Log.v("GamePad", String.valueOf(KeyEvent.isGamepadButton(keyCode)));

            String jsStr = jsString(keyCode, event);
            if (!jsStr.isEmpty()) {
                self.webView.sendJavascript(jsStr);
            }
            return true;
        }
    });
    this.webView.setOnGenericMotionListener(new OnGenericMotionListener() {
        public boolean onGenericMotion(View v, MotionEvent event) {
            if (event.isFromSource(InputDevice.SOURCE_CLASS_JOYSTICK)) {
                if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    // process the joystick movement...
                    JSONObject data = new JSONObject();
                    JSONArray axes = new JSONArray();
                    try {
                        axes.put(event.getAxisValue(MotionEvent.AXIS_X));
                        axes.put(event.getAxisValue(MotionEvent.AXIS_Y));
                        axes.put(event.getAxisValue(MotionEvent.AXIS_Z));
                        axes.put(event.getAxisValue(MotionEvent.AXIS_RZ));
                        data.put("deviceId", event.getDeviceId());
                        data.put("axes", axes);
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    self.webView.sendJavascript(
                            "cordova.fireWindowEvent('GamepadMotion', " + data.toString() + ");");

                    data = new JSONObject();
                    try {
                        data.put("deviceId", event.getDeviceId());
                        data.put("button", 6);
                        data.put("value", event.getAxisValue(MotionEvent.AXIS_LTRIGGER));
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    self.webView.sendJavascript(
                            "cordova.fireWindowEvent('GamepadMotion', " + data.toString() + ");");

                    data = new JSONObject();
                    try {
                        data.put("deviceId", event.getDeviceId());
                        data.put("button", 7);
                        data.put("value", event.getAxisValue(MotionEvent.AXIS_RTRIGGER));
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    self.webView.sendJavascript(
                            "cordova.fireWindowEvent('GamepadMotion', " + data.toString() + ");");

                    float hatX = event.getAxisValue(MotionEvent.AXIS_HAT_X);
                    float hatY = event.getAxisValue(MotionEvent.AXIS_HAT_Y);
                    try {
                        data = new JSONObject();
                        data.put("deviceId", event.getDeviceId());
                        data.put("button", 14);
                        data.put("value", hatX < 0.0f);
                        self.webView.sendJavascript(
                                "cordova.fireWindowEvent('GamepadMotion', " + data.toString() + ");");
                        data = new JSONObject();
                        data.put("deviceId", event.getDeviceId());
                        data.put("button", 15);
                        data.put("value", hatX > 0.0f);
                        self.webView.sendJavascript(
                                "cordova.fireWindowEvent('GamepadMotion', " + data.toString() + ");");
                    } catch (JSONException e) {
                    }
                    try {
                        data = new JSONObject();
                        data.put("deviceId", event.getDeviceId());
                        data.put("button", 12);
                        data.put("value", hatY < 0.0f);
                        self.webView.sendJavascript(
                                "cordova.fireWindowEvent('GamepadMotion', " + data.toString() + ");");
                        data = new JSONObject();
                        data.put("deviceId", event.getDeviceId());
                        data.put("button", 13);
                        data.put("value", hatY > 0.0f);
                        self.webView.sendJavascript(
                                "cordova.fireWindowEvent('GamepadMotion', " + data.toString() + ");");
                    } catch (JSONException e) {
                    }
                }
                Log.v("MOTION", event.toString());
                return true;
            }
            return false;
        }
    });

    Log.v("GamepadButtons", "initialized");
}

From source file:com.vladstirbu.cordova.Gamepad.java

/**
 * Constructs the JavaScript string that triggers the event in Cordova WebView
 * @param keyCode/*from w ww .  j  av a 2s  .c  om*/
 * @param event
 * @return
 */
private String jsString(int keyCode, KeyEvent event) {
    String eventType;
    JSONObject data = new JSONObject();

    if (this.map.containsKey(KeyEvent.keyCodeToString(keyCode))) {

        if (event.getAction() == 0) {
            eventType = "GamepadButtonDown";
            this.buttons[this.map.get(KeyEvent.keyCodeToString(keyCode))] = 1;
        } else {
            eventType = "GamepadButtonUp";
            this.buttons[this.map.get(KeyEvent.keyCodeToString(keyCode))] = 0;
        }

        try {
            data.put("button", this.map.get(KeyEvent.keyCodeToString(keyCode)));
            data.put("deviceId", event.getDeviceId());
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return "cordova.fireWindowEvent('" + eventType + "', " + data.toString() + ");";
    } else {
        return "";
    }
}