Skip to content

Commit

Permalink
fixed stop routine with syscall on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Jun 5, 2022
1 parent 3a7bcb1 commit e3c243e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
7 changes: 4 additions & 3 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keyboard
import (
"errors"
"fmt"
"io"
"os"
"unicode/utf8"

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

func getKeyPress(input io.Reader) (keys.Key, error) {
func getKeyPress() (keys.Key, error) {
var buf [256]byte

fmt.Println("a")
// Read
numBytes, err := input.Read(buf[:])
numBytes, err := inputTTY.Read(buf[:])
if err != nil {
if errors.Is(err, os.ErrClosed) {
return keys.Key{}, nil
}
return keys.Key{}, fmt.Errorf("could not read stdin: %w", err)
}
fmt.Println("b")

// Check if it's a sequence
if k, ok := sequences[string(buf[:numBytes])]; ok {
Expand Down
8 changes: 5 additions & 3 deletions keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keyboard
import (
"fmt"
"os"
"reflect"

"github.com/containerd/console"

Expand Down Expand Up @@ -79,8 +80,8 @@ func Listen(onKeyPress func(key keys.Key) (stop bool, err error)) error {
case keyInfo := <-mockChannel:
stopRoutine, _ = onKeyPress(keyInfo)
if stopRoutine {
closeInput()
inputTTY.Close()
onKeyPress(keys.Key{Code: keys.Null})
}
}
}
Expand All @@ -92,12 +93,13 @@ func Listen(onKeyPress func(key keys.Key) (stop bool, err error)) error {
}

for !stopRoutine {
key, err := getKeyPress(inputTTY)
key, err := getKeyPress()
if err != nil {
return err
}

if key.Code == keys.Null {
// check if returned key is empty
if reflect.DeepEqual(key, keys.Key{}) {
return nil
}

Expand Down
8 changes: 8 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
// +build darwin dragonfly freebsd linux netbsd openbsd solaris

package keyboard

func closeInput() {

}
7 changes: 7 additions & 0 deletions utils_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package keyboard

import "syscall"

func closeInput() {
syscall.CancelIoEx(syscall.Handle(inputTTY.Fd()), nil)
}

0 comments on commit e3c243e

Please sign in to comment.