new Keyboard(shortcutCombination, function, dataParametersopt) → {Object}
Create a new Keyboard object (registered shortcut). This API needs to be written in userlib.js to take effect.
Note: Keyboard execution code and Keyboard definition must be in Userlib.js
Supports:
Keyboard(shortcutCombination, function, dataParameters)
Keyboard(shortcutCombination, function)
Example
// Example 1: Simple key binding with one device parameter
// When the user presses Ctrl+Alt+V at the same time, the phone screen will automatically jump to the HOME page.
// Note: For functions with only one device parameter, the third parameter "dataParameters" of the new Keyboard object must be omitted (even if there is a third parameter, it doesn't make sense, because it cannot be passed in), so Keyboard is defined as follows:
var { Device } = require("sigma/device");
var { Keyboard } = require("sigma/Keyboard");
var haveatry = function(device) {
device.send(tcConst.KEY_HOME);
};
var h1 = new Keyboard("CA|V", haveatry);
// Or using inline function:
var h2 = new Keyboard("CA|V", function(device) {
Device.getMain().send(tcConst.KEY_HOME);
});
// Example 2: Key binding with custom data, no device/app binding
// When the user presses the Ctrl+Alt+V key at the same time, "data is 1" is output. , app and device are both null, so the shortcut Ctrl+Alt+V is always valid.
var { Device } = require("sigma/device");
var { Keyboard } = require("sigma/Keyboard");
var trytrytry = function(device, x) {
if (x == 1) {
print("data is 1");
} else {
print("x.device: " + x.device);
print("x.field1: " + x.field1);
print("x.field2: " + x.field2);
x.device.send(tcConst.KEY_HOME);
}
};
var h = new Keyboard("CA|V", trytrytry, {
app: null,
device: null,
data: 1
});
// Example 3: Shortcut bound to a specific app and device
// When the user clicks Ctrl+Alt+V, "data is 1" is output. The app and device are not empty, the shortcut Ctrl+Alt+U will only work if the device is specified and the foreground app is com.tencent.qqmusic.
var { Device } = require("sigma/device");
var { Keyboard } = require("sigma/Keyboard");
var trytrytry = function(device, x) {
if (x == 1) {
print("data is 1");
} else {
print("x.device: " + x.device);
print("x.field1: " + x.field1);
print("x.field2: " + x.field2);
x.device.send(tcConst.KEY_HOME);
}
};
var h = new Keyboard("CA|U", trytrytry, {
app: "com.tencent.qqmusic",
device: Device.getMain(),
data: { field1: 1, field2: 2 }
});
Parameters:
| Name | Type | Attributes | Description | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
shortcutCombination |
string | Define a combination key in the format of "control key combination | letter key".
|
|||||||||||||||||
function |
string | A user-defined function that can take only one device parameter or two parameters. With two parameters, the first parameter is device and the second parameter is data . Please refer to the examples below for details. |
|||||||||||||||||
dataParameters |
object |
<optional> |
Optional parameter, json object, including "app, device, data" three keywords, for example: {app: 'com.tencent.mm', ‘device’:..., data: ...} Properties
|
Returns:
Returns the keyboard object if successful. If it fails, you can get the specific error message through the lastError() function.
- Type
- Object
Members
delete
Delete the current Keyboard object.
Example
// Example: When the user presses the Ctrl+Alt+V key at the same time, "data is 1" is output. , app and device are both null, so the shortcut Ctrl+Alt+V is always valid.
var { Keyboard } = require("sigma/Keyboard");
// Define a shortcut that triggers Ctrl+Alt+V and prints a message or sends HOME key
var trytrytry = function(device, x) {
if (x === 1) {
print("data is 1");
} else {
print("x.device: " + x.device);
print("x.field1: " + x.field1);
print("x.field2: " + x.field2);
x.device.send(tcConst.KEY_HOME);
}
};
// Register a global shortcut (works on any app and device)
var key1 = new Keyboard('CA|V', trytrytry, {
app: null,
device: null,
data: 1
});
//Get the created Keyboard object getkey1.
var getkey1 = Keyboard.searchObject('CA|V');
// Get the created Keyboard object getkey1.
getkey1.delete(); // Now 'CA|V' will no longer trigger the function
trigger
Run the function corresponding to the shortcut key.
The Keyboard object runs the function corresponding to the shortcut by calling trigger(obj) .
Example
// Define a callback function
trytrytry = function(device, x) {
if (x == 1) {
print("data is 1");
} else {
print("x.device: " + x.device);
print("x.field1: " + x.field1);
print("x.field2: " + x.field2);
x.device.send(tcConst.KEY_HOME);
}
};
// Register the shortcut with 'CA|V' (Ctrl + Alt + V)
var key1 = new Keyboard('CA|V', trytrytry, {app: null, device: null, data: 1});
// Manually trigger the callback with custom parameters
key1.trigger({
device: Device.getMain(),
x: 1,
y: 2
});
// Operation Result:
// If `x == 1`, the output will be:
data is 1
unregister
Cancel the shortcuts that have been set.
The Keyboard object cancels the shortcut by calling unregister().
Example
var { Keyboard } = require("sigma/Keyboard");
// Define a function with two arguments (device, data)
var trytrytry = function (device, x) {
if (x === 1) {
print("data is 1");
} else {
print("x.device: " + x.device);
print("x.field1: " + x.field1);
print("x.field2: " + x.field2);
x.device.send(tcConst.KEY_HOME);
}
};
// Register a keyboard shortcut Ctrl+Alt+V with extra data
var key1 = new Keyboard('CA|V', trytrytry, {
app: null,
device: null,
data: 1
});
// Cancel the shortcuts that have been set
key1.unregister(); // The Ctrl+Alt+V shortcut is now disabled