| 1 |
package input |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strings" |
| 6 |
"unicode" |
| 7 |
|
| 8 |
"github.com/charmbracelet/x/ansi" |
| 9 |
) |
| 10 |
|
| 11 |
const ( |
| 12 |
|
| 13 |
|
| 14 |
KeyExtended = unicode.MaxRune + 1 |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
const ( |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
KeyUp rune = KeyExtended + iota + 1 |
| 23 |
KeyDown |
| 24 |
KeyRight |
| 25 |
KeyLeft |
| 26 |
KeyBegin |
| 27 |
KeyFind |
| 28 |
KeyInsert |
| 29 |
KeyDelete |
| 30 |
KeySelect |
| 31 |
KeyPgUp |
| 32 |
KeyPgDown |
| 33 |
KeyHome |
| 34 |
KeyEnd |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
KeyKpEnter |
| 39 |
KeyKpEqual |
| 40 |
KeyKpMultiply |
| 41 |
KeyKpPlus |
| 42 |
KeyKpComma |
| 43 |
KeyKpMinus |
| 44 |
KeyKpDecimal |
| 45 |
KeyKpDivide |
| 46 |
KeyKp0 |
| 47 |
KeyKp1 |
| 48 |
KeyKp2 |
| 49 |
KeyKp3 |
| 50 |
KeyKp4 |
| 51 |
KeyKp5 |
| 52 |
KeyKp6 |
| 53 |
KeyKp7 |
| 54 |
KeyKp8 |
| 55 |
KeyKp9 |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
KeyKpSep |
| 62 |
KeyKpUp |
| 63 |
KeyKpDown |
| 64 |
KeyKpLeft |
| 65 |
KeyKpRight |
| 66 |
KeyKpPgUp |
| 67 |
KeyKpPgDown |
| 68 |
KeyKpHome |
| 69 |
KeyKpEnd |
| 70 |
KeyKpInsert |
| 71 |
KeyKpDelete |
| 72 |
KeyKpBegin |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
KeyF1 |
| 77 |
KeyF2 |
| 78 |
KeyF3 |
| 79 |
KeyF4 |
| 80 |
KeyF5 |
| 81 |
KeyF6 |
| 82 |
KeyF7 |
| 83 |
KeyF8 |
| 84 |
KeyF9 |
| 85 |
KeyF10 |
| 86 |
KeyF11 |
| 87 |
KeyF12 |
| 88 |
KeyF13 |
| 89 |
KeyF14 |
| 90 |
KeyF15 |
| 91 |
KeyF16 |
| 92 |
KeyF17 |
| 93 |
KeyF18 |
| 94 |
KeyF19 |
| 95 |
KeyF20 |
| 96 |
KeyF21 |
| 97 |
KeyF22 |
| 98 |
KeyF23 |
| 99 |
KeyF24 |
| 100 |
KeyF25 |
| 101 |
KeyF26 |
| 102 |
KeyF27 |
| 103 |
KeyF28 |
| 104 |
KeyF29 |
| 105 |
KeyF30 |
| 106 |
KeyF31 |
| 107 |
KeyF32 |
| 108 |
KeyF33 |
| 109 |
KeyF34 |
| 110 |
KeyF35 |
| 111 |
KeyF36 |
| 112 |
KeyF37 |
| 113 |
KeyF38 |
| 114 |
KeyF39 |
| 115 |
KeyF40 |
| 116 |
KeyF41 |
| 117 |
KeyF42 |
| 118 |
KeyF43 |
| 119 |
KeyF44 |
| 120 |
KeyF45 |
| 121 |
KeyF46 |
| 122 |
KeyF47 |
| 123 |
KeyF48 |
| 124 |
KeyF49 |
| 125 |
KeyF50 |
| 126 |
KeyF51 |
| 127 |
KeyF52 |
| 128 |
KeyF53 |
| 129 |
KeyF54 |
| 130 |
KeyF55 |
| 131 |
KeyF56 |
| 132 |
KeyF57 |
| 133 |
KeyF58 |
| 134 |
KeyF59 |
| 135 |
KeyF60 |
| 136 |
KeyF61 |
| 137 |
KeyF62 |
| 138 |
KeyF63 |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
KeyCapsLock |
| 145 |
KeyScrollLock |
| 146 |
KeyNumLock |
| 147 |
KeyPrintScreen |
| 148 |
KeyPause |
| 149 |
KeyMenu |
| 150 |
|
| 151 |
KeyMediaPlay |
| 152 |
KeyMediaPause |
| 153 |
KeyMediaPlayPause |
| 154 |
KeyMediaReverse |
| 155 |
KeyMediaStop |
| 156 |
KeyMediaFastForward |
| 157 |
KeyMediaRewind |
| 158 |
KeyMediaNext |
| 159 |
KeyMediaPrev |
| 160 |
KeyMediaRecord |
| 161 |
|
| 162 |
KeyLowerVol |
| 163 |
KeyRaiseVol |
| 164 |
KeyMute |
| 165 |
|
| 166 |
KeyLeftShift |
| 167 |
KeyLeftAlt |
| 168 |
KeyLeftCtrl |
| 169 |
KeyLeftSuper |
| 170 |
KeyLeftHyper |
| 171 |
KeyLeftMeta |
| 172 |
KeyRightShift |
| 173 |
KeyRightAlt |
| 174 |
KeyRightCtrl |
| 175 |
KeyRightSuper |
| 176 |
KeyRightHyper |
| 177 |
KeyRightMeta |
| 178 |
KeyIsoLevel3Shift |
| 179 |
KeyIsoLevel5Shift |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
KeyBackspace = rune(ansi.DEL) |
| 184 |
KeyTab = rune(ansi.HT) |
| 185 |
KeyEnter = rune(ansi.CR) |
| 186 |
KeyReturn = KeyEnter |
| 187 |
KeyEscape = rune(ansi.ESC) |
| 188 |
KeyEsc = KeyEscape |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
KeySpace = rune(ansi.SP) |
| 193 |
) |
| 194 |
|
| 195 |
|
| 196 |
type KeyPressEvent Key |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
func (k KeyPressEvent) String() string { |
| 201 |
return Key(k).String() |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
func (k KeyPressEvent) Keystroke() string { |
| 219 |
return Key(k).Keystroke() |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
func (k KeyPressEvent) Key() Key { |
| 225 |
return Key(k) |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
type KeyReleaseEvent Key |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
func (k KeyReleaseEvent) String() string { |
| 234 |
return Key(k).String() |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
func (k KeyReleaseEvent) Keystroke() string { |
| 252 |
return Key(k).Keystroke() |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
func (k KeyReleaseEvent) Key() Key { |
| 259 |
return Key(k) |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
type KeyEvent interface { |
| 265 |
fmt.Stringer |
| 266 |
|
| 267 |
|
| 268 |
Key() Key |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
type Key struct { |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
Text string |
| 312 |
|
| 313 |
|
| 314 |
Mod KeyMod |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
Code rune |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
ShiftedCode rune |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
BaseCode rune |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
IsRepeat bool |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
func (k Key) String() string { |
| 357 |
if len(k.Text) > 0 && k.Text != " " { |
| 358 |
return k.Text |
| 359 |
} |
| 360 |
return k.Keystroke() |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
func (k Key) Keystroke() string { |
| 378 |
var sb strings.Builder |
| 379 |
if k.Mod.Contains(ModCtrl) && k.Code != KeyLeftCtrl && k.Code != KeyRightCtrl { |
| 380 |
sb.WriteString("ctrl+") |
| 381 |
} |
| 382 |
if k.Mod.Contains(ModAlt) && k.Code != KeyLeftAlt && k.Code != KeyRightAlt { |
| 383 |
sb.WriteString("alt+") |
| 384 |
} |
| 385 |
if k.Mod.Contains(ModShift) && k.Code != KeyLeftShift && k.Code != KeyRightShift { |
| 386 |
sb.WriteString("shift+") |
| 387 |
} |
| 388 |
if k.Mod.Contains(ModMeta) && k.Code != KeyLeftMeta && k.Code != KeyRightMeta { |
| 389 |
sb.WriteString("meta+") |
| 390 |
} |
| 391 |
if k.Mod.Contains(ModHyper) && k.Code != KeyLeftHyper && k.Code != KeyRightHyper { |
| 392 |
sb.WriteString("hyper+") |
| 393 |
} |
| 394 |
if k.Mod.Contains(ModSuper) && k.Code != KeyLeftSuper && k.Code != KeyRightSuper { |
| 395 |
sb.WriteString("super+") |
| 396 |
} |
| 397 |
|
| 398 |
if kt, ok := keyTypeString[k.Code]; ok { |
| 399 |
sb.WriteString(kt) |
| 400 |
} else { |
| 401 |
code := k.Code |
| 402 |
if k.BaseCode != 0 { |
| 403 |
|
| 404 |
|
| 405 |
code = k.BaseCode |
| 406 |
} |
| 407 |
|
| 408 |
switch code { |
| 409 |
case KeySpace: |
| 410 |
|
| 411 |
sb.WriteString("space") |
| 412 |
case KeyExtended: |
| 413 |
|
| 414 |
|
| 415 |
sb.WriteString(k.Text) |
| 416 |
default: |
| 417 |
sb.WriteRune(code) |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
return sb.String() |
| 422 |
} |
| 423 |
|
| 424 |
var keyTypeString = map[rune]string{ |
| 425 |
KeyEnter: "enter", |
| 426 |
KeyTab: "tab", |
| 427 |
KeyBackspace: "backspace", |
| 428 |
KeyEscape: "esc", |
| 429 |
KeySpace: "space", |
| 430 |
KeyUp: "up", |
| 431 |
KeyDown: "down", |
| 432 |
KeyLeft: "left", |
| 433 |
KeyRight: "right", |
| 434 |
KeyBegin: "begin", |
| 435 |
KeyFind: "find", |
| 436 |
KeyInsert: "insert", |
| 437 |
KeyDelete: "delete", |
| 438 |
KeySelect: "select", |
| 439 |
KeyPgUp: "pgup", |
| 440 |
KeyPgDown: "pgdown", |
| 441 |
KeyHome: "home", |
| 442 |
KeyEnd: "end", |
| 443 |
KeyKpEnter: "kpenter", |
| 444 |
KeyKpEqual: "kpequal", |
| 445 |
KeyKpMultiply: "kpmul", |
| 446 |
KeyKpPlus: "kpplus", |
| 447 |
KeyKpComma: "kpcomma", |
| 448 |
KeyKpMinus: "kpminus", |
| 449 |
KeyKpDecimal: "kpperiod", |
| 450 |
KeyKpDivide: "kpdiv", |
| 451 |
KeyKp0: "kp0", |
| 452 |
KeyKp1: "kp1", |
| 453 |
KeyKp2: "kp2", |
| 454 |
KeyKp3: "kp3", |
| 455 |
KeyKp4: "kp4", |
| 456 |
KeyKp5: "kp5", |
| 457 |
KeyKp6: "kp6", |
| 458 |
KeyKp7: "kp7", |
| 459 |
KeyKp8: "kp8", |
| 460 |
KeyKp9: "kp9", |
| 461 |
|
| 462 |
|
| 463 |
KeyKpSep: "kpsep", |
| 464 |
KeyKpUp: "kpup", |
| 465 |
KeyKpDown: "kpdown", |
| 466 |
KeyKpLeft: "kpleft", |
| 467 |
KeyKpRight: "kpright", |
| 468 |
KeyKpPgUp: "kppgup", |
| 469 |
KeyKpPgDown: "kppgdown", |
| 470 |
KeyKpHome: "kphome", |
| 471 |
KeyKpEnd: "kpend", |
| 472 |
KeyKpInsert: "kpinsert", |
| 473 |
KeyKpDelete: "kpdelete", |
| 474 |
KeyKpBegin: "kpbegin", |
| 475 |
|
| 476 |
KeyF1: "f1", |
| 477 |
KeyF2: "f2", |
| 478 |
KeyF3: "f3", |
| 479 |
KeyF4: "f4", |
| 480 |
KeyF5: "f5", |
| 481 |
KeyF6: "f6", |
| 482 |
KeyF7: "f7", |
| 483 |
KeyF8: "f8", |
| 484 |
KeyF9: "f9", |
| 485 |
KeyF10: "f10", |
| 486 |
KeyF11: "f11", |
| 487 |
KeyF12: "f12", |
| 488 |
KeyF13: "f13", |
| 489 |
KeyF14: "f14", |
| 490 |
KeyF15: "f15", |
| 491 |
KeyF16: "f16", |
| 492 |
KeyF17: "f17", |
| 493 |
KeyF18: "f18", |
| 494 |
KeyF19: "f19", |
| 495 |
KeyF20: "f20", |
| 496 |
KeyF21: "f21", |
| 497 |
KeyF22: "f22", |
| 498 |
KeyF23: "f23", |
| 499 |
KeyF24: "f24", |
| 500 |
KeyF25: "f25", |
| 501 |
KeyF26: "f26", |
| 502 |
KeyF27: "f27", |
| 503 |
KeyF28: "f28", |
| 504 |
KeyF29: "f29", |
| 505 |
KeyF30: "f30", |
| 506 |
KeyF31: "f31", |
| 507 |
KeyF32: "f32", |
| 508 |
KeyF33: "f33", |
| 509 |
KeyF34: "f34", |
| 510 |
KeyF35: "f35", |
| 511 |
KeyF36: "f36", |
| 512 |
KeyF37: "f37", |
| 513 |
KeyF38: "f38", |
| 514 |
KeyF39: "f39", |
| 515 |
KeyF40: "f40", |
| 516 |
KeyF41: "f41", |
| 517 |
KeyF42: "f42", |
| 518 |
KeyF43: "f43", |
| 519 |
KeyF44: "f44", |
| 520 |
KeyF45: "f45", |
| 521 |
KeyF46: "f46", |
| 522 |
KeyF47: "f47", |
| 523 |
KeyF48: "f48", |
| 524 |
KeyF49: "f49", |
| 525 |
KeyF50: "f50", |
| 526 |
KeyF51: "f51", |
| 527 |
KeyF52: "f52", |
| 528 |
KeyF53: "f53", |
| 529 |
KeyF54: "f54", |
| 530 |
KeyF55: "f55", |
| 531 |
KeyF56: "f56", |
| 532 |
KeyF57: "f57", |
| 533 |
KeyF58: "f58", |
| 534 |
KeyF59: "f59", |
| 535 |
KeyF60: "f60", |
| 536 |
KeyF61: "f61", |
| 537 |
KeyF62: "f62", |
| 538 |
KeyF63: "f63", |
| 539 |
|
| 540 |
|
| 541 |
KeyCapsLock: "capslock", |
| 542 |
KeyScrollLock: "scrolllock", |
| 543 |
KeyNumLock: "numlock", |
| 544 |
KeyPrintScreen: "printscreen", |
| 545 |
KeyPause: "pause", |
| 546 |
KeyMenu: "menu", |
| 547 |
KeyMediaPlay: "mediaplay", |
| 548 |
KeyMediaPause: "mediapause", |
| 549 |
KeyMediaPlayPause: "mediaplaypause", |
| 550 |
KeyMediaReverse: "mediareverse", |
| 551 |
KeyMediaStop: "mediastop", |
| 552 |
KeyMediaFastForward: "mediafastforward", |
| 553 |
KeyMediaRewind: "mediarewind", |
| 554 |
KeyMediaNext: "medianext", |
| 555 |
KeyMediaPrev: "mediaprev", |
| 556 |
KeyMediaRecord: "mediarecord", |
| 557 |
KeyLowerVol: "lowervol", |
| 558 |
KeyRaiseVol: "raisevol", |
| 559 |
KeyMute: "mute", |
| 560 |
KeyLeftShift: "leftshift", |
| 561 |
KeyLeftAlt: "leftalt", |
| 562 |
KeyLeftCtrl: "leftctrl", |
| 563 |
KeyLeftSuper: "leftsuper", |
| 564 |
KeyLeftHyper: "lefthyper", |
| 565 |
KeyLeftMeta: "leftmeta", |
| 566 |
KeyRightShift: "rightshift", |
| 567 |
KeyRightAlt: "rightalt", |
| 568 |
KeyRightCtrl: "rightctrl", |
| 569 |
KeyRightSuper: "rightsuper", |
| 570 |
KeyRightHyper: "righthyper", |
| 571 |
KeyRightMeta: "rightmeta", |
| 572 |
KeyIsoLevel3Shift: "isolevel3shift", |
| 573 |
KeyIsoLevel5Shift: "isolevel5shift", |
| 574 |
} |