eiannone / keyboard Goto Github PK
View Code? Open in Web Editor NEWSimple library to listen for keystrokes from the keyboard
License: MIT License
Simple library to listen for keystrokes from the keyboard
License: MIT License
Hello! ๐
I am using your library for a small project of mine. The problem is that, everytime I flood the stdin with random keys, the program panics.
The output:
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
github.com/eiannone/keyboard.Close()
.../src/github.com/eiannone/keyboard/keyboard_windows.go:293 +0xcc
github.com/eiannone/keyboard.GetSingleKey(0x0, 0x4, 0x8)
.../src/github.com/eiannone/keyboard/keyboard_windows.go:318 +0x8b
main.main()
.../src/github.com/Nhanderu/dois-mil/main.go:33 +0x104
goroutine 20 [chan send]:
github.com/eiannone/keyboard.inputEventsProducer()
.../src/github.com/eiannone/keyboard/keyboard_windows.go:253 +0x316
created by github.com/eiannone/keyboard.Open
.../src/github.com/eiannone/keyboard/keyboard_windows.go:279 +0x22c
It is not a huge problem for me, but it is always worth pointing it out. ๐
I am very pleased with the overall quality and I am very happy to use it. So thank you. The matter is certain terminals like alacritty, kitty do not respond as they should to arrow keys, in short they terminate, which makes me wonder if it's their internal keybinding or something else. No idea how to resolve that. Could you elaborate why? What's your mind on that? And Ctrl+M is treated as Enter. This also bugs me, and this happens everywhere. I am not doing anything weird.
Thanks for the keyboard package. I am using it in one of my projects, and I recently learnt about go's builtin race detector. I tried using it on my project expecting races in my own code, however it seems I hit a couple of races in keyboard library code. I thought to report this here. I'm not sure if this is serious or not.
Here's the race on keyboard Open
==================
WARNING: DATA RACE
Write at 0x00c42014e000 by goroutine 9:
internal/race.WriteRange()
/usr/local/Cellar/go/1.10.3/libexec/src/internal/race/race.go:49 +0x42
syscall.Read()
/usr/local/Cellar/go/1.10.3/libexec/src/syscall/syscall_unix.go:165 +0x9a
github.com/eiannone/keyboard.initConsole.func1()
/Users/someone/go/src/github.com/eiannone/keyboard/keyboard.go:188 +0xc5
Previous read at 0x00c42014e000 by goroutine 10:
runtime.slicecopy()
/usr/local/Cellar/go/1.10.3/libexec/src/runtime/slice.go:192 +0x0
github.com/eiannone/keyboard.inputEventsProducer()
/Users/someone/go/src/github.com/eiannone/keyboard/keyboard.go:123 +0x2c4
Goroutine 9 (running) created at:
github.com/eiannone/keyboard.initConsole()
/Users/someone/go/src/github.com/eiannone/keyboard/keyboard.go:182 +0x44d
github.com/eiannone/keyboard.Open()
/Users/someone/go/src/github.com/eiannone/keyboard/keyboard_common.go:101 +0x4e
main.main()
/Users/someone/go/src/github.com/somehash/someminer/main.go:65 +0x3bc
Goroutine 10 (running) created at:
github.com/eiannone/keyboard.initConsole()
/Users/someone/go/src/github.com/eiannone/keyboard/keyboard.go:205 +0x465
github.com/eiannone/keyboard.Open()
/Users/someone/go/src/github.com/eiannone/keyboard/keyboard_common.go:101 +0x4e
main.main()
/Users/someone/go/src/github.com/somehash/someminer/main.go:65 +0x3bc
==================
and another one on close()
==================
WARNING: DATA RACE
Read at 0x0000015c74d3 by main goroutine:
github.com/eiannone/keyboard.Close()
/Users/someone/go/src/github.com/eiannone/keyboard/keyboard_common.go:113 +0x59
main.main()
/Users/someone/go/src/github.com/somehash/someminer/main.go:187 +0x8fe
Previous write at 0x0000015c74d3 by goroutine 12:
github.com/eiannone/keyboard.GetKey()
/Users/someone/go/src/github.com/eiannone/keyboard/keyboard_common.go:124 +0x8e
main.main.func1.1()
/Users/someone/go/src/github.com/somehash/someminer/main.go:83 +0x33
Goroutine 12 (running) created at:
main.main.func1()
/Users/someone/go/src/github.com/somehash/someminer/main.go:80 +0xe7
==================
==================
WARNING: DATA RACE
Write at 0x0000015c74d2 by main goroutine:
github.com/eiannone/keyboard.Close()
/Users/someone/go/src/github.com/eiannone/keyboard/keyboard_common.go:117 +0x79
main.main()
/Users/someone/go/src/github.com/somehash/someminer/main.go:187 +0x8fe
Previous read at 0x0000015c74d2 by goroutine 12:
github.com/eiannone/keyboard.GetKey()
/Users/someone/go/src/github.com/eiannone/keyboard/keyboard_common.go:121 +0x6f
main.main.func1.1()
/Users/someone/go/src/github.com/somehash/someminer/main.go:83 +0x33
Goroutine 12 (running) created at:
main.main.func1()
/Users/someone/go/src/github.com/somehash/someminer/main.go:80 +0xe7
==================
Thanks for the wonderful library.
How to listen to keystrokes when keys pressed outside of this application window running.
Means, I want to run this app as service in windows and record every keystroke on all applications uses on the entire session of usage.
Hi , testing this seems the keyboard events are sandboxed to the console only , how does one enable global keyboard events ?
Hello again!
Already updated your module to the latest version.
A problem with the keyboard.GetSingleKey() method:
for {
ch, key, err := keyboard.GetSingleKey()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%v %v\n", ch, key)
}
I consistently press the LEFT, UP, RIGHT, DOWN keys after starting the program:
// Press LEFT - ok
0 65515
// Press UP - ok
0 65517
// Press RIGHT - not ok
0 65514
0 65514 <-- Duplicate
// Press DOWN - not ok
0 65516
0 65516 <-- Duplicate
// Press LEFT - not ok
0 65515
0 65515 <-- Duplicate
0 65515 <-- Duplicate
// Etc.
0 65517 <-- Press UP
0 65517
0 65517
0 65517
0 65514 <-- Press RIGHT
0 65514
0 65514
0 65514
0 65514
0 65514
0 65516 <-- Press DOWN
0 65516
0 65516
0 65516
0 65516
0 65516
0 65516
0 65516
0 65516
0 65515 <-- Press LEFT
0 65515
0 65515
0 65515
0 65515
0 65515
0 65515
0 65515
0 65515
0 65515
0 65515
0 65515
panic: runtime error: slice bounds out of range
goroutine 15 [running]:
github.com/eiannone/keyboard.initConsole.func1()
/home/kirs/go/src/github.com/eiannone/keyboard/keyboard.go:194 +0x383
created by github.com/eiannone/keyboard.initConsole
/home/kirs/go/src/github.com/eiannone/keyboard/keyboard.go:205 +0x336
In the terminal "linux":
// Press Left
0 65515
panic: runtime error: slice bounds out of range
goroutine 7 [running]:
github.com/eiannone/keyboard.initConsole.func1()
/home/kirs/go/src/github.com/eiannone/keyboard/keyboard.go:194 +0x383
created by github.com/eiannone/keyboard.initConsole
/home/kirs/go/src/github.com/eiannone/keyboard/keyboard.go:205 +0x336
In the Windows terminal, there is no such problem.
While pressing the "KeySpace" the "KeyEvent" produced must contain a rune value of "32" (ascii value for space) but it contains
"0" (ascii value for null).
Moreover,
"KeyBackSpace" must produce "\b"
"KeyEnter" must produce "\n"
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
(Via Google translate)
Hello.
I'm trying to use your project. Faced a problem.
Incorrectly processed keys up
down
left
right
in Linux (Mint).
Example:
keyboard.Open()
defer keyboard.Close()
for {
char, key, _ := keyboard.GetKey()
fmt.Printf("<%d 0x%x \"%s\" key: %d\n", char, char, string(char), key)
}
// Press A - OK
65 0x41 "A" key: 0
// Press ESC - OK
0 0x0 "" key: 27
// Press Left - ERROR
0 0x0 "" key: 27
// Press Left - ERROR
91 0x5b "[" key: 0
// Press Left - ERROR
68 0x44 "D" key: 0
// Press A
0 0x0 "" key: 27
it would be very useful if I could use this in a switch statement, an although internally a channel is used, this is not exposed to the outside.
In my code, I'm continuously calling keyboard.GetKey() in a for loop, taking the resulting character and sending it down a golang channel. It seemed to work very well, however, I accidentally pasted like 10 characters. And the resulting behavior was strange. GetKey() emitted only the first character in the paste string then waited. If I pressed an 11th character on the keyboard, it would emit character number 2 of the originally pasted string. If I clicked 12th character, it would emit the 3rd and so on.
So effectively it has buffered the whole thing and is emitting a character only upon every key press. I would not have expected that. The proper behavior IMO, was for GetKey() to continuously emit keys rapidly until it's out of keys.
Thanks again for the good library
GetSingleKey is not working properly in this new version.
It is not returning instantly.
Let me explain the whole scenario.....
suppose i press 'a'...
it will not detect nor display any result.
then suppose i press 'e' (or any random alphabet or key)
this time it displays the value of the previous action (i.e. value of 'a')
i hope u got it...
please fix this issue
following version works fine
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807
but latest one is having issue
Hi,
on a german keyboard the {
(Alt Gr
+ 7
) and the [
(Alt Gr
+ 8
) does not work.
Eventuell sind auch weitere keys davon betroffen: aws/session-manager-plugin#13
Would be nice if you could fix this.
when cross-compiling for freebsd 386, there are some error:
../vendor/github.com/eiannone/keyboard/keyboard.go:177:16: constant 2150396948 overflows int
../vendor/github.com/eiannone/keyboard/keyboard.go:211:10: constant 2150396948 overflows int
My os is ubuntu 18.04, kernel version is 4.15.0-34-generic, amd64 arch
go version go1.10.1 linux/amd64
Hi,
Thanks for this awesome library!
I started to create a little CLI with it, and it works perfectly.
Then I added unit tests. I'm using GetKeys
but that's a one direction channel, so I can't simulate adding keys there.
Then I found github.com/micmonay/keybd_event, and in some time it was working on my laptop. And then I configured it to run on github actions, and it seems to fail: actions/runner#241
So my question is: would be there an option to send simulated keys to the package in testing? I know it's against clean API, but I can't figure out another way to test it on github actions like this.
Thanks again
src/github.com/eiannone/keyboard/keyboard.go:161:17: undefined: unix.TCGETS
src/github.com/eiannone/keyboard/keyboard.go:177:17: undefined: unix.TCSETS
src/github.com/eiannone/keyboard/keyboard.go:211:11: undefined: unix.TCSETS
I can't seems to find any reference to these elsewhere in the package, so it seems like something is missing from the package.
I am getting following error when I tried option+right (or left) arrow keys in mac keyboard
tst.go:20 +0x236
exit status 2
Space on macOS is not caught by the library
code:
err = kb.Open()
if err != nil {
panic(err)
}
defer kb.Close()
keyboard := make(chan string)
go func(keyboard chan string) {
for {
char, _, err := kb.GetSingleKey()
if err != nil {
panic(err)
}
fmt.Printf("You pressed: %q\r\n", char)
keyboard <- string(char)
}
}(keyboard)
You pressed: '1'
You pressed: '2'
You pressed: '3'
panic: runtime error: slice bounds out of range
goroutine 33 [running]:
github.com/eiannone/keyboard.initConsole.func1()
/Users/kuinda01/Documents/go/src/github.com/eiannone/keyboard/keyboard.go:193 +0x2af
created by github.com/eiannone/keyboard.initConsole
/Users/kuinda01/Documents/go/src/github.com/eiannone/keyboard/keyboard.go:182 +0x31a
exit status 2
The following program, which simply calls Open() and Close(), freezes on Windows.
package main
import (
"fmt"
"time"
"github.com/eiannone/keyboard"
)
func main() {
fmt.Println("Open begin")
if err := keyboard.Open(); err != nil {
panic(err)
}
fmt.Println("Open end")
time.Sleep(time.Second)
fmt.Println("Close begin")
if err := keyboard.Close(); err != nil {
panic(err)
}
fmt.Println("Close end")
}
output:
Open begin
Open end
Close begin
If I input any key, "Close end" is shown and the program exits.
This means that keyboard.Close() freezes.
Can you allow inserting keystrokes (so as to release a GetKey() call)?
The scenario is like this: The main routine issues a GetKey() call and is waiting for input. A goroutine meanwhile is counting on a timer and wants to end the wait when the timout occurs. At this point, how do I 'cancel' the GetKey() call? The solution I found (and it works fine) is to have a PushKey(ev KeyEvent) method added to keyboard_common.go that only does inputComm <- ev. This will push a default keystroke in and completes the GetKey() which can then return.
Did you leave out this functionality because of the security implications? If so, what is the alternative solution to cancel a GetKey() blocking call?
Generally most of the application wants to get continues keystrokes.
For example imagine a game played through keyboard we have to listen for keystrokes until the game ends and also I am using this library to create a typewriting application which have to listen for keys until the application is quite.
Currently this library only supports getting single keystroke and n number of keystrokes. So I am recommending to add a function that gives this feature.
If there is a function for getting single keystroke (GetSingleKey()) then why not for getting continuous keystroke.
Hope you understand ๐.
Hey,
I'm playing with this project, and I found that some keys cause the same error: unrecognized escape sequence
.
I'm using the latest version of the project.
My terminal is termite (TERM=xterm-termite
), on archlinux up-to-date.
Here is a (not exhaustive) list of keys that cause this:
Also some keys don't seem to be detected, such as:
XF86AudioMute
, etc)Thanks a lot for your help!
Regards.
when ever i press any arrow key i am getting the error as panic: runtime error: index out of range [3] with length 3
goroutine 22 [running]:
github.com/eiannone/keyboard.extract_event(0xc0000a4080, 0x3, 0x80, 0x1, 0x6600000001, 0x0, 0x0)
/home/karthik/go/src/github.com/eiannone/keyboard/keyboard.go:68 +0x1ba
github.com/eiannone/keyboard.inputEventsProducer()
/home/karthik/go/src/github.com/eiannone/keyboard/keyboard.go:107 +0x7f
created by github.com/eiannone/keyboard.initConsole
/home/karthik/go/src/github.com/eiannone/keyboard/keyboard.go:197 +0x3de
Process finished with exit code 2
For me that would be a worthy addition to new x/term
package in go. How do you think?
You pressed: '7'
You pressed: '5'
You pressed: '3'
You pressed: '1'
You pressed: '1'
1You pressed: '3'
You pressed: '3'
You pressed: '3'
You pressed: '7'
You pressed: '3'
panic: runtime error: slice bounds out of range [1:0]
goroutine 78 [running]:
github.com/eiannone/keyboard.inputEventsProducer()
C:/Users/zhu/go/pkg/mod/github.com/eiannone/[email protected]/keyboard.go:126 +0x340
created by github.com/eiannone/keyboard.initConsole
C:/Users/zhu/go/pkg/mod/github.com/eiannone/[email protected]/keyboard.go:208 +0x294
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.