From 5095338341794671b765cccaed9202c5bc9f3b60 Mon Sep 17 00:00:00 2001 From: oldip Date: Thu, 14 Mar 2024 09:34:22 +0800 Subject: [PATCH 01/20] Add numpad keybindings to improve non-US input methods --- applications/system/js_app/modules/js_badusb.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/applications/system/js_app/modules/js_badusb.c b/applications/system/js_app/modules/js_badusb.c index 0ac5c47da2..986fdd5b3b 100644 --- a/applications/system/js_app/modules/js_badusb.c +++ b/applications/system/js_app/modules/js_badusb.c @@ -52,6 +52,17 @@ static const struct { {"F10", HID_KEYBOARD_F10}, {"F11", HID_KEYBOARD_F11}, {"F12", HID_KEYBOARD_F12}, + + {"NUMPAD_0", HID_KEYPAD_0}, + {"NUMPAD_1", HID_KEYPAD_1}, + {"NUMPAD_2", HID_KEYPAD_2}, + {"NUMPAD_3", HID_KEYPAD_3}, + {"NUMPAD_4", HID_KEYPAD_4}, + {"NUMPAD_5", HID_KEYPAD_5}, + {"NUMPAD_6", HID_KEYPAD_6}, + {"NUMPAD_7", HID_KEYPAD_7}, + {"NUMPAD_8", HID_KEYPAD_8}, + {"NUMPAD_9", HID_KEYPAD_9}, }; static void js_badusb_quit_free(JsBadusbInst* badusb) { From 8e44322a153eac0701744fc3a622c86124945de2 Mon Sep 17 00:00:00 2001 From: oldip Date: Thu, 14 Mar 2024 10:53:22 +0800 Subject: [PATCH 02/20] Add altPrintln functionality for ALT+Numpad support --- .../system/js_app/modules/js_badusb.c | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/applications/system/js_app/modules/js_badusb.c b/applications/system/js_app/modules/js_badusb.c index 986fdd5b3b..b246cfa1e2 100644 --- a/applications/system/js_app/modules/js_badusb.c +++ b/applications/system/js_app/modules/js_badusb.c @@ -399,6 +399,40 @@ static void js_badusb_println(struct mjs* mjs) { badusb_print(mjs, true); } +// Adding the altPrintln functionality +static void js_badusb_altPrintln(struct mjs* mjs) { + mjs_val_t obj_inst = mjs_get(mjs, mjs_get_this(mjs), INST_PROP_NAME, ~0); + JsBadusbInst* badusb = mjs_get_ptr(mjs, obj_inst); + furi_assert(badusb); + + mjs_val_t str_val = mjs_arg(mjs, 0); + size_t str_len; + const char* str = mjs_get_string(mjs, &str_val, &str_len); + + for(size_t i = 0; i < str_len; ++i) { + uint8_t ascii_code = (uint8_t)str[i]; + // Simulating pressing the ALT key + furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT); + // Inputting the ASCII code numbers + char ascii_str[5]; + snprintf(ascii_str, sizeof(ascii_str), "%u", ascii_code); + for(size_t j = 0; ascii_str[j] != '\0'; ++j) { + uint16_t numpad_keycode = get_keycode_by_name((const char[]){'NUMPAD_', ascii_str[j], '\0'}, 3); + if(numpad_keycode != HID_KEYBOARD_NONE) { + furi_hal_hid_kb_press(numpad_keycode); + furi_hal_hid_kb_release(numpad_keycode); + } + } + // Releasing the ALT key + furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT); + } + // Adding a new line + furi_hal_hid_kb_press(HID_KEYBOARD_RETURN); + furi_hal_hid_kb_release(HID_KEYBOARD_RETURN); + + mjs_return(mjs, MJS_UNDEFINED); +} + static void* js_badusb_create(struct mjs* mjs, mjs_val_t* object) { JsBadusbInst* badusb = malloc(sizeof(JsBadusbInst)); mjs_val_t badusb_obj = mjs_mk_object(mjs); @@ -411,6 +445,9 @@ static void* js_badusb_create(struct mjs* mjs, mjs_val_t* object) { mjs_set(mjs, badusb_obj, "release", ~0, MJS_MK_FN(js_badusb_release)); mjs_set(mjs, badusb_obj, "print", ~0, MJS_MK_FN(js_badusb_print)); mjs_set(mjs, badusb_obj, "println", ~0, MJS_MK_FN(js_badusb_println)); + // Register the altPrintln method for calling from JavaScript + mjs_set(mjs, badusb_obj, "altPrintln", ~0, MJS_MK_FN(js_badusb_altPrintln)); + *object = badusb_obj; return badusb; } From e790ffad3a84adc8d9926c1872f206bb7fbf3c78 Mon Sep 17 00:00:00 2001 From: oldip Date: Mon, 18 Mar 2024 15:19:35 +0800 Subject: [PATCH 03/20] Fix snprintf truncation issue in altPrintln --- .../system/js_app/modules/js_badusb.c | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/applications/system/js_app/modules/js_badusb.c b/applications/system/js_app/modules/js_badusb.c index b246cfa1e2..724f8072cb 100644 --- a/applications/system/js_app/modules/js_badusb.c +++ b/applications/system/js_app/modules/js_badusb.c @@ -409,18 +409,25 @@ static void js_badusb_altPrintln(struct mjs* mjs) { size_t str_len; const char* str = mjs_get_string(mjs, &str_val, &str_len); + // Corrected part in the js_badusb_altPrintln function for(size_t i = 0; i < str_len; ++i) { uint8_t ascii_code = (uint8_t)str[i]; // Simulating pressing the ALT key furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT); - // Inputting the ASCII code numbers - char ascii_str[5]; - snprintf(ascii_str, sizeof(ascii_str), "%u", ascii_code); - for(size_t j = 0; ascii_str[j] != '\0'; ++j) { - uint16_t numpad_keycode = get_keycode_by_name((const char[]){'NUMPAD_', ascii_str[j], '\0'}, 3); + // Preparing to input the ASCII code numbers + char numpad_sequence[16]; // Adjust to a larger array size to accommodate the complete string + + // For each character's ASCII code, convert it to a string with NUMPAD_ prefix + int num_digits = snprintf(NULL, 0, "%u", ascii_code); // Determine the number of digits in the ASCII code + snprintf(numpad_sequence, sizeof(numpad_sequence), "NUMPAD_%u", ascii_code); // Construct the string for numpad sequence + + // For each digit in the constructed NUMPAD_ string, find the corresponding keycode and simulate key press + for(int digit_index = 7; digit_index < 7 + num_digits; ++digit_index) { + char digit_str[2] = {numpad_sequence[digit_index], '\0'}; // Create a string for each digit + uint16_t numpad_keycode = get_keycode_by_name(digit_str, 1); // Get the keycode for the digit if(numpad_keycode != HID_KEYBOARD_NONE) { - furi_hal_hid_kb_press(numpad_keycode); - furi_hal_hid_kb_release(numpad_keycode); + furi_hal_hid_kb_press(numpad_keycode); // Press the digit key + furi_hal_hid_kb_release(numpad_keycode); // Release the digit key } } // Releasing the ALT key From fc3a1f28b21ed76d6089ec7dca5292482612a8f2 Mon Sep 17 00:00:00 2001 From: oldip Date: Mon, 18 Mar 2024 15:52:51 +0800 Subject: [PATCH 04/20] Adjusting the altPrintln functionality to release the ALT key after each character --- .../system/js_app/modules/js_badusb.c | 35 ++++++++----------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/applications/system/js_app/modules/js_badusb.c b/applications/system/js_app/modules/js_badusb.c index 724f8072cb..e26919593f 100644 --- a/applications/system/js_app/modules/js_badusb.c +++ b/applications/system/js_app/modules/js_badusb.c @@ -399,7 +399,7 @@ static void js_badusb_println(struct mjs* mjs) { badusb_print(mjs, true); } -// Adding the altPrintln functionality +// Adjusting the altPrintln functionality to release the ALT key after each character static void js_badusb_altPrintln(struct mjs* mjs) { mjs_val_t obj_inst = mjs_get(mjs, mjs_get_this(mjs), INST_PROP_NAME, ~0); JsBadusbInst* badusb = mjs_get_ptr(mjs, obj_inst); @@ -409,31 +409,26 @@ static void js_badusb_altPrintln(struct mjs* mjs) { size_t str_len; const char* str = mjs_get_string(mjs, &str_val, &str_len); - // Corrected part in the js_badusb_altPrintln function for(size_t i = 0; i < str_len; ++i) { uint8_t ascii_code = (uint8_t)str[i]; - // Simulating pressing the ALT key - furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT); - // Preparing to input the ASCII code numbers - char numpad_sequence[16]; // Adjust to a larger array size to accommodate the complete string - - // For each character's ASCII code, convert it to a string with NUMPAD_ prefix - int num_digits = snprintf(NULL, 0, "%u", ascii_code); // Determine the number of digits in the ASCII code - snprintf(numpad_sequence, sizeof(numpad_sequence), "NUMPAD_%u", ascii_code); // Construct the string for numpad sequence - // For each digit in the constructed NUMPAD_ string, find the corresponding keycode and simulate key press - for(int digit_index = 7; digit_index < 7 + num_digits; ++digit_index) { - char digit_str[2] = {numpad_sequence[digit_index], '\0'}; // Create a string for each digit - uint16_t numpad_keycode = get_keycode_by_name(digit_str, 1); // Get the keycode for the digit - if(numpad_keycode != HID_KEYBOARD_NONE) { - furi_hal_hid_kb_press(numpad_keycode); // Press the digit key - furi_hal_hid_kb_release(numpad_keycode); // Release the digit key + // Convert each character's ASCII code to a series of Numpad key presses with ALT key + char ascii_str[5]; // Enough to hold up to 4 digits plus a null terminator + snprintf(ascii_str, sizeof(ascii_str), "%u", ascii_code); + + // For each digit of the ASCII code, simulate pressing ALT + Numpad key + furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT); + for(size_t j = 0; ascii_str[j] != '\0'; ++j) { + uint16_t keycode = get_keycode_by_name((const char[]){"NUMPAD_", ascii_str[j], '\0'}, 3); + if(keycode != HID_KEYBOARD_NONE) { + furi_hal_hid_kb_press(keycode); // Press the Numpad key + furi_hal_hid_kb_release(keycode); // Release the Numpad key } } - // Releasing the ALT key - furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT); + furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT); // Release the ALT key after each character } - // Adding a new line + + // Simulate pressing the Enter key to mimic println behavior furi_hal_hid_kb_press(HID_KEYBOARD_RETURN); furi_hal_hid_kb_release(HID_KEYBOARD_RETURN); From 68746193a0d7ccd24910fb193d39e1cc73ae9927 Mon Sep 17 00:00:00 2001 From: oldip Date: Mon, 18 Mar 2024 16:01:13 +0800 Subject: [PATCH 05/20] Fix dynamic construction of NUMPAD keycodes in altPrintln --- .../system/js_app/modules/js_badusb.c | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/applications/system/js_app/modules/js_badusb.c b/applications/system/js_app/modules/js_badusb.c index e26919593f..c2acd553ae 100644 --- a/applications/system/js_app/modules/js_badusb.c +++ b/applications/system/js_app/modules/js_badusb.c @@ -412,20 +412,24 @@ static void js_badusb_altPrintln(struct mjs* mjs) { for(size_t i = 0; i < str_len; ++i) { uint8_t ascii_code = (uint8_t)str[i]; - // Convert each character's ASCII code to a series of Numpad key presses with ALT key - char ascii_str[5]; // Enough to hold up to 4 digits plus a null terminator + // Convert the ASCII code of each character into a string of digits + char ascii_str[5]; // Sufficient to hold up to 4 digits plus a null terminator snprintf(ascii_str, sizeof(ascii_str), "%u", ascii_code); - // For each digit of the ASCII code, simulate pressing ALT + Numpad key - furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT); for(size_t j = 0; ascii_str[j] != '\0'; ++j) { - uint16_t keycode = get_keycode_by_name((const char[]){"NUMPAD_", ascii_str[j], '\0'}, 3); + // Construct the keycode name string for the current digit + char keycode_name[11]; // "NUMPAD_" + one digit + '\0' + snprintf(keycode_name, sizeof(keycode_name), "NUMPAD_%c", ascii_str[j]); + + // Find the corresponding keycode for the Numpad key + uint16_t keycode = get_keycode_by_name(keycode_name, strlen(keycode_name)); if(keycode != HID_KEYBOARD_NONE) { - furi_hal_hid_kb_press(keycode); // Press the Numpad key - furi_hal_hid_kb_release(keycode); // Release the Numpad key + furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT); // Press the ALT key + furi_hal_hid_kb_press(keycode); // Press the Numpad key + furi_hal_hid_kb_release(keycode); // Release the Numpad key + furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT); // Release the ALT key } } - furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT); // Release the ALT key after each character } // Simulate pressing the Enter key to mimic println behavior From e4b72c003e0417d1f6f3895b7cf74c9e6361e0c5 Mon Sep 17 00:00:00 2001 From: oldip Date: Mon, 18 Mar 2024 18:35:12 +0800 Subject: [PATCH 06/20] rewrite js_badusb_altPrintln function --- .../system/js_app/modules/js_badusb.c | 54 +++++++++++-------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/applications/system/js_app/modules/js_badusb.c b/applications/system/js_app/modules/js_badusb.c index c2acd553ae..74555ed116 100644 --- a/applications/system/js_app/modules/js_badusb.c +++ b/applications/system/js_app/modules/js_badusb.c @@ -53,16 +53,16 @@ static const struct { {"F11", HID_KEYBOARD_F11}, {"F12", HID_KEYBOARD_F12}, - {"NUMPAD_0", HID_KEYPAD_0}, - {"NUMPAD_1", HID_KEYPAD_1}, - {"NUMPAD_2", HID_KEYPAD_2}, - {"NUMPAD_3", HID_KEYPAD_3}, - {"NUMPAD_4", HID_KEYPAD_4}, - {"NUMPAD_5", HID_KEYPAD_5}, - {"NUMPAD_6", HID_KEYPAD_6}, - {"NUMPAD_7", HID_KEYPAD_7}, - {"NUMPAD_8", HID_KEYPAD_8}, - {"NUMPAD_9", HID_KEYPAD_9}, + {"NUM0", HID_KEYPAD_0}, + {"NUM1", HID_KEYPAD_1}, + {"NUM2", HID_KEYPAD_2}, + {"NUM3", HID_KEYPAD_3}, + {"NUM4", HID_KEYPAD_4}, + {"NUM5", HID_KEYPAD_5}, + {"NUM6", HID_KEYPAD_6}, + {"NUM7", HID_KEYPAD_7}, + {"NUM8", HID_KEYPAD_8}, + {"NUM9", HID_KEYPAD_9}, }; static void js_badusb_quit_free(JsBadusbInst* badusb) { @@ -399,7 +399,7 @@ static void js_badusb_println(struct mjs* mjs) { badusb_print(mjs, true); } -// Adjusting the altPrintln functionality to release the ALT key after each character +// Adding the altPrintln functionality with delay after each key press static void js_badusb_altPrintln(struct mjs* mjs) { mjs_val_t obj_inst = mjs_get(mjs, mjs_get_this(mjs), INST_PROP_NAME, ~0); JsBadusbInst* badusb = mjs_get_ptr(mjs, obj_inst); @@ -411,34 +411,44 @@ static void js_badusb_altPrintln(struct mjs* mjs) { for(size_t i = 0; i < str_len; ++i) { uint8_t ascii_code = (uint8_t)str[i]; - + // Convert the ASCII code of each character into a string of digits - char ascii_str[5]; // Sufficient to hold up to 4 digits plus a null terminator + char ascii_str[5]; // Enough to hold up to 4 digits plus a null terminator snprintf(ascii_str, sizeof(ascii_str), "%u", ascii_code); for(size_t j = 0; ascii_str[j] != '\0'; ++j) { - // Construct the keycode name string for the current digit - char keycode_name[11]; // "NUMPAD_" + one digit + '\0' - snprintf(keycode_name, sizeof(keycode_name), "NUMPAD_%c", ascii_str[j]); + furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT); // Press the ALT key + // Insert delay after pressing ALT key + delay(50); - // Find the corresponding keycode for the Numpad key - uint16_t keycode = get_keycode_by_name(keycode_name, strlen(keycode_name)); + uint16_t keycode = get_keycode_by_name((const char[]){"NUMPAD_", ascii_str[j], '\0'}, 3); if(keycode != HID_KEYBOARD_NONE) { - furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT); // Press the ALT key - furi_hal_hid_kb_press(keycode); // Press the Numpad key - furi_hal_hid_kb_release(keycode); // Release the Numpad key - furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT); // Release the ALT key + furi_hal_hid_kb_press(keycode); // Press the Numpad key + delay(20); // Delay after pressing the Numpad key + furi_hal_hid_kb_release(keycode); // Release the Numpad key + delay(20); // Delay after releasing the Numpad key } + + furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT); // Release the ALT key + delay(50); // Delay after releasing the ALT key } } // Simulate pressing the Enter key to mimic println behavior furi_hal_hid_kb_press(HID_KEYBOARD_RETURN); + delay(20); furi_hal_hid_kb_release(HID_KEYBOARD_RETURN); + delay(50); // Optional delay after pressing Enter mjs_return(mjs, MJS_UNDEFINED); } +void delay(int milliseconds) { + // Implementation depends on your environment + // For example, in a POSIX environment, you might use usleep + usleep(milliseconds * 1000); // usleep takes microseconds +} + static void* js_badusb_create(struct mjs* mjs, mjs_val_t* object) { JsBadusbInst* badusb = malloc(sizeof(JsBadusbInst)); mjs_val_t badusb_obj = mjs_mk_object(mjs); From 728ed4d11abdd6294ce1211b5b1dfc6aa094a2e8 Mon Sep 17 00:00:00 2001 From: oldip Date: Mon, 18 Mar 2024 18:45:59 +0800 Subject: [PATCH 07/20] rewrite the js_badusb_altPrintln function and fix the missing pointer --- .../system/js_app/modules/js_badusb.c | 73 ++++++++----------- 1 file changed, 32 insertions(+), 41 deletions(-) diff --git a/applications/system/js_app/modules/js_badusb.c b/applications/system/js_app/modules/js_badusb.c index 74555ed116..adf93c125e 100644 --- a/applications/system/js_app/modules/js_badusb.c +++ b/applications/system/js_app/modules/js_badusb.c @@ -52,7 +52,7 @@ static const struct { {"F10", HID_KEYBOARD_F10}, {"F11", HID_KEYBOARD_F11}, {"F12", HID_KEYBOARD_F12}, - + {"NUM0", HID_KEYPAD_0}, {"NUM1", HID_KEYPAD_1}, {"NUM2", HID_KEYPAD_2}, @@ -399,56 +399,48 @@ static void js_badusb_println(struct mjs* mjs) { badusb_print(mjs, true); } -// Adding the altPrintln functionality with delay after each key press -static void js_badusb_altPrintln(struct mjs* mjs) { - mjs_val_t obj_inst = mjs_get(mjs, mjs_get_this(mjs), INST_PROP_NAME, ~0); - JsBadusbInst* badusb = mjs_get_ptr(mjs, obj_inst); - furi_assert(badusb); - - mjs_val_t str_val = mjs_arg(mjs, 0); - size_t str_len; - const char* str = mjs_get_string(mjs, &str_val, &str_len); - - for(size_t i = 0; i < str_len; ++i) { - uint8_t ascii_code = (uint8_t)str[i]; - - // Convert the ASCII code of each character into a string of digits - char ascii_str[5]; // Enough to hold up to 4 digits plus a null terminator - snprintf(ascii_str, sizeof(ascii_str), "%u", ascii_code); +// Simulates typing a character using the ALT key and Numpad ASCII code +static void alt_numpad_type_character(char character) { + // Press and hold the ALT key + furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT); + + // Convert the character to its ASCII value and then to a string + char ascii_str[4]; + snprintf(ascii_str, sizeof(ascii_str), "%03d", character); + + // For each digit in the ASCII string, create a temp string and press/release the corresponding numpad key + for(size_t i = 0; ascii_str[i] != '\0'; i++) { + char temp_str[2] = {ascii_str[i], '\0'}; // Create a temporary string for each digit + uint16_t numpad_keycode = get_keycode_by_name(temp_str, 1); + furi_hal_hid_kb_press(numpad_keycode); + furi_hal_hid_kb_release(numpad_keycode); + } - for(size_t j = 0; ascii_str[j] != '\0'; ++j) { - furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT); // Press the ALT key - // Insert delay after pressing ALT key - delay(50); + // Release the ALT key + furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT); +} - uint16_t keycode = get_keycode_by_name((const char[]){"NUMPAD_", ascii_str[j], '\0'}, 3); - if(keycode != HID_KEYBOARD_NONE) { - furi_hal_hid_kb_press(keycode); // Press the Numpad key - delay(20); // Delay after pressing the Numpad key - furi_hal_hid_kb_release(keycode); // Release the Numpad key - delay(20); // Delay after releasing the Numpad key - } +static void js_badusb_altPrintln(struct mjs* mjs) { + mjs_val_t obj_string = mjs_arg(mjs, 0); + if (!mjs_is_string(obj_string)) { + mjs_prepend_errorf(mjs, MJS_BAD_ARGS_ERROR, "Expected a string argument"); + mjs_return(mjs, MJS_UNDEFINED); + return; + } - furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT); // Release the ALT key - delay(50); // Delay after releasing the ALT key - } + size_t text_len; + const char* text = mjs_get_string(mjs, &obj_string, &text_len); + for (size_t i = 0; i < text_len; ++i) { + alt_numpad_type_character(text[i]); } - // Simulate pressing the Enter key to mimic println behavior + // Simulate pressing the ENTER key at the end furi_hal_hid_kb_press(HID_KEYBOARD_RETURN); - delay(20); furi_hal_hid_kb_release(HID_KEYBOARD_RETURN); - delay(50); // Optional delay after pressing Enter mjs_return(mjs, MJS_UNDEFINED); } -void delay(int milliseconds) { - // Implementation depends on your environment - // For example, in a POSIX environment, you might use usleep - usleep(milliseconds * 1000); // usleep takes microseconds -} - static void* js_badusb_create(struct mjs* mjs, mjs_val_t* object) { JsBadusbInst* badusb = malloc(sizeof(JsBadusbInst)); mjs_val_t badusb_obj = mjs_mk_object(mjs); @@ -463,7 +455,6 @@ static void* js_badusb_create(struct mjs* mjs, mjs_val_t* object) { mjs_set(mjs, badusb_obj, "println", ~0, MJS_MK_FN(js_badusb_println)); // Register the altPrintln method for calling from JavaScript mjs_set(mjs, badusb_obj, "altPrintln", ~0, MJS_MK_FN(js_badusb_altPrintln)); - *object = badusb_obj; return badusb; } From e09b39fccd5d0996fa956469d610c69cd9127f8e Mon Sep 17 00:00:00 2001 From: oldip Date: Mon, 18 Mar 2024 18:59:48 +0800 Subject: [PATCH 08/20] add delay --- applications/system/js_app/modules/js_badusb.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/applications/system/js_app/modules/js_badusb.c b/applications/system/js_app/modules/js_badusb.c index adf93c125e..88c0c0bc00 100644 --- a/applications/system/js_app/modules/js_badusb.c +++ b/applications/system/js_app/modules/js_badusb.c @@ -400,24 +400,30 @@ static void js_badusb_println(struct mjs* mjs) { } // Simulates typing a character using the ALT key and Numpad ASCII code -static void alt_numpad_type_character(char character) { +static void alt_numpad_type_character(struct mjs* mjs, char character) { + const uint32_t delay_ms = 50; // Example delay + // Press and hold the ALT key furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT); + js_delay_with_flags(mjs, delay_ms); // Convert the character to its ASCII value and then to a string char ascii_str[4]; snprintf(ascii_str, sizeof(ascii_str), "%03d", character); - // For each digit in the ASCII string, create a temp string and press/release the corresponding numpad key + // For each digit in the ASCII string, press and release the corresponding numpad key for(size_t i = 0; ascii_str[i] != '\0'; i++) { - char temp_str[2] = {ascii_str[i], '\0'}; // Create a temporary string for each digit - uint16_t numpad_keycode = get_keycode_by_name(temp_str, 1); + char digitStr[2] = {ascii_str[i], '\0'}; + uint16_t numpad_keycode = get_keycode_by_name(digitStr, 1); furi_hal_hid_kb_press(numpad_keycode); + js_delay_with_flags(mjs, delay_ms); furi_hal_hid_kb_release(numpad_keycode); + js_delay_with_flags(mjs, delay_ms); } // Release the ALT key furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT); + js_delay_with_flags(mjs, delay_ms); } static void js_badusb_altPrintln(struct mjs* mjs) { @@ -431,7 +437,7 @@ static void js_badusb_altPrintln(struct mjs* mjs) { size_t text_len; const char* text = mjs_get_string(mjs, &obj_string, &text_len); for (size_t i = 0; i < text_len; ++i) { - alt_numpad_type_character(text[i]); + alt_numpad_type_character(mjs, text[i]); } // Simulate pressing the ENTER key at the end From 1bee1c7749bfe72938502becbe8b00a37c7a2ec9 Mon Sep 17 00:00:00 2001 From: oldip Date: Mon, 18 Mar 2024 19:08:13 +0800 Subject: [PATCH 09/20] modify delay to 200ms --- applications/system/js_app/modules/js_badusb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/system/js_app/modules/js_badusb.c b/applications/system/js_app/modules/js_badusb.c index 88c0c0bc00..fba9e276cf 100644 --- a/applications/system/js_app/modules/js_badusb.c +++ b/applications/system/js_app/modules/js_badusb.c @@ -401,7 +401,7 @@ static void js_badusb_println(struct mjs* mjs) { // Simulates typing a character using the ALT key and Numpad ASCII code static void alt_numpad_type_character(struct mjs* mjs, char character) { - const uint32_t delay_ms = 50; // Example delay + const uint32_t delay_ms = 200; // Example delay // Press and hold the ALT key furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT); From 97a50221299e9ff1866f03b89fa9d49983c30b19 Mon Sep 17 00:00:00 2001 From: oldip Date: Mon, 18 Mar 2024 21:52:50 +0800 Subject: [PATCH 10/20] simulating key presses by converting each character to its ASCII code and then inputting it using the ALT+numeric keypad sequence --- .../system/js_app/modules/js_badusb.c | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/applications/system/js_app/modules/js_badusb.c b/applications/system/js_app/modules/js_badusb.c index fba9e276cf..1064d9c3c7 100644 --- a/applications/system/js_app/modules/js_badusb.c +++ b/applications/system/js_app/modules/js_badusb.c @@ -399,31 +399,33 @@ static void js_badusb_println(struct mjs* mjs) { badusb_print(mjs, true); } -// Simulates typing a character using the ALT key and Numpad ASCII code -static void alt_numpad_type_character(struct mjs* mjs, char character) { - const uint32_t delay_ms = 200; // Example delay - - // Press and hold the ALT key +static bool ducky_altchar(struct mjs* mjs, const char* ascii_code) { + // Hold the ALT key furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT); - js_delay_with_flags(mjs, delay_ms); + js_delay_with_flags(mjs, 200); - // Convert the character to its ASCII value and then to a string - char ascii_str[4]; - snprintf(ascii_str, sizeof(ascii_str), "%03d", character); - - // For each digit in the ASCII string, press and release the corresponding numpad key - for(size_t i = 0; ascii_str[i] != '\0'; i++) { - char digitStr[2] = {ascii_str[i], '\0'}; - uint16_t numpad_keycode = get_keycode_by_name(digitStr, 1); + // Loop through each digit of the ASCII code and press the corresponding numpad key + for(size_t i = 0; ascii_code[i] != '\0'; i++) { + char digit = ascii_code[i] - '0'; // Convert char to digit + uint16_t numpad_keycode = get_keycode_by_digit(digit); furi_hal_hid_kb_press(numpad_keycode); - js_delay_with_flags(mjs, delay_ms); + js_delay_with_flags(mjs, 200); furi_hal_hid_kb_release(numpad_keycode); - js_delay_with_flags(mjs, delay_ms); + js_delay_with_flags(mjs, 200); } // Release the ALT key furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT); - js_delay_with_flags(mjs, delay_ms); + js_delay_with_flags(mjs, 200); + + return true; // Indicate success +} + +static void alt_numpad_type_character(struct mjs* mjs, char character) { + char ascii_str[4]; + snprintf(ascii_str, sizeof(ascii_str), "%u", (unsigned char)character); + + ducky_altchar(mjs, ascii_str); } static void js_badusb_altPrintln(struct mjs* mjs) { From dbb0408f9961e6ca37e0b968e5beea1119a1b8a3 Mon Sep 17 00:00:00 2001 From: oldip Date: Mon, 18 Mar 2024 21:56:56 +0800 Subject: [PATCH 11/20] Construct the numpad key name --- applications/system/js_app/modules/js_badusb.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/applications/system/js_app/modules/js_badusb.c b/applications/system/js_app/modules/js_badusb.c index 1064d9c3c7..307e0bb133 100644 --- a/applications/system/js_app/modules/js_badusb.c +++ b/applications/system/js_app/modules/js_badusb.c @@ -406,8 +406,12 @@ static bool ducky_altchar(struct mjs* mjs, const char* ascii_code) { // Loop through each digit of the ASCII code and press the corresponding numpad key for(size_t i = 0; ascii_code[i] != '\0'; i++) { - char digit = ascii_code[i] - '0'; // Convert char to digit - uint16_t numpad_keycode = get_keycode_by_digit(digit); + char digitChar[5] = {'N', 'U', 'M', ascii_code[i], '\0'}; // Construct the numpad key name + uint16_t numpad_keycode = get_keycode_by_name(digitChar, strlen(digitChar)); + if(numpad_keycode == HID_KEYBOARD_NONE) { + // Handle error or unsupported keycode + continue; + } furi_hal_hid_kb_press(numpad_keycode); js_delay_with_flags(mjs, 200); furi_hal_hid_kb_release(numpad_keycode); From a5a9292aa1e4c961f4b3a4f19d2cc4184894cdb6 Mon Sep 17 00:00:00 2001 From: oldip Date: Mon, 18 Mar 2024 22:11:05 +0800 Subject: [PATCH 12/20] less delay --- applications/system/js_app/modules/js_badusb.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/applications/system/js_app/modules/js_badusb.c b/applications/system/js_app/modules/js_badusb.c index 307e0bb133..2993f0b711 100644 --- a/applications/system/js_app/modules/js_badusb.c +++ b/applications/system/js_app/modules/js_badusb.c @@ -402,7 +402,7 @@ static void js_badusb_println(struct mjs* mjs) { static bool ducky_altchar(struct mjs* mjs, const char* ascii_code) { // Hold the ALT key furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT); - js_delay_with_flags(mjs, 200); + js_delay_with_flags(mjs, 50); // Loop through each digit of the ASCII code and press the corresponding numpad key for(size_t i = 0; ascii_code[i] != '\0'; i++) { @@ -413,14 +413,14 @@ static bool ducky_altchar(struct mjs* mjs, const char* ascii_code) { continue; } furi_hal_hid_kb_press(numpad_keycode); - js_delay_with_flags(mjs, 200); + js_delay_with_flags(mjs, 50); furi_hal_hid_kb_release(numpad_keycode); - js_delay_with_flags(mjs, 200); + js_delay_with_flags(mjs, 50); } // Release the ALT key furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT); - js_delay_with_flags(mjs, 200); + js_delay_with_flags(mjs, 50); return true; // Indicate success } From 4737c3af8c25724ff8b0fbfac5706d93d62f2581 Mon Sep 17 00:00:00 2001 From: oldip Date: Mon, 18 Mar 2024 22:14:51 +0800 Subject: [PATCH 13/20] Function to simulate typing text using ALT key + Numpad ASCII code without adding a newline at the end --- .../system/js_app/modules/js_badusb.c | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/applications/system/js_app/modules/js_badusb.c b/applications/system/js_app/modules/js_badusb.c index 2993f0b711..2863abaa44 100644 --- a/applications/system/js_app/modules/js_badusb.c +++ b/applications/system/js_app/modules/js_badusb.c @@ -432,6 +432,34 @@ static void alt_numpad_type_character(struct mjs* mjs, char character) { ducky_altchar(mjs, ascii_str); } +// Function to simulate typing text using ALT key + Numpad ASCII code without adding a newline at the end +static void js_badusb_altPrint(struct mjs* mjs) { + mjs_val_t obj_string = mjs_arg(mjs, 0); + if (!mjs_is_string(obj_string)) { + mjs_prepend_errorf(mjs, MJS_BAD_ARGS_ERROR, "Expected a string argument"); + mjs_return(mjs, MJS_UNDEFINED); + return; + } + + size_t text_len; + const char* text = mjs_get_string(mjs, &obj_string, &text_len); + for (size_t i = 0; i < text_len; ++i) { + alt_numpad_type_character(mjs, text[i]); + } + + mjs_return(mjs, MJS_UNDEFINED); +} + +// Updated js_badusb_altPrintln function, now simply calls js_badusb_altPrint for the text part +static void js_badusb_altPrintln(struct mjs* mjs) { + // First, print the text without a newline + js_badusb_altPrint(mjs); + + // Then, simulate pressing the ENTER key to add a newline + furi_hal_hid_kb_press(HID_KEYBOARD_RETURN); + furi_hal_hid_kb_release(HID_KEYBOARD_RETURN); +} + static void js_badusb_altPrintln(struct mjs* mjs) { mjs_val_t obj_string = mjs_arg(mjs, 0); if (!mjs_is_string(obj_string)) { From 879d654f70bc40efa78e14d4c3a37099f4a1c315 Mon Sep 17 00:00:00 2001 From: oldip Date: Mon, 18 Mar 2024 22:15:25 +0800 Subject: [PATCH 14/20] Function to simulate typing text using ALT key + Numpad ASCII code without adding a newline at the end --- applications/system/js_app/modules/js_badusb.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/applications/system/js_app/modules/js_badusb.c b/applications/system/js_app/modules/js_badusb.c index 2863abaa44..ae11d8abd3 100644 --- a/applications/system/js_app/modules/js_badusb.c +++ b/applications/system/js_app/modules/js_badusb.c @@ -493,6 +493,8 @@ static void* js_badusb_create(struct mjs* mjs, mjs_val_t* object) { mjs_set(mjs, badusb_obj, "release", ~0, MJS_MK_FN(js_badusb_release)); mjs_set(mjs, badusb_obj, "print", ~0, MJS_MK_FN(js_badusb_print)); mjs_set(mjs, badusb_obj, "println", ~0, MJS_MK_FN(js_badusb_println)); + // Register the altPrint method for calling from JavaScript + mjs_set(mjs, badusb_obj, "altPrint", ~0, MJS_MK_FN(js_badusb_altPrint)); // Register the altPrintln method for calling from JavaScript mjs_set(mjs, badusb_obj, "altPrintln", ~0, MJS_MK_FN(js_badusb_altPrintln)); *object = badusb_obj; From eff99a6015cd33696c8ce6e9e00668a1f5c6b829 Mon Sep 17 00:00:00 2001 From: oldip Date: Mon, 18 Mar 2024 22:21:15 +0800 Subject: [PATCH 15/20] orrectly defined js_badusb_altPrint function --- applications/system/js_app/modules/js_badusb.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/applications/system/js_app/modules/js_badusb.c b/applications/system/js_app/modules/js_badusb.c index ae11d8abd3..dcd848bc63 100644 --- a/applications/system/js_app/modules/js_badusb.c +++ b/applications/system/js_app/modules/js_badusb.c @@ -432,7 +432,7 @@ static void alt_numpad_type_character(struct mjs* mjs, char character) { ducky_altchar(mjs, ascii_str); } -// Function to simulate typing text using ALT key + Numpad ASCII code without adding a newline at the end +// Correctly defined js_badusb_altPrint function static void js_badusb_altPrint(struct mjs* mjs) { mjs_val_t obj_string = mjs_arg(mjs, 0); if (!mjs_is_string(obj_string)) { @@ -450,16 +450,6 @@ static void js_badusb_altPrint(struct mjs* mjs) { mjs_return(mjs, MJS_UNDEFINED); } -// Updated js_badusb_altPrintln function, now simply calls js_badusb_altPrint for the text part -static void js_badusb_altPrintln(struct mjs* mjs) { - // First, print the text without a newline - js_badusb_altPrint(mjs); - - // Then, simulate pressing the ENTER key to add a newline - furi_hal_hid_kb_press(HID_KEYBOARD_RETURN); - furi_hal_hid_kb_release(HID_KEYBOARD_RETURN); -} - static void js_badusb_altPrintln(struct mjs* mjs) { mjs_val_t obj_string = mjs_arg(mjs, 0); if (!mjs_is_string(obj_string)) { From 23033095b1f4a4693f8aa993dfc68ff3760ae2c6 Mon Sep 17 00:00:00 2001 From: oldip Date: Mon, 18 Mar 2024 22:22:45 +0800 Subject: [PATCH 16/20] Correctly defined js_badusb_altPrint function --- applications/system/js_app/modules/js_badusb.c | 1 + 1 file changed, 1 insertion(+) diff --git a/applications/system/js_app/modules/js_badusb.c b/applications/system/js_app/modules/js_badusb.c index dcd848bc63..6ec06f6fd1 100644 --- a/applications/system/js_app/modules/js_badusb.c +++ b/applications/system/js_app/modules/js_badusb.c @@ -450,6 +450,7 @@ static void js_badusb_altPrint(struct mjs* mjs) { mjs_return(mjs, MJS_UNDEFINED); } +// Ensure js_badusb_altPrintln is only defined once and correctly calls js_badusb_altPrint static void js_badusb_altPrintln(struct mjs* mjs) { mjs_val_t obj_string = mjs_arg(mjs, 0); if (!mjs_is_string(obj_string)) { From 88a080c168ca8032fecb54335187fcc95d60bdbf Mon Sep 17 00:00:00 2001 From: oldip Date: Mon, 18 Mar 2024 22:33:19 +0800 Subject: [PATCH 17/20] Remove the delay --- applications/system/js_app/modules/js_badusb.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/applications/system/js_app/modules/js_badusb.c b/applications/system/js_app/modules/js_badusb.c index 6ec06f6fd1..d3b26dfe94 100644 --- a/applications/system/js_app/modules/js_badusb.c +++ b/applications/system/js_app/modules/js_badusb.c @@ -402,7 +402,7 @@ static void js_badusb_println(struct mjs* mjs) { static bool ducky_altchar(struct mjs* mjs, const char* ascii_code) { // Hold the ALT key furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT); - js_delay_with_flags(mjs, 50); + // js_delay_with_flags(mjs, 50); // Loop through each digit of the ASCII code and press the corresponding numpad key for(size_t i = 0; ascii_code[i] != '\0'; i++) { @@ -413,14 +413,14 @@ static bool ducky_altchar(struct mjs* mjs, const char* ascii_code) { continue; } furi_hal_hid_kb_press(numpad_keycode); - js_delay_with_flags(mjs, 50); + // js_delay_with_flags(mjs, 50); furi_hal_hid_kb_release(numpad_keycode); - js_delay_with_flags(mjs, 50); + // js_delay_with_flags(mjs, 50); } // Release the ALT key furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT); - js_delay_with_flags(mjs, 50); + // js_delay_with_flags(mjs, 50); return true; // Indicate success } From 4d1678a99aabdb284522fd617c17b07c9ef8326b Mon Sep 17 00:00:00 2001 From: oldip Date: Mon, 18 Mar 2024 23:14:06 +0800 Subject: [PATCH 18/20] Improve ALT+Numpad typing simulation with adjustable delay AND Testing Completed --- .../system/js_app/modules/js_badusb.c | 69 +++++++++---------- 1 file changed, 31 insertions(+), 38 deletions(-) diff --git a/applications/system/js_app/modules/js_badusb.c b/applications/system/js_app/modules/js_badusb.c index d3b26dfe94..5348b0598c 100644 --- a/applications/system/js_app/modules/js_badusb.c +++ b/applications/system/js_app/modules/js_badusb.c @@ -399,79 +399,72 @@ static void js_badusb_println(struct mjs* mjs) { badusb_print(mjs, true); } +// Simulate pressing a character using ALT+Numpad ASCII code static bool ducky_altchar(struct mjs* mjs, const char* ascii_code) { + (void)mjs; // Mark the mjs parameter as unused // Hold the ALT key furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT); - // js_delay_with_flags(mjs, 50); - // Loop through each digit of the ASCII code and press the corresponding numpad key + // Press the corresponding numpad key for each digit of the ASCII code for(size_t i = 0; ascii_code[i] != '\0'; i++) { char digitChar[5] = {'N', 'U', 'M', ascii_code[i], '\0'}; // Construct the numpad key name uint16_t numpad_keycode = get_keycode_by_name(digitChar, strlen(digitChar)); if(numpad_keycode == HID_KEYBOARD_NONE) { - // Handle error or unsupported keycode - continue; + continue; // Skip if keycode not found } furi_hal_hid_kb_press(numpad_keycode); - // js_delay_with_flags(mjs, 50); furi_hal_hid_kb_release(numpad_keycode); - // js_delay_with_flags(mjs, 50); } // Release the ALT key furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT); - // js_delay_with_flags(mjs, 50); - return true; // Indicate success + return true; } -static void alt_numpad_type_character(struct mjs* mjs, char character) { +// Type a character using the ALT+Numpad method and apply delay between characters +static void alt_numpad_type_character(struct mjs* mjs, char character, uint32_t delay_ms) { char ascii_str[4]; snprintf(ascii_str, sizeof(ascii_str), "%u", (unsigned char)character); - ducky_altchar(mjs, ascii_str); + js_delay_with_flags(mjs, delay_ms); // Apply delay between characters } -// Correctly defined js_badusb_altPrint function -static void js_badusb_altPrint(struct mjs* mjs) { +// Handles the text input and decides whether to add a newline at the end, including character delays +static void altPrint(struct mjs* mjs, bool ln) { + size_t text_len = 0; + uint32_t delay_ms = 0; // Initialize delay to 0 ms mjs_val_t obj_string = mjs_arg(mjs, 0); - if (!mjs_is_string(obj_string)) { - mjs_prepend_errorf(mjs, MJS_BAD_ARGS_ERROR, "Expected a string argument"); - mjs_return(mjs, MJS_UNDEFINED); - return; - } - - size_t text_len; const char* text = mjs_get_string(mjs, &obj_string, &text_len); - for (size_t i = 0; i < text_len; ++i) { - alt_numpad_type_character(mjs, text[i]); - } - - mjs_return(mjs, MJS_UNDEFINED); -} -// Ensure js_badusb_altPrintln is only defined once and correctly calls js_badusb_altPrint -static void js_badusb_altPrintln(struct mjs* mjs) { - mjs_val_t obj_string = mjs_arg(mjs, 0); - if (!mjs_is_string(obj_string)) { - mjs_prepend_errorf(mjs, MJS_BAD_ARGS_ERROR, "Expected a string argument"); - mjs_return(mjs, MJS_UNDEFINED); - return; + if (mjs_nargs(mjs) == 2) { + // If delay is specified as the second argument + delay_ms = (uint32_t)mjs_get_int32(mjs, mjs_arg(mjs, 1)); } - size_t text_len; - const char* text = mjs_get_string(mjs, &obj_string, &text_len); for (size_t i = 0; i < text_len; ++i) { - alt_numpad_type_character(mjs, text[i]); + alt_numpad_type_character(mjs, text[i], delay_ms); } - // Simulate pressing the ENTER key at the end - furi_hal_hid_kb_press(HID_KEYBOARD_RETURN); - furi_hal_hid_kb_release(HID_KEYBOARD_RETURN); + if (ln) { + // Simulate pressing the ENTER key at the end if needed + furi_hal_hid_kb_press(HID_KEYBOARD_RETURN); + furi_hal_hid_kb_release(HID_KEYBOARD_RETURN); + } mjs_return(mjs, MJS_UNDEFINED); } +// Wrapper function for altPrint without newline +static void js_badusb_altPrint(struct mjs* mjs) { + altPrint(mjs, false); +} + +// Wrapper function for altPrint with newline +static void js_badusb_altPrintln(struct mjs* mjs) { + altPrint(mjs, true); +} + static void* js_badusb_create(struct mjs* mjs, mjs_val_t* object) { JsBadusbInst* badusb = malloc(sizeof(JsBadusbInst)); mjs_val_t badusb_obj = mjs_mk_object(mjs); From 1bf0d3ab9ae167967691ad14b8bfc0a993b75cab Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Tue, 19 Mar 2024 04:41:12 +0000 Subject: [PATCH 19/20] JS: Improve badusb.altPrint() robustness --- .../system/js_app/modules/js_badusb.c | 124 +++++++----------- 1 file changed, 51 insertions(+), 73 deletions(-) diff --git a/applications/system/js_app/modules/js_badusb.c b/applications/system/js_app/modules/js_badusb.c index 5348b0598c..389539be58 100644 --- a/applications/system/js_app/modules/js_badusb.c +++ b/applications/system/js_app/modules/js_badusb.c @@ -52,7 +52,7 @@ static const struct { {"F10", HID_KEYBOARD_F10}, {"F11", HID_KEYBOARD_F11}, {"F12", HID_KEYBOARD_F12}, - + {"NUM0", HID_KEYPAD_0}, {"NUM1", HID_KEYPAD_1}, {"NUM2", HID_KEYPAD_2}, @@ -325,7 +325,35 @@ static void js_badusb_release(struct mjs* mjs) { mjs_return(mjs, MJS_UNDEFINED); } -static void badusb_print(struct mjs* mjs, bool ln) { +// Make sure NUMLOCK is enabled for altchar +static void ducky_numlock_on() { + if((furi_hal_hid_get_led_state() & HID_KB_LED_NUM) == 0) { + furi_hal_hid_kb_press(HID_KEYBOARD_LOCK_NUM_LOCK); + furi_hal_hid_kb_release(HID_KEYBOARD_LOCK_NUM_LOCK); + } +} + +// Simulate pressing a character using ALT+Numpad ASCII code +static void ducky_altchar(const char* ascii_code) { + // Hold the ALT key + furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT); + + // Press the corresponding numpad key for each digit of the ASCII code + for(size_t i = 0; ascii_code[i] != '\0'; i++) { + char digitChar[5] = {'N', 'U', 'M', ascii_code[i], '\0'}; // Construct the numpad key name + uint16_t numpad_keycode = get_keycode_by_name(digitChar, strlen(digitChar)); + if(numpad_keycode == HID_KEYBOARD_NONE) { + continue; // Skip if keycode not found + } + furi_hal_hid_kb_press(numpad_keycode); + furi_hal_hid_kb_release(numpad_keycode); + } + + // Release the ALT key + furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT); +} + +static void badusb_print(struct mjs* mjs, bool ln, bool alt) { mjs_val_t obj_inst = mjs_get(mjs, mjs_get_this(mjs), INST_PROP_NAME, ~0); JsBadusbInst* badusb = mjs_get_ptr(mjs, obj_inst); furi_assert(badusb); @@ -371,10 +399,20 @@ static void badusb_print(struct mjs* mjs, bool ln) { return; } + if(alt) { + ducky_numlock_on(); + } for(size_t i = 0; i < text_len; i++) { - uint16_t keycode = HID_ASCII_TO_KEY(text_str[i]); - furi_hal_hid_kb_press(keycode); - furi_hal_hid_kb_release(keycode); + if(alt) { + // Convert character to ascii numeric value + char ascii_str[4]; + snprintf(ascii_str, sizeof(ascii_str), "%u", (uint8_t)text_str[i]); + ducky_altchar(ascii_str); + } else { + uint16_t keycode = HID_ASCII_TO_KEY(text_str[i]); + furi_hal_hid_kb_press(keycode); + furi_hal_hid_kb_release(keycode); + } if(delay_val > 0) { bool need_exit = js_delay_with_flags(mjs, delay_val); if(need_exit) { @@ -392,77 +430,19 @@ static void badusb_print(struct mjs* mjs, bool ln) { } static void js_badusb_print(struct mjs* mjs) { - badusb_print(mjs, false); + badusb_print(mjs, false, false); } static void js_badusb_println(struct mjs* mjs) { - badusb_print(mjs, true); -} - -// Simulate pressing a character using ALT+Numpad ASCII code -static bool ducky_altchar(struct mjs* mjs, const char* ascii_code) { - (void)mjs; // Mark the mjs parameter as unused - // Hold the ALT key - furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT); - - // Press the corresponding numpad key for each digit of the ASCII code - for(size_t i = 0; ascii_code[i] != '\0'; i++) { - char digitChar[5] = {'N', 'U', 'M', ascii_code[i], '\0'}; // Construct the numpad key name - uint16_t numpad_keycode = get_keycode_by_name(digitChar, strlen(digitChar)); - if(numpad_keycode == HID_KEYBOARD_NONE) { - continue; // Skip if keycode not found - } - furi_hal_hid_kb_press(numpad_keycode); - furi_hal_hid_kb_release(numpad_keycode); - } - - // Release the ALT key - furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT); - - return true; -} - -// Type a character using the ALT+Numpad method and apply delay between characters -static void alt_numpad_type_character(struct mjs* mjs, char character, uint32_t delay_ms) { - char ascii_str[4]; - snprintf(ascii_str, sizeof(ascii_str), "%u", (unsigned char)character); - ducky_altchar(mjs, ascii_str); - js_delay_with_flags(mjs, delay_ms); // Apply delay between characters -} - -// Handles the text input and decides whether to add a newline at the end, including character delays -static void altPrint(struct mjs* mjs, bool ln) { - size_t text_len = 0; - uint32_t delay_ms = 0; // Initialize delay to 0 ms - mjs_val_t obj_string = mjs_arg(mjs, 0); - const char* text = mjs_get_string(mjs, &obj_string, &text_len); - - if (mjs_nargs(mjs) == 2) { - // If delay is specified as the second argument - delay_ms = (uint32_t)mjs_get_int32(mjs, mjs_arg(mjs, 1)); - } - - for (size_t i = 0; i < text_len; ++i) { - alt_numpad_type_character(mjs, text[i], delay_ms); - } - - if (ln) { - // Simulate pressing the ENTER key at the end if needed - furi_hal_hid_kb_press(HID_KEYBOARD_RETURN); - furi_hal_hid_kb_release(HID_KEYBOARD_RETURN); - } - - mjs_return(mjs, MJS_UNDEFINED); + badusb_print(mjs, true, false); } -// Wrapper function for altPrint without newline -static void js_badusb_altPrint(struct mjs* mjs) { - altPrint(mjs, false); +static void js_badusb_alt_print(struct mjs* mjs) { + badusb_print(mjs, false, true); } -// Wrapper function for altPrint with newline -static void js_badusb_altPrintln(struct mjs* mjs) { - altPrint(mjs, true); +static void js_badusb_alt_println(struct mjs* mjs) { + badusb_print(mjs, true, true); } static void* js_badusb_create(struct mjs* mjs, mjs_val_t* object) { @@ -477,10 +457,8 @@ static void* js_badusb_create(struct mjs* mjs, mjs_val_t* object) { mjs_set(mjs, badusb_obj, "release", ~0, MJS_MK_FN(js_badusb_release)); mjs_set(mjs, badusb_obj, "print", ~0, MJS_MK_FN(js_badusb_print)); mjs_set(mjs, badusb_obj, "println", ~0, MJS_MK_FN(js_badusb_println)); - // Register the altPrint method for calling from JavaScript - mjs_set(mjs, badusb_obj, "altPrint", ~0, MJS_MK_FN(js_badusb_altPrint)); - // Register the altPrintln method for calling from JavaScript - mjs_set(mjs, badusb_obj, "altPrintln", ~0, MJS_MK_FN(js_badusb_altPrintln)); + mjs_set(mjs, badusb_obj, "altPrint", ~0, MJS_MK_FN(js_badusb_alt_print)); + mjs_set(mjs, badusb_obj, "altPrintln", ~0, MJS_MK_FN(js_badusb_alt_println)); *object = badusb_obj; return badusb; } From f48848ca4db25b182074d936937d5c6add406ed4 Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Tue, 19 Mar 2024 04:45:54 +0000 Subject: [PATCH 20/20] JS: Add BadUSB Alt+Numpad example --- .../system/js_app/examples/apps/Scripts/badusb_demo.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/applications/system/js_app/examples/apps/Scripts/badusb_demo.js b/applications/system/js_app/examples/apps/Scripts/badusb_demo.js index bbeedd4457..8605020233 100644 --- a/applications/system/js_app/examples/apps/Scripts/badusb_demo.js +++ b/applications/system/js_app/examples/apps/Scripts/badusb_demo.js @@ -26,6 +26,12 @@ if (badusb.isConnected()) { badusb.println("Flipper Name: " + flipper.getName()); badusb.println("Battery level: " + to_string(flipper.getBatteryCharge()) + "%"); + // Alt+Numpad method works only on Windows!!! + badusb.altPrintln("This was printed with Alt+Numpad method!"); + + // There's also badusb.print() and badusb.altPrint() + // which don't add the return at the end + notify.success(); } else { print("USB not connected");