Skip to content

Latest commit

 

History

History
49 lines (42 loc) · 1.18 KB

README.md

File metadata and controls

49 lines (42 loc) · 1.18 KB

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