Get the state of any keyboard key.
- To be able to compile C code to go: MinGW-w64
- For go to allow C code compilation, you may need to set the environment variable
CGO_ENABLEDto1.
go get github.com/CTNOriginals/getkeystate
Import this package and call the GetKeyState function.
You may pass in any key as it's respective Vertual Key Code.
For example, the CapsLock key can be passed in as VK_CAPITAL, 0x14 or 20.
The return value of this function is a 2 bit int that represents the state of the key:
The left most bit represents wether it is currently held down (active).
The right most bit represents wether the key is currently toggled (works for any key, but mostly useful for keys like CapsLock).
CapsLock state check
import gks "github.com/CTNOriginals/getkeystate"
func main() {
var capslock = gks.GetKeyState(gks.VK_CAPITAL)
println("Value: ", capslock)
println("Active: ", gks.IsActive(capslock))
println("Toggled: ", gks.IsToggled(capslock))
}The following test case assumes that capslock starts as not toggled
| Condition | Value | Active | Toggled |
|---|---|---|---|
| CapsLock is not held down | 0 (00) |
false |
false |
| CapsLock is held down | 3 (11) |
true |
true |
| CapsLock is toggled but is not held down | 1 (01) |
false |
true |
| CapsLock is toggled and is held down | 2 (10) |
true |
false |