Skip to content

Instantly share code, notes, and snippets.

@jukefr
Created February 11, 2020 23:52
Show Gist options
  • Save jukefr/5afa5422588ebc79025686e818ae5647 to your computer and use it in GitHub Desktop.
Save jukefr/5afa5422588ebc79025686e818ae5647 to your computer and use it in GitHub Desktop.
convert text to keystrokes and send to a Virtualbox machine via VBoxManage (scuffed clipboard)
#!/usr/bin/env bash
# requires bash 4+ (for associative arrays)
# description: convert text to keystrokes and send to a Virtualbox machine via VBoxManage
# (when guest additions are not an option, for adding ssh-keys and such, it is quite slow)
# usage: ./type $1 $2
# $1: virtual machine name
# $2: text to type
# example: ./type CoreOS "Hello World!"
declare -A keys
result=()
# Decimal scan codes for key down press
# ref: https://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html
keys=(
["1!"]=2
["2@"]=3
["3#"]=4
["4$"]=5
["5%"]=6
["6^"]=7
["7&"]=8
["8*"]=9
["9("]=10
["0)"]=11
["-_"]=12
["=+"]=13
["qQ"]=16
["wW"]=17
["eE"]=18
["rR"]=19
["tT"]=20
["yY"]=21
["uU"]=22
["iI"]=23
["oO"]=24
["pP"]=25
["[{"]=26
["]}"]=27
["aA"]=30
["sS"]=31
["dD"]=32
["fF"]=33
["gG"]=34
["hH"]=35
["jJ"]=36
["kK"]=37
["lL"]=38
[";:"]=39
["'\""]=40
["zZ"]=44
["xX"]=45
["cC"]=46
["vV"]=47
["bB"]=48
["nN"]=49
["mM"]=50
[",<"]=51
[".>"]=52
["/?"]=53
["\`~"]=41
["\\|"]=43
[" "]=57
)
vm="$1"
text="$2"
for ((i = 0; i < ${#text}; i++));
do
letter="${text:$i:1}"
for scancode in "${!keys[@]}";
do
if [[ $letter == "${scancode:0:1}" ]];
then
result+=( "${keys[$scancode]}" )
result+=( "$((${keys[$scancode]} + 128))" )
fi
if [[ $letter == "${scancode:1:1}" ]];
then
# press shift
result+=( "42" )
result+=( "${keys[$scancode]}" )
result+=( "$((${keys[$scancode]} + 128))" )
# lift shift
result+=( "170" )
fi
done
done
for decimal in "${result[@]}";
do
hex_result="$(printf '%02x ' "$decimal")"
VBoxManage controlvm $vm keyboardputscancode $hex_result
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment