Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-hatton/c1f7b3a344f89a3e78c9346b345ef25e to your computer and use it in GitHub Desktop.
Save chris-hatton/c1f7b3a344f89a3e78c9346b345ef25e to your computer and use it in GitHub Desktop.
Fragment to get pressed D-Pad button from SteamVR controller in Unity
static class SteamVR_Controller_Extension
{
const float threshold = 0.3f;
/*
* You might expect that pressing one of the edges of the SteamVR controller touchpad could
* be detected with a call to device.GetPress( EVRButtonId.k_EButton_DPad_* ), but currently this always returns false.
* Not sure whether this is SteamVR's design intent, not yet implemented, or a bug.
* The expected behaviour can be achieved by detecting overall Touchpad press, with Touch-Axis comparison to an edge threshold.
*/
public static bool GetDPadPress( this SteamVR_Controller.Device device, EVRButtonId dPadButtonId )
{
if ( device.GetPress( SteamVR_Controller.ButtonMask.Touchpad ) ) // Is any DPad button pressed?
{
var touchpadAxis = device.GetAxis( EVRButtonId.k_EButton_SteamVR_Touchpad );
if ( touchpadAxis.y > (1.0f - threshold ) ) { return dPadButtonId == EVRButtonId.k_EButton_DPad_Up; }
else if ( touchpadAxis.y < threshold ) { return dPadButtonId == EVRButtonId.k_EButton_DPad_Down; }
else if ( touchpadAxis.x > (1.0f - threshold ) ) { return dPadButtonId == EVRButtonId.k_EButton_DPad_Right; }
else if ( touchpadAxis.x < threshold ) { return dPadButtonId == EVRButtonId.k_EButton_DPad_Left; }
}
return false;
}
}
@applesaucesome
Copy link

applesaucesome commented Jun 23, 2016

Hi Chris, thank you for your work on this, I don't understand why Valve hasn't enabled the Dpad support yet. While I was using your gist, I found that some of the code didn't work (at least on the current Vive firmware), so I slightly adjusted your code and it seems to be working as itended now.

Thanks again, there's barely any documentation right now!

The parts I slightly adjusted were your left and right if() statements:

if (touchpadAxis.y > (1.0f - threshold)) { return dPadButtonId == EVRButtonId.k_EButton_DPad_Up; } else if (touchpadAxis.y < (-1f + threshold)) { return dPadButtonId == EVRButtonId.k_EButton_DPad_Down; } else if (touchpadAxis.x > (1.0f - threshold)) { return dPadButtonId == EVRButtonId.k_EButton_DPad_Right; } else if (touchpadAxis.x < (-1f + threshold)) { return dPadButtonId == EVRButtonId.k_EButton_DPad_Left; }

@applesaucesome
Copy link

Oh, also, if you wanted to make it so it captures the press once the user releases the button, instead of currently where it sends a constant stream if the button is held down, you can update your if() statement for device.GetPress()instad to be device.GetPressUp()

Sorry, I'll stop spamming your wall now, thanks again! (I made sure to credit your gist in my source)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment