Creating an Action
Actions are how Logitech devices interact with your plugin. There are two types of actions:
- Commands
- Adjustments
Command Action
A command action is designed to be executed once a device button is pressed. To create a command action, extend the CommandAction class and implement the class properties and functions
import { CommandAction } from '@logitech/plugin-sdk';
import { exec } from 'child_process';
function sendMessage(message) {
exec(`start cmd.exe /K echo ${message}`);
}
export class MyTestAction extends CommandAction {
name = 'My_Test_Action';
displayName = 'My Test Action';
description = 'My custom action';
groupName = 'My Actions';
onKeyDown() {
sendMessage(`${this.name} is called by key clicking!`);
}
}
This action will open a command prompt window with the message you have specified. The action also needs to be registered with the PluginSDK. This needs to be done before calling the connect function:
import { PluginSDK } from '@logitech/plugin-sdk';
import { MyTestAction } from './src/test-actions.js';
const pluginSDK = new PluginSDK();
// Register plugin actions
pluginSDK.registerAction(new MyTestAction());
await pluginSDK.connect();
Adjustment Action
An adjustment action is an action that responds to a rotation adjustment from a Logitech device, such as a scroll wheel or a knob. To create an adjustment action, extend the AdjustmentAction class and implement the class properties and functions:
import { AdjustmentAction } from '@logitech/plugin-sdk';
import { exec } from 'child_process';
function sendMessage(message) {
exec(`start cmd.exe /K echo ${message}`);
}
export class MyTestAdjustment extends AdjustmentAction {
name = 'My_Test_Adjustment';
displayName = 'My Test Adjustment';
description = 'My Test Adjustment';
execute(event) {
const message = `${this.name} is called. event.tick: ${event.tick}`;
sendMessage(message);
}
}
This action will open a command prompt and display in ticks, how much the dial has been rotated. Use event.tick to measure how much the dial has turned. The action also needs to be registered with the PluginSDK. This needs to be done before calling the connect function: