aboutsummaryrefslogtreecommitdiff
path: root/unix/utils/sendkeys.awk
blob: 16a3fa9e33feb99642d467c5a8bd5d49bc812444 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env awk -f
#
# AWK script to send multiple `sendkey` commands to a QEMU virtual machine.
# It writes at a rate of roughly 40 keys per second, due to lower delays
# resulting in garbage output.
#
# It makes use of a TCP client created by an external utility, such as OpenBSD
# Netcat, to interact with QEMU's monitor and send a stream of `sendkey`
# commands. This is a practical way to transfer a small file or to script
# interactions with a terminal user interface.

BEGIN {
  # Set default delay if not provided via command-line args
  if (!delay) {
    delay = 0.025
  }

  # Define key mappings for common characters and symbols
  key["#"] = "backspace"
  key["	"] = "tab"
  key[" "] = "spc"
  key["!"] = "shift-1"
  key["\""] = "shift-apostrophe"
  key["#"] = "shift-3"
  key["$"] = "shift-4"
  key["%"] = "shift-5"
  key["&"] = "shift-7"
  key["'"] = "apostrophe"
  key["("] = "shift-9"
  key[")"] = "shift-0"
  key["*"] = "shift-8"
  key["+"] = "shift-equal"
  key[","] = "comma"
  key["-"] = "minus"
  key["."] = "dot"
  key["/"] = "slash"
  key[":"] = "shift-semicolon"
  key[";"] = "semicolon"
  key["<"] = "shift-comma"
  key["="] = "equal"
  key[">"] = "shift-dot"
  key["?"] = "shift-slash"
  key["@"] = "shift-2"

  # Map numbers
  for (i = 48; i < 48 + 10; ++i) {
    number = sprintf("%c", i)
    key[number] = number
  }

  # Map letters A-Z, including shift
  for (i = 65; i < 65 + 26; ++i) {
    key[sprintf("%c", i)] = sprintf("shift-%c", i + 32)
  }

  # Other symbols
  key["["] = "bracket_left"
  key["\\"] = "backslash"
  key["]"] = "bracket_right"
  key["^"] = "shift-6"
  key["_"] = "shift-minus"
  key["`"] = "grave_accent"
  key["{"] = "shift-bracket_left"
  key["|"] = "shift-backslash"
  key["}"] = "shift-bracket_right"
  key["~"] = "shift-grave_accent"
  key[""] = "delete"

  # Handle Super and Caps Lock key mappings (for remapping Caps to Super)
  key["capslock"] = "super"
  key["super"] = "super"

  # Handle other keys if needed
}

{
  split($0, chars, "")
  for (i = 1; i <= length($0); i++) {
    # Print sendkey command for the character, mapping it through the key[] array
    if (key[chars[i]] != "") {
      printf("sendkey %s\n", key[chars[i]])
    }
    system("sleep " delay) # Sleep for the defined delay
  }
  printf "sendkey ret\n" # Send "return" (enter) key at the end
}