Skip to content

Commit

Permalink
Implement keystroke delay again.
Browse files Browse the repository at this point in the history
  • Loading branch information
kareltucek committed Oct 25, 2020
1 parent fdc3523 commit a766f95
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ The following grammar is supported:
COMMAND = setStickyModsEnabled {0|never|smart|always|1}
COMMAND = setCompensateDiagonalSpeed {0|1}
COMMAND = setDebounceDelay <time in ms, at most 250 (NUMBER)>
COMMAND = setKeystrokeDelay <time in ms, at most 65535 (NUMBER)>
COMMAND = setReg <register index (NUMBER)> <value (NUMBER)>
COMMAND = setEmergencyKey KEYID
COMMAND = {addReg|subReg|mulReg} <register index (NUMBER)> <value (NUMBER)>
Expand Down Expand Up @@ -285,7 +286,6 @@ The following grammar is supported:
#REMOVEWD#
##########
COMMAND = setSplitCompositeKeystroke {0|1}
COMMAND = setKeystrokeDelay <time in ms, at most 250 (NUMBER)>
COMMAND = setActivateOnRelease {0|1}
MODIFIER = suppressKeys

Expand Down Expand Up @@ -414,6 +414,7 @@ The following grammar is supported:
- `setStickyModsEnabled` globally turns on or off sticky modifiers. This affects only standard scancode actions. Macro actions (both gui and command ones) are always nonsticky, unless `sticky` flag is included in `tapKey|holdKey|pressKey` commands. Default value is `smart`, which is the official behaviour - i.e., `<alt/ctrl/gui> + <tab/arrows>` are sticky. Furthermore `0 == never` and `1 == always`.
- `setCompensateDiagonalSpeed` will divide diagonal mouse speed by sqrt(2) if enabled.
- `setDebounceDelay <time in ms, at most 250>` prevents key state from changing for some time after every state change. This is needed because contacts of mechanical switches can bounce after contact and therefore change state multiple times in span of a few milliseconds. Official firmware debounce time is 50 ms for both press and release. Recommended value is 10-50, default is 50.
- `setKeystrokeDelay <time in ms, at most 65535>` allows slowing down keyboard input. This is handy for lousily written RDP clients and other software which just scans keys once a while and processes them in wrong order if multiple keys have been pressed inbetween. In more detail, this setting adds a delay whenever a basic usb report is sent. During this delay, key matrix is still scanned and keys are debounced, but instead of activating, the keys are added into a queue to be replayed later.
- Argument parsing rules:
- `NUMBER` is parsed as a 32 bit signed integer and then assigned into the target variable. However, the target variable is often only 8 or 16 bit unsigned. If a number is prefixed with '#', it is interpretted as a register address (index). If a number is prefixed with '@', current macro index is added to the final value. `#key` returns activation key's hardware id. If prefixed with `%`, returns keyid of nth press event in the postponer queue (e.g., `%0` returns `KEYID` of first key which is postponed but not yet activated).
- `KEYMAPID` - is assumed to be 3 characters long abbreviation of a keymap.
Expand Down
10 changes: 10 additions & 0 deletions right/src/macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,13 @@ bool processSetDebounceDelayCommand(const char* arg, const char *argEnd)
return false;
}

bool processSetKeystrokeDelayCommand(const char* arg, const char *argEnd)
{
uint16_t delay = parseNUM(arg, argEnd);
KeystrokeDelay = delay;
return false;
}

bool processStatsRuntimeCommand()
{
int ms = Timer_GetElapsedTime(&s->currentMacroStartTime);
Expand Down Expand Up @@ -2275,6 +2282,9 @@ bool processCommand(const char* cmd, const char* cmdEnd)
else if(TokenMatches(cmd, cmdEnd, "setDebounceDelay")) {
return processSetDebounceDelayCommand(arg1, cmdEnd);
}
else if(TokenMatches(cmd, cmdEnd, "setKeystrokeDelay")) {
return processSetKeystrokeDelayCommand(arg1, cmdEnd);
}
else if(TokenMatches(cmd, cmdEnd, "setEmergencyKey")) {
return processSetEmergencyKeyCommand(arg1, cmdEnd);
}
Expand Down
21 changes: 21 additions & 0 deletions right/src/usb_report_updater.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ bool SuppressMods = false;
bool SuppressKeys = false;
sticky_strategy_t StickyModifierStrategy = Stick_Smart;

uint16_t KeystrokeDelay = 0;

key_state_t* EmergencyKey = NULL;

// Holds are applied on current base layer.
Expand Down Expand Up @@ -459,10 +461,23 @@ static void updateActiveUsbReports(void)
}

uint32_t UsbReportUpdateCounter;
void justPreprocessInput(void) {
// Make preprocessKeyState push new events into postponer queue.
// As a side-effect, postpone first cycle after we switch back to regular update loop
PostponerCore_PostponeNCycles(0);
for (uint8_t slotId=0; slotId<SLOT_COUNT; slotId++) {
for (uint8_t keyId=0; keyId<MAX_KEY_COUNT_PER_MODULE; keyId++) {
key_state_t *keyState = &KeyStates[slotId][keyId];

preprocessKeyState(keyState);
}
}
}

void UpdateUsbReports(void)
{
static uint32_t lastUpdateTime;
static uint32_t lastReportTime;

for (uint8_t keyId = 0; keyId < RIGHT_KEY_MATRIX_KEY_COUNT; keyId++) {
KeyStates[SlotId_RightKeyboardHalf][keyId].hardwareSwitchState = RightKeyMatrix.keyStates[keyId];
Expand All @@ -476,6 +491,11 @@ void UpdateUsbReports(void)
}
}

if(Timer_GetElapsedTime(&lastReportTime) < KeystrokeDelay) {
justPreprocessInput();
return;
}

lastUpdateTime = CurrentTime;
UsbReportUpdateCounter++;

Expand Down Expand Up @@ -506,6 +526,7 @@ void UpdateUsbReports(void)
//TODO: consider either making it atomic, or lowering semaphore reset delay
UsbReportUpdateSemaphore &= ~(1 << USB_BASIC_KEYBOARD_INTERFACE_INDEX);
}
lastReportTime = CurrentTime;
}
}

Expand Down
2 changes: 2 additions & 0 deletions right/src/usb_report_updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
extern bool PendingPostponedAndReleased;
extern bool ActivateOnRelease;
extern key_state_t* EmergencyKey;
extern uint16_t KeystrokeDelay;


// Functions:

Expand Down

0 comments on commit a766f95

Please sign in to comment.