Share via


Accelerometer.Shaken Event

Definition

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

// Register
event_token Shaken(TypedEventHandler<Accelerometer, AccelerometerShakenEventArgs const&> const& handler) const;

// Revoke with event_token
void Shaken(event_token const* cookie) const;

// Revoke with event_revoker
Accelerometer::Shaken_revoker Shaken(auto_revoke_t, TypedEventHandler<Accelerometer, AccelerometerShakenEventArgs const&> const& handler) const;
public event TypedEventHandler<Accelerometer,AccelerometerShakenEventArgs> Shaken;
function onShaken(eventArgs) { /* Your code */ }
accelerometer.addEventListener("shaken", onShaken);
accelerometer.removeEventListener("shaken", onShaken);
- or -
accelerometer.onshaken = onShaken;
Public Custom Event Shaken As TypedEventHandler(Of Accelerometer, AccelerometerShakenEventArgs) 

Event Type

Examples

The following example demonstrates how a UWP 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();
    });
}

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.

Support for the Shaken event is dependent upon hardware and driver support. In practice, very few accelerometers support the Shaken event. If the accelerometer does not support the Shaken event and you add an event handler for the Shaken event, no error is raised, but the code in the event handler won't run.

Applies to