import { Shake } from '@capgo/capacitor-shake';
// Start listening for shake gesturesawait Shake.start();
// Listen for shake eventsShake.addListener('shake', () => { console.log('Device was shaken!'); // Perform your action here});
Install the package
npm i @capgo/capacitor-shake
pnpm add @capgo/capacitor-shake
yarn add @capgo/capacitor-shake
bun add @capgo/capacitor-shake
Sync with native projects
npx cap sync
pnpm cap sync
yarn cap sync
bunx cap sync
Configure the plugin
import { Shake } from '@capgo/capacitor-shake';
// Start listening for shake gesturesawait Shake.start();
// Listen for shake eventsShake.addListener('shake', () => { console.log('Device was shaken!'); // Perform your action here});
// Configure shake sensitivityawait Shake.start({ threshold: 3.5 // Adjust sensitivity (default: 3.5)});
No additional setup required for iOS.
No additional setup required for Android.
Stop listening for shakes
// Stop shake detection when not neededawait Shake.stop();
// Remove specific listenerconst handle = await Shake.addListener('shake', () => { console.log('Shaken!');});handle.remove();
Example implementation
import { Shake } from '@capgo/capacitor-shake';import { App } from '@capacitor/app';
export class ShakeService { private shakeListener: any;
async initialize() { // Start shake detection await Shake.start({ threshold: 4.0 });
// Add shake listener this.shakeListener = await Shake.addListener('shake', () => { this.handleShake(); });
// Clean up on app pause App.addListener('pause', () => { Shake.stop(); });
// Resume on app resume App.addListener('resume', () => { Shake.start({ threshold: 4.0 }); }); }
private handleShake() { console.log('Shake detected!'); // Show debug menu, refresh data, or trigger any action }
async cleanup() { if (this.shakeListener) { this.shakeListener.remove(); } await Shake.stop(); }}
start(options?: ShakeOptions)
Start listening for shake gestures.
Parameters:
options
(optional): Configuration object
threshold
: number - Sensitivity threshold (default: 3.5)stop()
Stop listening for shake gestures.
addListener('shake', callback: () => void)
Add a listener for shake events.
Returns: Promise with a handle to remove the listener
interface ShakeOptions { threshold?: number; // Shake sensitivity threshold}
Shake not detected:
Shake.start()
Too sensitive/not sensitive enough:
threshold
parameter in start()
options