Skip to content

Commit

Permalink
Merge pull request #61 from codelaboratoryltd/fix/control-keys
Browse files Browse the repository at this point in the history
Fix/control keys
  • Loading branch information
andydotxyz authored Jan 16, 2024
2 parents cb5b5d9 + b9832fe commit 637e8f2
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
16 changes: 14 additions & 2 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,20 @@ func (t *Terminal) TypedShortcut(s fyne.Shortcut) {
if ds, ok := s.(*desktop.CustomShortcut); ok {
t.ShortcutHandler.TypedShortcut(s) // it's not clear how we can check if this consumed the event

off := ds.KeyName[0] - 'A' + 1
_, _ = t.in.Write([]byte{off})
// handle CTRL+A to CTRL+_ and everything inbetween
if ds.Modifier == fyne.KeyModifierControl {
char := ds.KeyName[0]
off := char - 'A' + 1
switch {
case ds.Key() == fyne.KeySpace:
fallthrough
case ds.Key() == "@":
off = 0
fallthrough
case char >= 'A' && char <= '_':
_, _ = t.in.Write([]byte{off})
}
}
return
}

Expand Down
60 changes: 60 additions & 0 deletions input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/driver/desktop"
)

// NopCloser returns a WriteCloser with a no-op Close method wrapping
Expand Down Expand Up @@ -127,3 +128,62 @@ func TestTerminal_TypedKey_LineMode(t *testing.T) {
})
}
}

func TestTerminal_TypedShortcut(t *testing.T) {
tests := map[string]struct {
shortcut fyne.Shortcut
want []byte
}{
"LeftOption+U": {
shortcut: &desktop.CustomShortcut{
Modifier: fyne.KeyModifierAlt,
KeyName: fyne.KeyU},
want: []byte{},
},
"Control+@": {
shortcut: &desktop.CustomShortcut{
Modifier: fyne.KeyModifierControl,
KeyName: "@"},
want: []byte{0},
},
"Control+Space": {
shortcut: &desktop.CustomShortcut{
Modifier: fyne.KeyModifierControl,
KeyName: fyne.KeySpace},
want: []byte{0},
},
"Control+C": {
shortcut: &desktop.CustomShortcut{
Modifier: fyne.KeyModifierControl,
KeyName: fyne.KeyC},
want: []byte{3},
},
"Control+_": {
shortcut: &desktop.CustomShortcut{
Modifier: fyne.KeyModifierControl,
KeyName: "_"},
want: []byte{31},
},
"Control+X": {
shortcut: &desktop.CustomShortcut{
Modifier: fyne.KeyModifierControl,
KeyName: fyne.KeyX},
want: []byte{24},
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
// Creating a mock terminal
inBuffer := bytes.NewBuffer([]byte{})
term := &Terminal{in: NopCloser(inBuffer)}

term.TypedShortcut(tt.shortcut)

got := inBuffer.Bytes()
if !bytes.Equal(got, tt.want) {
t.Errorf("TypedShortcut() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 637e8f2

Please sign in to comment.