Example usage for android.view KeyEvent getDeviceId

List of usage examples for android.view KeyEvent getDeviceId

Introduction

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

Prototype

@Override
public final int getDeviceId() 

Source Link

Usage

From source file:org.uoyabause.android.tv.GameSelectActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {

    InputDevice dev = InputDevice.getDevice(event.getDeviceId());
    if (dev != null && dev.getName().contains("HuiJia")) {
        if (event.getKeyCode() > 200) {
            return true;
        }/*from   ww w . ja va  2  s  .  c  om*/

    }
    return super.dispatchKeyEvent(event);
}

From source file:pl.pelotasplus.pongtv.PongFragment.java

@Override
public void onKeyDown(int keyCode, KeyEvent event) {
    PongView.Player player;//from   ww w .j a  v  a 2s  .  c om

    if (event.getDeviceId() % 2 == 0) {
        player = PongView.Player.LEFT;
    } else {
        player = PongView.Player.RIGHT;
    }
    Log.e("tag", "keyCode: " + keyCode);

    if (keyCode == KeyEvent.KEYCODE_DPAD_UP || keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
        pongView.changePlayerPosition(PongView.Player.LEFT, keyCode);
    } else if (keyCode == 23) {
        pongView.changePlayerPosition(PongView.Player.RIGHT, KeyEvent.KEYCODE_DPAD_DOWN);
    } else if (keyCode == 62) {
        pongView.changePlayerPosition(PongView.Player.RIGHT, KeyEvent.KEYCODE_DPAD_UP);
    }
}

From source file:org.ulteo.ovd.AndRdpActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    AudioManager audioManager;/*from   w w w.ja v  a  2 s  .  c  o  m*/

    if (Config.DEBUG)
        Log.i(Config.TAG, "AndRdpActivity.dispatchKeyEvent " + event);

    // Disable keypress if it is from a mouse or touchpad.
    if (event.getDeviceId() >= 0) {
        InputDevice dev = InputDevice.getDevice(event.getDeviceId());
        if (dev.getSources() == InputDevice.SOURCE_MOUSE || dev.getSources() == InputDevice.SOURCE_TOUCHPAD)
            return true;
    }

    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_MENU:
            OnThreeFingers(null);
            return true;
        case KeyEvent.KEYCODE_BACK:
            Log.i(Config.TAG, "Back key pressed");
            askLogoutDialog();
            return true;
        case KeyEvent.KEYCODE_VOLUME_UP:
            audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE,
                    AudioManager.FLAG_SHOW_UI);
            return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER,
                    AudioManager.FLAG_SHOW_UI);
            return true;
        }
    } else if (event.getAction() == KeyEvent.ACTION_UP) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_MENU:
        case KeyEvent.KEYCODE_BACK:
        case KeyEvent.KEYCODE_VOLUME_UP:
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            return true;
        }
    }

    if (isLoggedIn() && rdp.dispatchKeyEvent(event))
        return true;

    return super.dispatchKeyEvent(event);
}

From source file:android.support.v7.app.ToolbarActionBar.java

@Override
public boolean onKeyShortcut(int keyCode, KeyEvent ev) {
    Menu menu = getMenu();/* w  ww  .  j  ava 2s .co m*/
    if (menu != null) {
        final KeyCharacterMap kmap = KeyCharacterMap
                .load(ev != null ? ev.getDeviceId() : KeyCharacterMap.VIRTUAL_KEYBOARD);
        menu.setQwertyMode(kmap.getKeyboardType() != KeyCharacterMap.NUMERIC);
        menu.performShortcut(keyCode, ev, 0);
    }
    // This action bar always returns true for handling keyboard shortcuts.
    // This will block the window from preparing a temporary panel to handle
    // keyboard shortcuts.
    return true;
}

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

/**
 * Constructs the JavaScript string that triggers the event in Cordova WebView
 * @param keyCode/*  w  ww  . j  a  v  a 2s .  c o  m*/
 * @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 "";
    }
}

From source file:com.goodhustle.ouyaunitybridge.OuyaUnityActivity.java

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    boolean handled = false;
    int playerNum = OuyaController.getPlayerNumByDeviceId(event.getDeviceId());
    if (playerNum != DEVICE_NOT_OUYACONTROLLER_COMPATIBLE) {
        // A special MENU KeyUp event is triggered at the same time as its KeyDown event
        // in the OUYA SDK. We tell the Unity layer to handle this specially and emulate
        // a 1-frame menu button press.
        if (keyCode == OuyaController.BUTTON_MENU) {
            UnityPlayer.UnitySendMessage("OuyaBridge", "MenuButtonPressed", "" + playerNum);
            return handled || super.onKeyDown(keyCode, event);
        }//from   w  ww. java2 s  .c o m

        // Pass to OuyaController first, then process.
        handled = OuyaController.onKeyDown(keyCode, event);
        if (mPaused)
            return handled || super.onKeyDown(keyCode, event);
        try {
            playerNum = OuyaController.getPlayerNumByDeviceId(event.getDeviceId());
            ControllerState data = playerStates[playerNum];
            OuyaController c = OuyaController.getControllerByPlayer(playerNum);
            if (data != null) {
                data.ButtonO = c.getButton(OuyaController.BUTTON_O);
                data.ButtonU = c.getButton(OuyaController.BUTTON_U);
                data.ButtonY = c.getButton(OuyaController.BUTTON_Y);
                data.ButtonA = c.getButton(OuyaController.BUTTON_A);
                data.ButtonDPD = c.getButton(OuyaController.BUTTON_DPAD_DOWN);
                data.ButtonDPU = c.getButton(OuyaController.BUTTON_DPAD_UP);
                data.ButtonDPL = c.getButton(OuyaController.BUTTON_DPAD_LEFT);
                data.ButtonDPR = c.getButton(OuyaController.BUTTON_DPAD_RIGHT);
                data.ButtonL1 = c.getButton(OuyaController.BUTTON_L1);
                data.ButtonL2 = c.getButton(OuyaController.BUTTON_L2);
                data.ButtonL3 = c.getButton(OuyaController.BUTTON_L3);
                data.ButtonR1 = c.getButton(OuyaController.BUTTON_R1);
                data.ButtonR2 = c.getButton(OuyaController.BUTTON_R2);
                data.ButtonR3 = c.getButton(OuyaController.BUTTON_R3);
            }
        } catch (Exception e) {
            Log.w(LOG_TAG, "Exception occurred getting controller state for player " + playerNum + ": "
                    + e.toString());
        }
    }
    return handled || mUnityPlayer.onKeyDown(keyCode, event);
}

From source file:com.goodhustle.ouyaunitybridge.OuyaUnityActivity.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Pass to OuyaController first, then process.
    boolean handled = false;
    int playerNum = OuyaController.getPlayerNumByDeviceId(event.getDeviceId());
    if (playerNum != DEVICE_NOT_OUYACONTROLLER_COMPATIBLE) {
        handled = OuyaController.onKeyDown(keyCode, event);
        if (mPaused)
            return handled || super.onKeyDown(keyCode, event);
        try {/* www.  j  av  a 2  s.com*/
            ControllerState data = playerStates[playerNum];
            OuyaController c = OuyaController.getControllerByPlayer(playerNum);
            if (data != null) {
                data.ButtonO = c.getButton(OuyaController.BUTTON_O);
                data.ButtonU = c.getButton(OuyaController.BUTTON_U);
                data.ButtonY = c.getButton(OuyaController.BUTTON_Y);
                data.ButtonA = c.getButton(OuyaController.BUTTON_A);
                data.ButtonDPD = c.getButton(OuyaController.BUTTON_DPAD_DOWN);
                data.ButtonDPU = c.getButton(OuyaController.BUTTON_DPAD_UP);
                data.ButtonDPL = c.getButton(OuyaController.BUTTON_DPAD_LEFT);
                data.ButtonDPR = c.getButton(OuyaController.BUTTON_DPAD_RIGHT);
                data.ButtonL1 = c.getButton(OuyaController.BUTTON_L1);
                data.ButtonL2 = c.getButton(OuyaController.BUTTON_L2);
                data.ButtonL3 = c.getButton(OuyaController.BUTTON_L3);
                data.ButtonR1 = c.getButton(OuyaController.BUTTON_R1);
                data.ButtonR2 = c.getButton(OuyaController.BUTTON_R2);
                data.ButtonR3 = c.getButton(OuyaController.BUTTON_R3);
            }
        } catch (Exception e) {
            Log.w(LOG_TAG, "Exception occurred getting controller state for player " + playerNum + ": "
                    + e.toString());
        }
    }
    return handled || mUnityPlayer.onKeyDown(keyCode, event);
}

From source file:info.bartowski.easteregg.MLand.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent ev) {
    L("keyDown: %d", keyCode);
    switch (keyCode) {
    case KeyEvent.KEYCODE_DPAD_CENTER:
    case KeyEvent.KEYCODE_DPAD_UP:
    case KeyEvent.KEYCODE_SPACE:
    case KeyEvent.KEYCODE_ENTER:
    case KeyEvent.KEYCODE_BUTTON_A:
        int player = getControllerPlayer(ev.getDeviceId());
        poke(player);//from w w w . j a  va 2 s .c  om
        return true;
    }
    return false;
}

From source file:info.bartowski.easteregg.MLand.java

@Override
public boolean onKeyUp(int keyCode, KeyEvent ev) {
    L("keyDown: %d", keyCode);
    switch (keyCode) {
    case KeyEvent.KEYCODE_DPAD_CENTER:
    case KeyEvent.KEYCODE_DPAD_UP:
    case KeyEvent.KEYCODE_SPACE:
    case KeyEvent.KEYCODE_ENTER:
    case KeyEvent.KEYCODE_BUTTON_A:
        int player = getControllerPlayer(ev.getDeviceId());
        unpoke(player);/*from  w  ww .j a v  a  2 s.  c  o m*/
        return true;
    }
    return false;
}

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

/**
 * @param cordova The context of the main Activity.
 * @param webView The associated CordovaWebView.
 *//*  ww  w.j  a  v  a  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");
}