Skip to content

CTNOriginals/getkeystate

Repository files navigation

GetKeyState

Go Reference

Get the state of any keyboard key.

Requirements

  • 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_ENABLED to 1.

Install package

go get github.com/CTNOriginals/getkeystate

Usage

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).

Example

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))
}

Here are the different state conditions that this bit of code may produce:

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

About

Get the state of any keyboard key.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published