Accelerometer.Shaken | shaken event

Applies to Windows and Windows Phone

Occurs when the accelerometer detects that the PC has been shaken.

Syntax


public:
event TypedEventHandler<Accelerometer, AccelerometerShakenEventArgs>^ Shaken {
   Windows::Foundation::EventRegistrationToken add(TypedEventHandler<Accelerometer, AccelerometerShakenEventArgs>^ value);
   void remove(Windows::Foundation::EventRegistrationToken token);
}

Event information

Delegate TypedEventHandler<Accelerometer, AccelerometerShakenEventArgs>

Remarks

Use this event to receive notification that the device containing the accelerometer has been shaken. The app is not required to set a report interval prior to registering for Shaken events.

The following example demonstrates how a Windows Store app built with C# and XAML registers its Shaken event handler.


private void ScenarioEnable(object sender, RoutedEventArgs e)
{
    if (_accelerometer != null)
    {
        Window.Current.VisibilityChanged += new WindowVisibilityChangedEventHandler(VisibilityChanged);
        _accelerometer.Shaken += new TypedEventHandler<Accelerometer, AccelerometerShakenEventArgs>(Shaken);
        ScenarioEnableButton.IsEnabled = false;
        ScenarioDisableButton.IsEnabled = true;
    }
    else
    {
        rootPage.NotifyUser("No accelerometer found", NotifyType.StatusMessage);
    }
}


The following example shows the Shaken event handler.


async private void Shaken(object sender, AccelerometerShakenEventArgs e)
{
    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        _shakeCount++;
        ScenarioOutputText.Text = _shakeCount.ToString();
    });
}


The following example demonstrates how a Windows Store appbuilt with JavaScript registers its Shaken event handler.


function enableShakenScenario() {
    if (accelerometer) {
        document.addEventListener("msvisibilitychange", msVisibilityChangeHandler, false);
        accelerometer.addEventListener("shaken", onShaken);
        document.getElementById("scenario2Open").disabled = true;
        document.getElementById("scenario2Revoke").disabled = false;
    } else {
        WinJS.log && WinJS.log("No accelerometer found", "sample", "error");
    }
}


The following example shows the Shaken event handler.


var onShaken = (function () {
    var shakeCount = 0;

    return function (e) {
        shakeCount++;
        document.getElementById("shakeOutput").innerHTML = shakeCount;
    };
})();


Windows Phone 8:  The accelerometer sensor does not raise the Shaken event on the Windows Phone 8 platform. If you add an event handler for the Shaken event in your Windows Phone 8 app, no error is raised, but the code in the event handler won’t run.

Requirements

Minimum supported client

Windows 8

Minimum supported server

None supported

Minimum supported phone

Windows Phone 8

Namespace

Windows.Devices.Sensors
Windows::Devices::Sensors [C++]

Metadata

Windows.winmd

See also

Accelerometer

 

 

Show:
© 2014 Microsoft