Skip to content

Commit e3c243e

Browse files
committed
fixed stop routine with syscall on windows
1 parent 3a7bcb1 commit e3c243e

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

input.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package keyboard
33
import (
44
"errors"
55
"fmt"
6-
"io"
76
"os"
87
"unicode/utf8"
98

@@ -163,17 +162,19 @@ var hexCodes = map[string]keys.Key{
163162
"1b4f44": {Code: keys.Left, AltPressed: false},
164163
}
165164

166-
func getKeyPress(input io.Reader) (keys.Key, error) {
165+
func getKeyPress() (keys.Key, error) {
167166
var buf [256]byte
168167

168+
fmt.Println("a")
169169
// Read
170-
numBytes, err := input.Read(buf[:])
170+
numBytes, err := inputTTY.Read(buf[:])
171171
if err != nil {
172172
if errors.Is(err, os.ErrClosed) {
173173
return keys.Key{}, nil
174174
}
175175
return keys.Key{}, fmt.Errorf("could not read stdin: %w", err)
176176
}
177+
fmt.Println("b")
177178

178179
// Check if it's a sequence
179180
if k, ok := sequences[string(buf[:numBytes])]; ok {

keyboard.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package keyboard
33
import (
44
"fmt"
55
"os"
6+
"reflect"
67

78
"github.com/containerd/console"
89

@@ -79,8 +80,8 @@ func Listen(onKeyPress func(key keys.Key) (stop bool, err error)) error {
7980
case keyInfo := <-mockChannel:
8081
stopRoutine, _ = onKeyPress(keyInfo)
8182
if stopRoutine {
83+
closeInput()
8284
inputTTY.Close()
83-
onKeyPress(keys.Key{Code: keys.Null})
8485
}
8586
}
8687
}
@@ -92,12 +93,13 @@ func Listen(onKeyPress func(key keys.Key) (stop bool, err error)) error {
9293
}
9394

9495
for !stopRoutine {
95-
key, err := getKeyPress(inputTTY)
96+
key, err := getKeyPress()
9697
if err != nil {
9798
return err
9899
}
99100

100-
if key.Code == keys.Null {
101+
// check if returned key is empty
102+
if reflect.DeepEqual(key, keys.Key{}) {
101103
return nil
102104
}
103105

utils.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
2+
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
3+
4+
package keyboard
5+
6+
func closeInput() {
7+
8+
}

utils_windows.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package keyboard
2+
3+
import "syscall"
4+
5+
func closeInput() {
6+
syscall.CancelIoEx(syscall.Handle(inputTTY.Fd()), nil)
7+
}

0 commit comments

Comments
 (0)