Implemented GetKeyNumsFromOutput

This commit is contained in:
Dmitry Lyukov
2016-03-25 22:47:17 +03:00
parent 169c57db87
commit a179f24eff

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"log"
"os/exec"
"regexp"
"strconv"
"time"
)
@@ -17,7 +18,20 @@ const (
// Extract pressed keys from bufer buf
// It returns slice with key numbers in the same order
func GetKeyNumsFromOutput(buf []byte) []uint8 {
return make([]uint8, 0)
const KEY_NUM_STRING_RE = "press[ ]+(\\d+)"
re := regexp.MustCompile(KEY_NUM_STRING_RE)
res_byte := re.FindAll(buf, -1)
keyNums := make([]uint8, len(res_byte))
re = regexp.MustCompile("\\d+")
for i, line := range res_byte {
num_byte := re.Find(line)
if num, err := strconv.Atoi(string(num_byte)); err == nil {
keyNums[i] = uint8(num)
} else {
log.Fatal(err)
}
}
return keyNums
}
func main() {