123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package remdeshid
- import "fmt"
- //Convert a HIDCommand to bytes that can be sent over the USBKVM device
- func ConvHIDCommandToBytes(cmd HIDCommand) ([]byte, error) {
- // Convert the HID command to bytes
- var data []byte
- if cmd.EventType == FRONTEND_OPR_TYPE_KEYBOARD_WRITE {
- data = []byte{OPR_TYPE_KEYBOARD_WRITE}
- /* Keyboard Subtypes */
- if len(cmd.Data) == 1 && cmd.Data[0] >= 32 && cmd.Data[0] <= 127 {
- //Valid ASCII character
- if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN {
- data = append(data, SUBTYPE_KEYBOARD_ASCII_PRESS)
- } else if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_UP {
- data = append(data, SUBTYPE_KEYBOARD_ASCII_RELEASE)
- } else {
- //Key Click
- data = append(data, SUBTYPE_KEYBOARD_ASCII_WRITE)
- }
- data = append(data, cmd.Data[0])
- return data, nil
- } else if isFuncKey(cmd.Data) {
- //Function Key
- if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN {
- data = append(data, SUBTYPE_KEYBOARD_FUNCTKEY_PRESS)
- } else {
- data = append(data, SUBTYPE_KEYBOARD_FUNCTKEY_RELEASE)
- }
- data = append(data, funcKeyToByte(cmd.Data))
- if data[0] == 0xFF {
- return nil, fmt.Errorf("invalid function key: %v", cmd.Data)
- }
- return data, nil
- } else if isModifierKey(cmd.Data) {
- //Modifier Key
- if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN {
- data = append(data, SUBTYPE_KEYBOARD_MODIFIER_PRESS)
- } else {
- data = append(data, SUBTYPE_KEYBOARD_MODIFIER_RELEASE)
- }
- data = append(data, modifierKeyToByte(cmd.Data))
- if data[0] == 0xFF {
- return nil, fmt.Errorf("invalid modifier key: %v", cmd.Data)
- }
- return data, nil
- } else if isOtherKeys(cmd.Data) {
- //Other Keys
- if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN {
- data = append(data, SUBTYPE_KEYBOARD_OTHERKEY_PRESS)
- } else if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_UP {
- data = append(data, SUBTYPE_KEYBOARD_OTHERKEY_RELEASE)
- } else {
- return nil, fmt.Errorf("invalid HID command subtype: %v", cmd.Data)
- }
- data = append(data, nonAsciiKeysToBytes(cmd.Data)...)
- return data, nil
- } else if isNumpadKey(cmd.Data) {
- //Numpad Keys
- if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN {
- data = append(data, SUBTYPE_KEYBOARD_NUMPAD_PRESS)
- } else if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_UP {
- data = append(data, SUBTYPE_KEYBOARD_NUMPAD_RELEASE)
- } else {
- return nil, fmt.Errorf("invalid HID command subtype: %v", cmd.Data)
- }
- data = append(data, numpadKeyToByte(string(cmd.Data)))
- return data, nil
- } else if cmd.Data == "NumLock" {
- if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN {
- data = append(data, SUBTYPE_KEYBOARD_SPECIAL_NUMLOCK)
- data = append(data, 0x00)
- return data, nil
- }
- return nil, fmt.Errorf("numLock do not support key up")
- } else if cmd.Data == "Pause" {
- if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN {
- data = append(data, SUBTYPE_KEYBOARD_SPECIAL_PAUSE)
- data = append(data, 0x00)
- return data, nil
- }
- return nil, fmt.Errorf("pause do not support key up")
- } else if cmd.Data == "PrintScreen" {
- if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_UP {
- data = append(data, SUBTYPE_KEYBOARD_SPECIAL_PRINT_SCREEN)
- data = append(data, 0x00)
- return data, nil
- }
- return nil, fmt.Errorf("printScreen do not support key down")
- } else if cmd.Data == "ScrollLock" {
- if cmd.EventSubType == FRONTEND_SUBTYPE_KEYBOARD_KEY_DOWN {
- data = append(data, SUBTYPE_KEYBOARD_SPECIAL_SCROLL_LOCK)
- data = append(data, 0x00)
- return data, nil
- }
- return nil, fmt.Errorf("scrollLock do not support key up")
- } else if cmd.Data == "ContextMenu" {
- //Special Key: ContextMenu
- //TODO: Implement ContextMenu
- return nil, fmt.Errorf("ContextMenu not implemented")
- } else if cmd.Data == "Ctrl+Alt+Del" {
- //Special Key: Ctrl+Alt+Del
- data = append(data, SUBTYPE_KEYBOARD_SPECIAL_CTRLALTDEL)
- data = append(data, 0x00)
- return data, nil
- } else {
- return nil, fmt.Errorf("invalid HID command subtype: %v", cmd.Data)
- }
- } else if cmd.EventType == FRONTEND_OPR_TYPE_MOUSE_WRITE {
- /* Mouse Write Event */
- data = []byte{OPR_TYPE_MOUSE_WRITE}
- } else {
- return nil, fmt.Errorf("invalid HID command type: %s", cmd.EventType)
- }
- return data, nil
- }
|