/keypresses

This is a lib that provide you checking if any key is pressed

Primary LanguageGo

keypresses

This lib provides you checking if any key is pressed, fully built on win32api

Installation

Install this package with command go get -u github.com/Numenorean/keypresses

Finding virtualKeyCodes

Just go to this page https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

Usage

Example of getting key state (even if current window is inactive)

package main

import (
	"github.com/Numenorean/keypresses"
	"time"
	"fmt"
)

func main() {
  for true {
    // 0x50 is virtualKeyCode, char "P" in human format
    fmt.Println(keypresses.IsKeyPressed(0x50))
    // Sleeping to prevent 100% CPU usage
    time.Sleep(1 * time.Microsecond)
  }
}

Example of getting key state only if current window is active

package main

import (
	"github.com/Numenorean/keypresses"
	"time"
	"fmt"
)

func main() {
  for true {
    // "false" argument means that to get key state, window should be active
    // "true" argument means that to get key state, window might not be active. The same as an IsKeyPressed function
    fmt.Println(keypresses.IsKeyPressedGlobal(0x50, false))
    // Sleeping to prevent 100% CPU usage
    time.Sleep(1 * time.Microsecond)
  }
}