convert a string of keys to an array of RemoteKeyCode special keys are: UP; DOWN; LEFT; RIGHT; HOME; BACK; VOLP; VOLM; MUTE; PWR; ENTER you can also directly use defined keys in types.go like KEYCODE_TV_INPUT_HDMI_1 each special key is separated by a semicolon example: `A ;UP; tst;KEYCODE_TV_INPUT_H
(data string)
| 11 | // each special key is separated by a semicolon |
| 12 | // example: `A ;UP; tst;KEYCODE_TV_INPUT_HDMI_1` will be parsed as [RemoteKeyCode_KEYCODE_A, RemoteKeyCode_KEYCODE_SPACE, RemoteKeyCode_KEYCODE_DPAD_UP, RemoteKeyCode_KEYCODE_SPACE, RemoteKeyCode_KEYCODE_T, RemoteKeyCode_KEYCODE_S, RemoteKeyCode_KEYCODE_T, KEYCODE_TV_INPUT_HDMI_1] |
| 13 | func ParseKeys(data string) []RemoteKeyCode { |
| 14 | ret := []RemoteKeyCode{} |
| 15 | special := map[string]RemoteKeyCode{ |
| 16 | "UP": RemoteKeyCode_KEYCODE_DPAD_UP, |
| 17 | "DOWN": RemoteKeyCode_KEYCODE_DPAD_DOWN, |
| 18 | "LEFT": RemoteKeyCode_KEYCODE_DPAD_LEFT, |
| 19 | "RIGHT": RemoteKeyCode_KEYCODE_DPAD_RIGHT, |
| 20 | "HOME": RemoteKeyCode_KEYCODE_HOME, |
| 21 | "BACK": RemoteKeyCode_KEYCODE_BACK, |
| 22 | "MUTE": RemoteKeyCode_KEYCODE_MUTE, |
| 23 | "VOLP": RemoteKeyCode_KEYCODE_VOLUME_UP, |
| 24 | "VOLM": RemoteKeyCode_KEYCODE_VOLUME_DOWN, |
| 25 | "PWR": RemoteKeyCode_KEYCODE_POWER, |
| 26 | "ENTER": RemoteKeyCode_KEYCODE_ENTER, |
| 27 | } |
| 28 | |
| 29 | for _, k := range strings.Split(data, ";") { |
| 30 | if len(k) > 8 && k[0:8] == "KEYCODE_" { |
| 31 | // if KEYCODE is directly specified |
| 32 | ret = append(ret, RemoteKeyCode(RemoteKeyCode_value[k])) |
| 33 | } else { |
| 34 | if val, ok := special[k]; ok { |
| 35 | // if it is a special key |
| 36 | ret = append(ret, val) |
| 37 | } else { |
| 38 | for _, x := range k { |
| 39 | // loop over each character |
| 40 | c := string(x) |
| 41 | if regexp.MustCompile(`^[A-Z]{1}$`).MatchString(c) { |
| 42 | // if A-Z |
| 43 | ret = append(ret, RemoteKeyCode(RemoteKeyCode_value["KEYCODE_"+c])) |
| 44 | } else if regexp.MustCompile(`^[0-9]{1}$`).MatchString(c) { |
| 45 | // if digit |
| 46 | ret = append(ret, RemoteKeyCode(RemoteKeyCode_value["KEYCODE_NUMPAD_"+c])) |
| 47 | } else if c == " " { |
| 48 | // if digit |
| 49 | ret = append(ret, RemoteKeyCode_KEYCODE_SPACE) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | return ret |
| 56 | } |
no test coverage detected