@@ -492,35 +492,7 @@ func readInputs(input io.Reader) ([]Msg, error) {
492492 return m , nil
493493 }
494494
495- // Is it a sequence, like an arrow key?
496- if k , ok := sequences [string (buf [:numBytes ])]; ok {
497- return []Msg {
498- KeyMsg (k ),
499- }, nil
500- }
501-
502- // Some of these need special handling.
503- hex := fmt .Sprintf ("%x" , buf [:numBytes ])
504- if k , ok := hexes [hex ]; ok {
505- return []Msg {
506- KeyMsg (k ),
507- }, nil
508- }
509-
510- // Is the alt key pressed? If so, the buffer will be prefixed with an
511- // escape.
512- if numBytes > 1 && buf [0 ] == 0x1b {
513- // Now remove the initial escape sequence and re-process to get the
514- // character being pressed in combination with alt.
515- c , _ := utf8 .DecodeRune (buf [1 :])
516- if c == utf8 .RuneError {
517- return nil , errors .New ("could not decode rune after removing initial escape" )
518- }
519- return []Msg {
520- KeyMsg (Key {Alt : true , Type : KeyRunes , Runes : []rune {c }}),
521- }, nil
522- }
523-
495+ var runeSets [][]rune
524496 var runes []rune
525497 b := buf [:numBytes ]
526498
@@ -532,38 +504,64 @@ func readInputs(input io.Reader) ([]Msg, error) {
532504 if r == utf8 .RuneError {
533505 return nil , errors .New ("could not decode rune" )
534506 }
507+
508+ if r == '\x1b' && len (runes ) > 1 {
509+ // a new key sequence has started
510+ runeSets = append (runeSets , runes )
511+ runes = []rune {}
512+ }
513+
535514 runes = append (runes , r )
536515 w = width
537516 }
517+ // add the final set of runes we decoded
518+ runeSets = append (runeSets , runes )
538519
539- if len (runes ) == 0 {
520+ if len (runeSets ) == 0 {
540521 return nil , errors .New ("received 0 runes from input" )
541- } else if len (runes ) > 1 {
542- // We received multiple runes, so we know this isn't a control
543- // character, sequence, and so on.
544- return []Msg {
545- KeyMsg (Key {Type : KeyRunes , Runes : runes }),
546- }, nil
547522 }
548523
549- // Is the first rune a control character?
550- r := KeyType (runes [0 ])
551- if numBytes == 1 && r <= keyUS || r == keyDEL {
552- return []Msg {
553- KeyMsg (Key {Type : r }),
554- }, nil
555- }
524+ var msgs []Msg
525+ for _ , runes := range runeSets {
526+ // Is it a sequence, like an arrow key?
527+ if k , ok := sequences [string (runes )]; ok {
528+ msgs = append (msgs , KeyMsg (k ))
529+ continue
530+ }
531+
532+ // Some of these need special handling.
533+ hex := fmt .Sprintf ("%x" , runes )
534+ if k , ok := hexes [hex ]; ok {
535+ msgs = append (msgs , KeyMsg (k ))
536+ continue
537+ }
538+
539+ // Is the alt key pressed? If so, the buffer will be prefixed with an
540+ // escape.
541+ if len (runes ) > 1 && runes [0 ] == 0x1b {
542+ msgs = append (msgs , KeyMsg (Key {Alt : true , Type : KeyRunes , Runes : runes [1 :]}))
543+ continue
544+ }
556545
557- // If it's a space, override the type with KeySpace (but still include the
558- // rune).
559- if runes [0 ] == ' ' {
560- return []Msg {
561- KeyMsg (Key {Type : KeySpace , Runes : runes }),
562- }, nil
546+ for _ , v := range runes {
547+ // Is the first rune a control character?
548+ r := KeyType (v )
549+ if r <= keyUS || r == keyDEL {
550+ msgs = append (msgs , KeyMsg (Key {Type : r }))
551+ continue
552+ }
553+
554+ // If it's a space, override the type with KeySpace (but still include
555+ // the rune).
556+ if r == ' ' {
557+ msgs = append (msgs , KeyMsg (Key {Type : KeySpace , Runes : []rune {v }}))
558+ continue
559+ }
560+
561+ // Welp, just regular, ol' runes.
562+ msgs = append (msgs , KeyMsg (Key {Type : KeyRunes , Runes : []rune {v }}))
563+ }
563564 }
564565
565- // Welp, it's just a regular, ol' single rune.
566- return []Msg {
567- KeyMsg (Key {Type : KeyRunes , Runes : runes }),
568- }, nil
566+ return msgs , nil
569567}
0 commit comments