Adjusting the altPrintln functionality to release the ALT key after each character

This commit is contained in:
oldip
2024-03-18 15:52:51 +08:00
parent e790ffad3a
commit fc3a1f28b2

View File

@@ -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);