more proper type checking

This commit is contained in:
Siyuan Miao 2025-09-10 02:42:36 +02:00
parent f58e5476bf
commit 854cdc8e82
1 changed files with 23 additions and 13 deletions

View File

@ -1111,28 +1111,38 @@ func rpcKeyboardReportMulti(ctx context.Context, macro []map[string]any) (usbgad
}
var modifier byte
if m, ok := step["modifier"].(float64); ok {
switch m := step["modifier"].(type) {
case uint8:
modifier = m
case int:
modifier = byte(m)
case float64:
modifier = byte(int(m))
} else if mi, ok := step["modifier"].(int); ok {
modifier = byte(mi)
} else if mb, ok := step["modifier"].(uint8); ok {
modifier = mb
default:
return last, fmt.Errorf("invalid modifier type: %T", m)
}
var keys []byte
if arr, ok := step["keys"].([]any); ok {
switch k := step["keys"].(type) {
case []byte:
keys = k
case []any:
arr := k
keys = make([]byte, 0, len(arr))
for _, v := range arr {
if f, ok := v.(float64); ok {
switch f := v.(type) {
case uint8:
keys = append(keys, f)
case int:
keys = append(keys, byte(f))
case float64:
keys = append(keys, byte(int(f)))
} else if i, ok := v.(int); ok {
keys = append(keys, byte(i))
} else if b, ok := v.(uint8); ok {
keys = append(keys, b)
default:
return last, fmt.Errorf("invalid key type: %T", f)
}
}
} else if bs, ok := step["keys"].([]byte); ok {
keys = bs
default:
return last, fmt.Errorf("invalid keys type: %T", k)
}
// Use context-aware sleep that can be cancelled