Windows desktop applications
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Accelerometer.Shaken | shaken event

[ Updated for UWP apps on Windows 10. For Windows 8.x articles, see the archive ]

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

Syntax


function onShaken(eventArgs) { /* Your code */ }
 
// addEventListener syntax
accelerometer.addEventListener("shaken", onShaken);
accelerometer.removeEventListener("shaken", onShaken);
 
- or -

accelerometer.onshaken = onShaken;


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 (Windows 10 device family)

Device family

Universal

API contract

Windows.Foundation.UniversalApiContract, introduced version 1.0

Namespace

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

Metadata

Windows.Foundation.UniversalApiContract.winmd

Requirements (Windows 8.x and Windows Phone 8.x)

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:
© 2016 Microsoft