List of usage examples for android.view MotionEvent AXIS_HAT_Y
int AXIS_HAT_Y
To view the source code for android.view MotionEvent AXIS_HAT_Y.
Click Source Link
From source file:com.vladstirbu.cordova.Gamepad.java
/** * @param cordova The context of the main Activity. * @param webView The associated CordovaWebView. *///from w ww .j a va 2 s . com @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:org.gearvrf.controls.Worm.java
public void interactWithDPad() { if (!ScaleWorm.animPlaying) { if (GamepadInput.getCenteredAxis(MotionEvent.AXIS_HAT_X) >= 1 || GamepadInput.getCenteredAxis(MotionEvent.AXIS_X) >= 1 || GamepadInput.getCenteredAxis(MotionEvent.AXIS_RX) >= 1) { rotateAroundCamera(.1f, -5f); rotateWorm(MovementDirection.Right); }//from w w w. j a v a2 s.c o m if (GamepadInput.getCenteredAxis(MotionEvent.AXIS_HAT_X) <= -1 || GamepadInput.getCenteredAxis(MotionEvent.AXIS_X) <= -1 || GamepadInput.getCenteredAxis(MotionEvent.AXIS_RX) <= -1) { rotateAroundCamera(.1f, 5f); rotateWorm(MovementDirection.Left); } if (GamepadInput.getCenteredAxis(MotionEvent.AXIS_HAT_Y) >= 1 || GamepadInput.getCenteredAxis(MotionEvent.AXIS_Y) >= 1 || GamepadInput.getCenteredAxis(MotionEvent.AXIS_RY) >= 1) { moveAlongCameraVector(.1f, -.225f); rotateWorm(MovementDirection.Down); } if (GamepadInput.getCenteredAxis(MotionEvent.AXIS_HAT_Y) <= -1 || GamepadInput.getCenteredAxis(MotionEvent.AXIS_Y) <= -1 || GamepadInput.getCenteredAxis(MotionEvent.AXIS_RY) <= -1) { moveAlongCameraVector(.1f, .225f); rotateWorm(MovementDirection.Up); } } }
From source file:com.amazon.appstream.fireclient.FireClientActivity.java
/** * A "generic motion event" includes joystick and mouse motion * when the mouse button isn't down. In our simple sample, we're * not handling the joystick, but this is where any such code * would live.//from w w w .ja va 2s. c om * * This will only ever be called in HONEYCOMB_MR1 (12) or later, so I'm marking the * function using \@TargetApi to allow it to call the super. */ @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1) @Override public boolean dispatchGenericMotionEvent(MotionEvent event) { if (event.getSource() == InputDevice.SOURCE_MOUSE) { event.getPointerCoords(0, mCoordHolder); switch (event.getAction()) { case MotionEvent.ACTION_HOVER_MOVE: AppStreamInterface.mouseEvent((int) mCoordHolder.x, (int) mCoordHolder.y, 0); break; default: return super.dispatchGenericMotionEvent(event); } return true; } else if (event.getSource() == InputDevice.SOURCE_JOYSTICK) { Log.v(TAG, "Joystick event:" + event.toString()); if (!mInitializedJoystick) { mInitializedJoystick = true; InputDevice joystick = InputDevice.getDevice(event.getDeviceId()); InputDevice.MotionRange lThumbX = joystick.getMotionRange(MotionEvent.AXIS_X, InputDevice.SOURCE_JOYSTICK); InputDevice.MotionRange lThumbY = joystick.getMotionRange(MotionEvent.AXIS_Y, InputDevice.SOURCE_JOYSTICK); InputDevice.MotionRange rThumbX = joystick.getMotionRange(MotionEvent.AXIS_Z); InputDevice.MotionRange rThumbY = joystick.getMotionRange(MotionEvent.AXIS_RZ); InputDevice.MotionRange rTrigger = joystick.getMotionRange(MotionEvent.AXIS_GAS); if (rTrigger == null) { rTrigger = joystick.getMotionRange(MotionEvent.AXIS_RTRIGGER); mRTrigger = MotionEvent.AXIS_RTRIGGER; } InputDevice.MotionRange lTrigger = joystick.getMotionRange(MotionEvent.AXIS_BRAKE); if (lTrigger == null) { lTrigger = joystick.getMotionRange(MotionEvent.AXIS_LTRIGGER); mLTrigger = MotionEvent.AXIS_LTRIGGER; } List<InputDevice.MotionRange> ranges = joystick.getMotionRanges(); InputDevice.MotionRange dPad = null; String name = joystick.getName(); /* The Amazon Fire Game Controller follows the NVidia standard of sending AXIS_HAT_X/AXIS_HAT_Y results when the user hits the D-Pad. Only if we return false from dispatchGenericMotionEvent() will it then send DPAD keycodes. But the most popular Android joystick on the market at this time, the Nyko Playpad Pro, returns AXIS_HAT_X/AXIS_HAT_Y results when the left analog stick hits its extremes, meaning that the analog stick will generate DPAD keys if we return false. The Nyko generates DPAD keys directly for the DPAD controller. So we have two incompatible standards fighting with each other. Probably the safest thing to do would be to ask the user to press on their DPAD and see what messages we get, but that is beyond the scope of this example code. */ if (name.equals("Amazon Fire Game Controler")) { for (int i = 0; i < ranges.size(); ++i) { InputDevice.MotionRange range = ranges.get(i); int axis = range.getAxis(); if (axis == MotionEvent.AXIS_HAT_X || axis == MotionEvent.AXIS_HAT_Y) { dPad = ranges.get(i); break; } } } JoystickHelper.joystickDeadZones(lTrigger, rTrigger, lThumbX, lThumbY, rThumbX, rThumbY, dPad); } float lThumbX = event.getAxisValue(MotionEvent.AXIS_X); float lThumbY = -event.getAxisValue(MotionEvent.AXIS_Y); float rThumbX = event.getAxisValue(MotionEvent.AXIS_Z); float rThumbY = -event.getAxisValue(MotionEvent.AXIS_RZ); float lTrigger = event.getAxisValue(mLTrigger); float rTrigger = event.getAxisValue(mRTrigger); float hatX = event.getAxisValue(MotionEvent.AXIS_HAT_X); float hatY = event.getAxisValue(MotionEvent.AXIS_HAT_Y); JoystickHelper.setJoystickState(lTrigger, rTrigger, lThumbX, lThumbY, rThumbX, rThumbY, hatX, hatY); return true; } return super.dispatchGenericMotionEvent(event); }
From source file:com.thalmic.android.sample.helloworld.HelloWorldActivity.java
private void processJoystickInput(MotionEvent event, int historyPos) { InputDevice mInputDevice = event.getDevice(); // Calculate the horizontal distance to move by // using the input value from one of these physical controls: // the left control stick, hat axis, or the right control stick. float x = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_X, historyPos); if (x == 0) { x = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_HAT_X, historyPos); }// w w w . j a va 2 s . c om if (x == 0) { x = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_Z, historyPos); } // Calculate the vertical distance to move by // using the input value from one of these physical controls: // the left control stick, hat switch, or the right control stick. float y = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_Y, historyPos); if (y == 0) { y = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_HAT_Y, historyPos); } if (y == 0) { y = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_RZ, historyPos); } deltaJoystick = ((Math.abs(oldJoystickX - x) >= 0.1) || (Math.abs(oldJoystickY - y) >= 0.1)); JoystickData joystickDataX = new JoystickData("x", x); JoystickData joystickDataY = new JoystickData("y", y); if (deltaJoystick) { mPitch.setText("y is :" + y); oldJoystickX = x; oldJoystickY = y; if (client != null) { try { //client.send("servo01", "moveTo",(roll+90.0)); client.send("joystick", "publishJoystickInput", joystickDataX); client.send("joystick", "publishJoystickInput", joystickDataY); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:com.fishstix.dosboxfree.DBGLSurfaceView.java
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1) private void processJoystickInput(MotionEvent event, int historyPos) { float hatX = 0.0f; InputDevice.MotionRange range = event.getDevice().getMotionRange(MotionEvent.AXIS_HAT_X, event.getSource()); if (range != null) { if (historyPos >= 0) { hatX = InputDeviceState.ProcessAxis(range, event.getHistoricalAxisValue(MotionEvent.AXIS_HAT_X, historyPos)); } else {/*from w ww . j av a2 s .c om*/ hatX = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_HAT_X)); } } float hatY = 0.0f; range = event.getDevice().getMotionRange(MotionEvent.AXIS_HAT_Y, event.getSource()); if (range != null) { if (historyPos >= 0) { hatY = InputDeviceState.ProcessAxis(range, event.getHistoricalAxisValue(MotionEvent.AXIS_HAT_Y, historyPos)); } else { hatY = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_HAT_Y)); } } float joyX = 0.0f; range = event.getDevice().getMotionRange(MotionEvent.AXIS_X, event.getSource()); if (range != null) { if (historyPos >= 0) { joyX = InputDeviceState.ProcessAxis(range, event.getHistoricalAxisValue(MotionEvent.AXIS_X, historyPos)); } else { joyX = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_X)); } } float joyY = 0.0f; range = event.getDevice().getMotionRange(MotionEvent.AXIS_Y, event.getSource()); if (range != null) { if (historyPos >= 0) { joyY = InputDeviceState.ProcessAxis(range, event.getHistoricalAxisValue(MotionEvent.AXIS_Y, historyPos)); } else { joyY = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_Y)); } } float joy2X = 0.0f; range = event.getDevice().getMotionRange(MotionEvent.AXIS_Z, event.getSource()); if (range != null) { if (historyPos >= 0) { joy2X = InputDeviceState.ProcessAxis(range, event.getHistoricalAxisValue(MotionEvent.AXIS_Z, historyPos)); } else { joy2X = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_Z)); } } float joy2Y = 0.0f; range = event.getDevice().getMotionRange(MotionEvent.AXIS_RZ, event.getSource()); if (range != null) { if (historyPos >= 0) { joy2Y = InputDeviceState.ProcessAxis(range, event.getHistoricalAxisValue(MotionEvent.AXIS_RZ, historyPos)); } else { joy2Y = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_RZ)); } } if (mAnalogStickPref == 0) { mMouseThread.setCoord( (int) ((Math.abs(joyX * 32.0f) > DEADZONE) ? (-joyX * 32.0f * mMouseSensitivityX) : 0), (int) ((Math.abs(joyY * 32.0f) > DEADZONE) ? (-joyY * 32.0f * mMouseSensitivityY) : 0)); DosBoxControl.nativeJoystick((int) ((joy2X * 256.0f) + mJoyCenterX), (int) ((joy2Y * 256.0f) + mJoyCenterY), ACTION_MOVE, -1); } else { mMouseThread.setCoord( (int) ((Math.abs(joy2X * 32.0f) > DEADZONE) ? (-joy2X * 32.0f * mMouseSensitivityX) : 0), (int) ((Math.abs(joy2Y * 32.0f) > DEADZONE) ? (-joy2Y * 32.0f * mMouseSensitivityY) : 0)); DosBoxControl.nativeJoystick((int) ((joyX * 256.0f) + mJoyCenterX), (int) ((joyY * 256.0f) + mJoyCenterY), ACTION_MOVE, -1); } // Handle all other keyevents int value = 0; int tKeyCode = MAP_NONE; if (hatX < 0) { value = customMap.get(KeyEvent.KEYCODE_DPAD_LEFT); if (value > 0) { // found a valid mapping tKeyCode = getMappedKeyCode(value, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_LEFT)); if (tKeyCode > MAP_NONE) { DosBoxControl.sendNativeKey(tKeyCode, true, mModifierCtrl, mModifierAlt, mModifierShift); } } hatXlast = hatX; } else if (hatX > 0) { value = customMap.get(KeyEvent.KEYCODE_DPAD_RIGHT); if (value > 0) { // found a valid mapping tKeyCode = getMappedKeyCode(value, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT)); if (tKeyCode > MAP_NONE) { DosBoxControl.sendNativeKey(tKeyCode, true, mModifierCtrl, mModifierAlt, mModifierShift); } } hatXlast = hatX; } else { // released if (hatX != hatXlast) { if (hatXlast < 0) { value = customMap.get(KeyEvent.KEYCODE_DPAD_LEFT); if (value > 0) { // found a valid mapping tKeyCode = getMappedKeyCode(value, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_LEFT)); if (tKeyCode > MAP_NONE) { DosBoxControl.sendNativeKey(tKeyCode, false, mModifierCtrl, mModifierAlt, mModifierShift); } } } else if (hatXlast > 0) { value = customMap.get(KeyEvent.KEYCODE_DPAD_RIGHT); if (value > 0) { // found a valid mapping tKeyCode = getMappedKeyCode(value, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_RIGHT)); if (tKeyCode > MAP_NONE) { DosBoxControl.sendNativeKey(tKeyCode, false, mModifierCtrl, mModifierAlt, mModifierShift); } } } } hatXlast = hatX; } if (hatY < 0) { value = customMap.get(KeyEvent.KEYCODE_DPAD_UP); if (value > 0) { // found a valid mapping tKeyCode = getMappedKeyCode(value, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_UP)); if (tKeyCode > MAP_NONE) { DosBoxControl.sendNativeKey(tKeyCode, true, mModifierCtrl, mModifierAlt, mModifierShift); } } hatYlast = hatY; } else if (hatY > 0) { value = customMap.get(KeyEvent.KEYCODE_DPAD_DOWN); if (value > 0) { // found a valid mapping tKeyCode = getMappedKeyCode(value, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_DOWN)); if (tKeyCode > MAP_NONE) { DosBoxControl.sendNativeKey(tKeyCode, true, mModifierCtrl, mModifierAlt, mModifierShift); } } hatYlast = hatY; } else { // released if (hatY != hatYlast) { if (hatYlast < 0) { value = customMap.get(KeyEvent.KEYCODE_DPAD_UP); if (value > 0) { // found a valid mapping tKeyCode = getMappedKeyCode(value, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_UP)); if (tKeyCode > MAP_NONE) { DosBoxControl.sendNativeKey(tKeyCode, false, mModifierCtrl, mModifierAlt, mModifierShift); } } } else if (hatYlast > 0) { value = customMap.get(KeyEvent.KEYCODE_DPAD_DOWN); if (value > 0) { // found a valid mapping tKeyCode = getMappedKeyCode(value, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_DOWN)); if (tKeyCode > MAP_NONE) { DosBoxControl.sendNativeKey(tKeyCode, false, mModifierCtrl, mModifierAlt, mModifierShift); } } } } hatYlast = hatY; } }