SimulateKeyPress simulate a key press. It can be used to mock user stdin and test your application. Example: go func() { keyboard.SimulateKeyPress("Hello") // Simulate key press for every letter in string keyboard.SimulateKeyPress(keys.Enter) // Simulate key press for Ent
(input ...interface{})
| 177 | // keyboard.SimulateKeyPress('x', keys.Down, 'a') // Simulate key presses for multiple inputs |
| 178 | // }() |
| 179 | func SimulateKeyPress(input ...interface{}) error { |
| 180 | for _, key := range input { |
| 181 | // Check if key is a keys.Key |
| 182 | if key, ok := key.(keys.Key); ok { |
| 183 | mockChannel <- key |
| 184 | return nil |
| 185 | } |
| 186 | |
| 187 | // Check if key is a rune |
| 188 | if key, ok := key.(rune); ok { |
| 189 | mockChannel <- keys.Key{ |
| 190 | Code: keys.RuneKey, |
| 191 | Runes: []rune{key}, |
| 192 | } |
| 193 | |
| 194 | return nil |
| 195 | } |
| 196 | |
| 197 | // Check if key is a string |
| 198 | if key, ok := key.(string); ok { |
| 199 | for _, r := range key { |
| 200 | mockChannel <- keys.Key{ |
| 201 | Code: keys.RuneKey, |
| 202 | Runes: []rune{r}, |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | return nil |
| 207 | } |
| 208 | |
| 209 | // Check if key is a KeyCode |
| 210 | if key, ok := key.(keys.KeyCode); ok { |
| 211 | mockChannel <- keys.Key{ |
| 212 | Code: key, |
| 213 | } |
| 214 | |
| 215 | return nil |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | return nil |
| 220 | } |
no outgoing calls