eiannone / keyboard

Simple library to listen for keystrokes from the keyboard

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrectly read keypress in Linux

vlamai opened this issue · comments

package main

import (
	"fmt"
	"github.com/eiannone/keyboard"
)

func main() {	
	err := keyboard.Open()
	if err != nil {
		panic(err)
	}
	defer keyboard.Close()

	fmt.Println("Press ESC to quit")
	for {
		char, key, err := keyboard.GetKey()
		if (err != nil) {
			panic(err)
		} 
		fmt.Printf("char: %q\r\n", char)
		fmt.Printf("key: %v\r\n", key)
	}	
}

go run main.go
Press ESC to quit
char: 'a'
key: 0
char: '\x00' <- i pressed left arrow
key: 27
char: '\x00' <- i pressed right arrow
key: 27
char: '\x00' <- i pressed up arrow
key: 27
char: '\x00' <- i pressed down arrow
key: 27
char: '\x00' <- i pressed page up
key: 27
char: '\x00' <- i pressed page down
key: 27
char: '\x00'
key: 13
char: '\x00'
key: 32

printenv TERM
xterm-256color

the output if I run your code

Press ESC to quit
char: 'a'
key: 0
char: '\x00'
key: 65515
char: '\x00'
key: 65514
char: '\x00'
key: 65517
char: '\x00'
key: 65516
char: '\x00'
key: 65519
char: '\x00'
key: 65518
$ echo $TERM
xterm-256color

Hi @kzoper , I tried your code on linux (Ubuntu) with latest version of library, and it seems to be working. I couldn't reproduce your error. Could you please pull latest version and try again?

@eiannone I can reproduce this when I run it under tmux or screen. Outside of tmux/screen it works as expected.