Improving logic of mod keys when releasing a key

This commit is contained in:
matthisc 2023-02-26 10:23:33 +01:00
parent 2bb76e09c6
commit 958797062d

View File

@ -11,6 +11,10 @@ static uint8_t LOGITECH_HELLO[] = {0x00, 0x4F, 0x00, 0x04, 0xB0, 0x10, 0x00, 0x0
static uint8_t LOGITECH_KEEPALIVE[] = {0x00, 0x40, 0x00, 0x55, 0x6B}; static uint8_t LOGITECH_KEEPALIVE[] = {0x00, 0x40, 0x00, 0x55, 0x6B};
uint8_t prev_hid = 0; uint8_t prev_hid = 0;
static bool holding_ctrl = false;
static bool holding_shift = false;
static bool holding_alt = false;
static bool holding_gui = false;
#define RT_THRESHOLD 50 #define RT_THRESHOLD 50
#define LOGITECH_MIN_CHANNEL 2 #define LOGITECH_MIN_CHANNEL 2
@ -152,6 +156,33 @@ static void build_hid_packet(uint8_t mod, uint8_t hid, uint8_t* payload) {
checksum(payload, LOGITECH_HID_TEMPLATE_SIZE); checksum(payload, LOGITECH_HID_TEMPLATE_SIZE);
} }
static void release_key(
FuriHalSpiBusHandle* handle,
uint8_t* addr,
uint8_t addr_size,
uint8_t rate,
PluginState* plugin_state
) {
// This function release keys currently pressed, but keep pressing special keys
// if holding mod keys variable are set to true
uint8_t hid_payload[LOGITECH_HID_TEMPLATE_SIZE] = {0};
build_hid_packet(
0 | holding_ctrl
| holding_shift << 1
| holding_alt << 2
| holding_gui << 3,
0, hid_payload);
inject_packet(
handle,
addr,
addr_size,
rate,
hid_payload,
LOGITECH_HID_TEMPLATE_SIZE,
plugin_state); // empty hid packet
}
static void send_hid_packet( static void send_hid_packet(
FuriHalSpiBusHandle* handle, FuriHalSpiBusHandle* handle,
uint8_t* addr, uint8_t* addr,
@ -161,19 +192,15 @@ static void send_hid_packet(
uint8_t hid, uint8_t hid,
PluginState* plugin_state) { PluginState* plugin_state) {
uint8_t hid_payload[LOGITECH_HID_TEMPLATE_SIZE] = {0}; uint8_t hid_payload[LOGITECH_HID_TEMPLATE_SIZE] = {0};
build_hid_packet(0, 0, hid_payload); if(hid == prev_hid )
if(hid == prev_hid) release_key(handle, addr, addr_size, rate, plugin_state);
inject_packet(
handle,
addr,
addr_size,
rate,
hid_payload,
LOGITECH_HID_TEMPLATE_SIZE,
plugin_state); // empty hid packet
prev_hid = hid; prev_hid = hid;
build_hid_packet(mod, hid, hid_payload); build_hid_packet(mod
| holding_ctrl
| holding_shift << 1
| holding_alt << 2
| holding_gui << 3, hid, hid_payload);
inject_packet( inject_packet(
handle, addr, addr_size, rate, hid_payload, LOGITECH_HID_TEMPLATE_SIZE, plugin_state); handle, addr, addr_size, rate, hid_payload, LOGITECH_HID_TEMPLATE_SIZE, plugin_state);
furi_delay_ms(12); furi_delay_ms(12);
@ -269,7 +296,9 @@ static bool mj_process_ducky_line(
} else if(strncmp(line_tmp, "ALT", strlen("ALT")) == 0) { } else if(strncmp(line_tmp, "ALT", strlen("ALT")) == 0) {
line_tmp = &line_tmp[mj_ducky_get_command_len(line_tmp) + 1]; line_tmp = &line_tmp[mj_ducky_get_command_len(line_tmp) + 1];
if(!mj_get_ducky_key(line_tmp, strlen(line_tmp), &dk)) return false; if(!mj_get_ducky_key(line_tmp, strlen(line_tmp), &dk)) return false;
send_hid_packet(handle, addr, addr_size, rate, dk.mod | 4, dk.hid, plugin_state); holding_alt = true;
send_hid_packet(handle, addr, addr_size, rate, dk.mod, dk.hid, plugin_state);
holding_alt = false;
return true; return true;
} else if( } else if(
strncmp(line_tmp, "GUI", strlen("GUI")) == 0 || strncmp(line_tmp, "GUI", strlen("GUI")) == 0 ||
@ -277,33 +306,47 @@ static bool mj_process_ducky_line(
strncmp(line_tmp, "COMMAND", strlen("COMMAND")) == 0) { strncmp(line_tmp, "COMMAND", strlen("COMMAND")) == 0) {
line_tmp = &line_tmp[mj_ducky_get_command_len(line_tmp) + 1]; line_tmp = &line_tmp[mj_ducky_get_command_len(line_tmp) + 1];
if(!mj_get_ducky_key(line_tmp, strlen(line_tmp), &dk)) return false; if(!mj_get_ducky_key(line_tmp, strlen(line_tmp), &dk)) return false;
send_hid_packet(handle, addr, addr_size, rate, dk.mod | 8, dk.hid, plugin_state); holding_gui = true;
send_hid_packet(handle, addr, addr_size, rate, dk.mod, dk.hid, plugin_state);
holding_gui = false;
return true; return true;
} else if( } else if(
strncmp(line_tmp, "CTRL-ALT", strlen("CTRL-ALT")) == 0 || strncmp(line_tmp, "CTRL-ALT", strlen("CTRL-ALT")) == 0 ||
strncmp(line_tmp, "CONTROL-ALT", strlen("CONTROL-ALT")) == 0) { strncmp(line_tmp, "CONTROL-ALT", strlen("CONTROL-ALT")) == 0) {
line_tmp = &line_tmp[mj_ducky_get_command_len(line_tmp) + 1]; line_tmp = &line_tmp[mj_ducky_get_command_len(line_tmp) + 1];
if(!mj_get_ducky_key(line_tmp, strlen(line_tmp), &dk)) return false; if(!mj_get_ducky_key(line_tmp, strlen(line_tmp), &dk)) return false;
send_hid_packet(handle, addr, addr_size, rate, dk.mod | 4 | 1, dk.hid, plugin_state); holding_ctrl = true;
holding_alt = true;
send_hid_packet(handle, addr, addr_size, rate, dk.mod, dk.hid, plugin_state);
holding_ctrl = false;
holding_alt = false;
return true; return true;
} else if( } else if(
strncmp(line_tmp, "CTRL-SHIFT", strlen("CTRL-SHIFT")) == 0 || strncmp(line_tmp, "CTRL-SHIFT", strlen("CTRL-SHIFT")) == 0 ||
strncmp(line_tmp, "CONTROL-SHIFT", strlen("CONTROL-SHIFT")) == 0) { strncmp(line_tmp, "CONTROL-SHIFT", strlen("CONTROL-SHIFT")) == 0) {
line_tmp = &line_tmp[mj_ducky_get_command_len(line_tmp) + 1]; line_tmp = &line_tmp[mj_ducky_get_command_len(line_tmp) + 1];
if(!mj_get_ducky_key(line_tmp, strlen(line_tmp), &dk)) return false; if(!mj_get_ducky_key(line_tmp, strlen(line_tmp), &dk)) return false;
send_hid_packet(handle, addr, addr_size, rate, dk.mod | 1 | 2, dk.hid, plugin_state); holding_ctrl = true;
holding_shift = true;
send_hid_packet(handle, addr, addr_size, rate, dk.mod, dk.hid, plugin_state);
holding_ctrl = false;
holding_shift = false;
return true; return true;
} else if( } else if(
strncmp(line_tmp, "CTRL", strlen("CTRL")) == 0 || strncmp(line_tmp, "CTRL", strlen("CTRL")) == 0 ||
strncmp(line_tmp, "CONTROL", strlen("CONTROL")) == 0) { strncmp(line_tmp, "CONTROL", strlen("CONTROL")) == 0) {
line_tmp = &line_tmp[mj_ducky_get_command_len(line_tmp) + 1]; line_tmp = &line_tmp[mj_ducky_get_command_len(line_tmp) + 1];
if(!mj_get_ducky_key(line_tmp, strlen(line_tmp), &dk)) return false; if(!mj_get_ducky_key(line_tmp, strlen(line_tmp), &dk)) return false;
send_hid_packet(handle, addr, addr_size, rate, dk.mod | 1, dk.hid, plugin_state); holding_ctrl = true;
send_hid_packet(handle, addr, addr_size, rate, dk.mod, dk.hid, plugin_state);
holding_ctrl = false;
return true; return true;
} else if(strncmp(line_tmp, "SHIFT", strlen("SHIFT")) == 0) { } else if(strncmp(line_tmp, "SHIFT", strlen("SHIFT")) == 0) {
line_tmp = &line_tmp[mj_ducky_get_command_len(line_tmp) + 1]; line_tmp = &line_tmp[mj_ducky_get_command_len(line_tmp) + 1];
if(!mj_get_ducky_key(line_tmp, strlen(line_tmp), &dk)) return false; if(!mj_get_ducky_key(line_tmp, strlen(line_tmp), &dk)) return false;
send_hid_packet(handle, addr, addr_size, rate, dk.mod | 2, dk.hid, plugin_state); holding_shift = true;
send_hid_packet(handle, addr, addr_size, rate, dk.mod, dk.hid, plugin_state);
holding_shift = false;
return true; return true;
} else if( } else if(
strncmp(line_tmp, "ESC", strlen("ESC")) == 0 || strncmp(line_tmp, "ESC", strlen("ESC")) == 0 ||