Skip to content

Commit f946ce1

Browse files
committed
Merge remote-tracking branch 'sereinity/feat/delay'
2 parents 707c5fe + 685f99c commit f946ce1

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ To press/release modifiers, `-M`/`-m` can be used respectively.
2525
wtype -M ctrl c -m ctrl
2626
```
2727

28+
To alter delay between keystrokes, `-d`.
29+
30+
```
31+
# delay of 0 when typing "foo", 120ms on "bar"
32+
wtype foo -d 120 bar
33+
34+
# also applied on stdin
35+
echo everything | wtype -d 12 -
36+
```
2837

2938
To press/release a named key (as given by [xkb_keysym_get_name](https://xkbcommon.org/doc/current/group__keysyms.html)),
3039
`-P`/`-p` can be used.
@@ -33,6 +42,7 @@ To press/release a named key (as given by [xkb_keysym_get_name](https://xkbcommo
3342
# Press and release the Left key
3443
wtype -P left -p left
3544
```
45+
3646
Note that when wtype terminates, all the pressed keys/modifiers get released, as the compositor destroys the associated
3747
virtual keyboard object. To help performing a more complicated sequence of key presses, `-s` can be used to insert delays into the stream of key events.
3848

main.c

+14
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct wtype_command {
4545
struct {
4646
unsigned int *key_codes;
4747
size_t key_codes_len;
48+
unsigned int delay_ms;
4849
};
4950
unsigned int single_key_code;
5051
enum wtype_mod mod;
@@ -205,6 +206,7 @@ static void parse_args(struct wtype *wtype, int argc, const char *argv[])
205206
bool raw_text = false;
206207
bool prefix_with_space = false;
207208
bool use_stdin = false;
209+
unsigned int delay_ms = 0;
208210
for (int i = 1; i < argc; i++) {
209211
struct wtype_command *cmd = &wtype->commands[wtype->command_count];
210212
if (!raw_text && !strcmp("--", argv[i])) {
@@ -216,6 +218,7 @@ static void parse_args(struct wtype *wtype, int argc, const char *argv[])
216218
}
217219
use_stdin = true;
218220
cmd->type = WTYPE_COMMAND_TEXT_STDIN;
221+
cmd->delay_ms = delay_ms;
219222
wtype->command_count++;
220223
} else if (!raw_text && argv[i][0] == '-'){
221224
if (i == argc - 1) {
@@ -242,6 +245,12 @@ static void parse_args(struct wtype *wtype, int argc, const char *argv[])
242245
if (cmd->sleep_ms <= 0) {
243246
fail("Invalid sleep time");
244247
}
248+
} else if (!strcmp("-d", argv[i])) {
249+
// Set delay between type keystrokes
250+
delay_ms = atoi(argv[i + 1]);
251+
if (delay_ms <= 0) {
252+
fail("Invalid sleep time");
253+
}
245254
} else if (!strcmp("-k", argv[i])) {
246255
//size_t k;
247256
xkb_keysym_t ks = xkb_keysym_from_name(argv[i + 1], XKB_KEYSYM_CASE_INSENSITIVE);
@@ -252,6 +261,7 @@ static void parse_args(struct wtype *wtype, int argc, const char *argv[])
252261
cmd->key_codes = malloc(sizeof(cmd->key_codes[0]));
253262
cmd->key_codes_len = 1;
254263
cmd->key_codes[0] = get_key_code_by_xkb(wtype, ks);
264+
cmd->delay_ms = delay_ms;
255265
} else if (!strcmp("-P", argv[i]) || !strcmp("-p", argv[i])) {
256266
// Press/release a key
257267
xkb_keysym_t ks = xkb_keysym_from_name(argv[i + 1], XKB_KEYSYM_CASE_INSENSITIVE);
@@ -269,6 +279,7 @@ static void parse_args(struct wtype *wtype, int argc, const char *argv[])
269279
} else {
270280
// Text
271281
cmd->type = WTYPE_COMMAND_TEXT;
282+
cmd->delay_ms = delay_ms;
272283
wtype->command_count++;
273284

274285
size_t raw_len = strlen(argv[i]) + 2; // NULL byte and the potential space
@@ -349,6 +360,7 @@ static void run_text(struct wtype *wtype, struct wtype_command *cmd)
349360
{
350361
for (size_t i = 0; i < cmd->key_codes_len; i++) {
351362
type_keycode(wtype, cmd->key_codes[i]);
363+
usleep(cmd->delay_ms * 1000);
352364
}
353365
}
354366

@@ -389,6 +401,7 @@ static void run_text_stdin(struct wtype *wtype, struct wtype_command *cmd)
389401
upload_keymap(wtype);
390402
for (size_t i = 0; i < cmd->key_codes_len; i++) {
391403
type_keycode(wtype, cmd->key_codes[i]);
404+
usleep(cmd->delay_ms * 1000);
392405
}
393406
cmd->key_codes_len = 0;
394407
}
@@ -398,6 +411,7 @@ static void run_text_stdin(struct wtype *wtype, struct wtype_command *cmd)
398411
upload_keymap(wtype);
399412
for (size_t i = 0; i < cmd->key_codes_len; i++) {
400413
type_keycode(wtype, cmd->key_codes[i]);
414+
usleep(cmd->delay_ms * 1000);
401415
}
402416
}
403417

man/wtype.1

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ Release key KEY.
4545
Type (press and release) key KEY.
4646

4747
.TP
48+
.BR \-d\ \fITIME\fR
49+
Sleep for TIME milliseconds between keystrokes when typing texts.
50+
Can be used multiple times, default 0.
51+
.TP
52+
4853
.BR \-s\ \fITIME\fR
4954
Sleep for TIME milliseconds before interpreting the following options. This
5055
can be used to perform more complicated modifier sequences.

0 commit comments

Comments
 (0)