2025-03-18 20:08:23 +04:00
|
|
|
# BadUSB module {#js_badusb}
|
2024-04-09 12:06:37 +03:00
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
let badusb = require("badusb");
|
|
|
|
|
```
|
|
|
|
|
# Methods
|
2025-03-26 17:29:26 +03:00
|
|
|
## setup()
|
2024-04-09 12:06:37 +03:00
|
|
|
Start USB HID with optional parameters. Should be called before all other methods.
|
|
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
**Parameters**
|
|
|
|
|
|
|
|
|
|
Configuration object *(optional)*:
|
2024-04-09 12:06:37 +03:00
|
|
|
- vid, pid (number): VID and PID values, both are mandatory
|
|
|
|
|
- mfr_name (string): Manufacturer name (32 ASCII characters max), optional
|
|
|
|
|
- prod_name (string): Product name (32 ASCII characters max), optional
|
|
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
**Examples**
|
2024-04-09 12:06:37 +03:00
|
|
|
```js
|
|
|
|
|
// Start USB HID with default parameters
|
|
|
|
|
badusb.setup();
|
|
|
|
|
// Start USB HID with custom vid:pid = AAAA:BBBB, manufacturer and product strings not defined
|
|
|
|
|
badusb.setup({ vid: 0xAAAA, pid: 0xBBBB });
|
|
|
|
|
// Start USB HID with custom vid:pid = AAAA:BBBB, manufacturer string = "Flipper Devices", product string = "Flipper Zero"
|
|
|
|
|
badusb.setup({ vid: 0xAAAA, pid: 0xBBBB, mfr_name: "Flipper Devices", prod_name: "Flipper Zero" });
|
|
|
|
|
```
|
|
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
<br>
|
|
|
|
|
|
|
|
|
|
## isConnected()
|
2024-04-09 12:06:37 +03:00
|
|
|
Returns USB connection state.
|
|
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
**Example**
|
2024-04-09 12:06:37 +03:00
|
|
|
```js
|
|
|
|
|
if (badusb.isConnected()) {
|
|
|
|
|
// Do something
|
|
|
|
|
} else {
|
|
|
|
|
// Show an error
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
<br>
|
|
|
|
|
|
|
|
|
|
## press()
|
2024-04-09 12:06:37 +03:00
|
|
|
Press and release a key.
|
|
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
**Parameters**
|
|
|
|
|
|
2024-04-09 12:06:37 +03:00
|
|
|
Key or modifier name, key code.
|
|
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
See a [list of key names below](#js_badusb_keynames).
|
2024-04-09 12:06:37 +03:00
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
**Examples**
|
2024-04-09 12:06:37 +03:00
|
|
|
```js
|
|
|
|
|
badusb.press("a"); // Press "a" key
|
|
|
|
|
badusb.press("A"); // SHIFT + "a"
|
|
|
|
|
badusb.press("CTRL", "a"); // CTRL + "a"
|
|
|
|
|
badusb.press("CTRL", "SHIFT", "ESC"); // CTRL + SHIFT + ESC combo
|
|
|
|
|
badusb.press(98); // Press key with HID code (dec) 98 (Numpad 0 / Insert)
|
|
|
|
|
badusb.press(0x47); // Press key with HID code (hex) 0x47 (Scroll lock)
|
|
|
|
|
```
|
|
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
<br>
|
|
|
|
|
|
|
|
|
|
## hold()
|
2024-04-09 12:06:37 +03:00
|
|
|
Hold a key. Up to 5 keys (excluding modifiers) can be held simultaneously.
|
|
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
**Parameters**
|
|
|
|
|
|
2025-03-18 20:08:23 +04:00
|
|
|
Same as `press`.
|
2024-04-09 12:06:37 +03:00
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
**Examples**
|
2024-04-09 12:06:37 +03:00
|
|
|
```js
|
|
|
|
|
badusb.hold("a"); // Press and hold "a" key
|
|
|
|
|
badusb.hold("CTRL", "v"); // Press and hold CTRL + "v" combo
|
|
|
|
|
```
|
|
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
<br>
|
|
|
|
|
|
|
|
|
|
## release()
|
2024-10-08 15:27:16 +04:00
|
|
|
Release a previously held key.
|
2024-04-09 12:06:37 +03:00
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
**Parameters**
|
|
|
|
|
|
2025-03-18 20:08:23 +04:00
|
|
|
Same as `press`.
|
2024-04-09 12:06:37 +03:00
|
|
|
|
2025-03-18 20:08:23 +04:00
|
|
|
Release all keys if called without parameters.
|
2024-04-09 12:06:37 +03:00
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
**Examples**
|
2024-04-09 12:06:37 +03:00
|
|
|
```js
|
|
|
|
|
badusb.release(); // Release all keys
|
|
|
|
|
badusb.release("a"); // Release "a" key
|
|
|
|
|
```
|
|
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
<br>
|
|
|
|
|
|
|
|
|
|
## print()
|
2024-04-09 12:06:37 +03:00
|
|
|
Print a string.
|
|
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
**Parameters**
|
|
|
|
|
|
2024-04-09 12:06:37 +03:00
|
|
|
- A string to print
|
2025-03-18 20:08:23 +04:00
|
|
|
- *(optional)* Delay between key presses
|
2024-04-09 12:06:37 +03:00
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
**Examples**
|
2024-04-09 12:06:37 +03:00
|
|
|
```js
|
|
|
|
|
badusb.print("Hello, world!"); // print "Hello, world!"
|
|
|
|
|
badusb.print("Hello, world!", 100); // Add 100ms delay between key presses
|
|
|
|
|
```
|
2025-03-26 17:29:26 +03:00
|
|
|
<br>
|
2024-04-09 12:06:37 +03:00
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
## println()
|
2024-04-09 12:06:37 +03:00
|
|
|
Same as `print` but ended with "ENTER" press.
|
|
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
**Parameters**
|
|
|
|
|
|
2024-04-09 12:06:37 +03:00
|
|
|
- A string to print
|
2025-03-18 20:08:23 +04:00
|
|
|
- *(optional)* Delay between key presses
|
2024-04-09 12:06:37 +03:00
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
**Examples**
|
2024-04-09 12:06:37 +03:00
|
|
|
```js
|
|
|
|
|
badusb.println("Hello, world!"); // print "Hello, world!" and press "ENTER"
|
|
|
|
|
```
|
2025-03-26 17:29:26 +03:00
|
|
|
<br>
|
2024-04-09 12:06:37 +03:00
|
|
|
|
2025-03-26 17:29:26 +03:00
|
|
|
# Key names list {#js_badusb_keynames}
|
2024-04-09 12:06:37 +03:00
|
|
|
|
|
|
|
|
## Modifier keys
|
|
|
|
|
|
|
|
|
|
| Name |
|
|
|
|
|
| ------------- |
|
2024-06-30 15:18:46 +04:00
|
|
|
| CTRL |
|
|
|
|
|
| SHIFT |
|
2024-04-09 12:06:37 +03:00
|
|
|
| ALT |
|
2024-06-30 15:18:46 +04:00
|
|
|
| GUI |
|
2024-04-09 12:06:37 +03:00
|
|
|
|
|
|
|
|
## Special keys
|
|
|
|
|
|
|
|
|
|
| Name | Notes |
|
|
|
|
|
| ------------------ | ---------------- |
|
|
|
|
|
| DOWN | Down arrow |
|
|
|
|
|
| LEFT | Left arrow |
|
|
|
|
|
| RIGHT | Right arrow |
|
|
|
|
|
| UP | Up arrow |
|
|
|
|
|
| ENTER | |
|
|
|
|
|
| DELETE | |
|
|
|
|
|
| BACKSPACE | |
|
|
|
|
|
| END | |
|
|
|
|
|
| HOME | |
|
|
|
|
|
| ESC | |
|
|
|
|
|
| INSERT | |
|
|
|
|
|
| PAGEUP | |
|
|
|
|
|
| PAGEDOWN | |
|
|
|
|
|
| CAPSLOCK | |
|
|
|
|
|
| NUMLOCK | |
|
|
|
|
|
| SCROLLLOCK | |
|
|
|
|
|
| PRINTSCREEN | |
|
|
|
|
|
| PAUSE | Pause/Break key |
|
|
|
|
|
| SPACE | |
|
|
|
|
|
| TAB | |
|
|
|
|
|
| MENU | Context menu key |
|
|
|
|
|
| Fx | F1-F24 keys |
|