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:
| string | shortcutCombination |
Define a combination key in the format of "control key combination | letter key".
|
|||||||||||||
| string | function |
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. |
|||||||||||||
| object | dataParameters |
<optional> |
Optional parameter, json object, including "app, device, data" three keywords, for example: {app: 'com.tencent.mm', ‘device’:..., data: ...} Properties
|
Returns:
| Object |
Returns the keyboard object if successful. If it fails, you can get the specific error message through the lastError() function. |