micmonay/keybd_event

Pressing most keys on Windows doesn't work as expected

Closed this issue · 1 comments

The problem

What I mean by that that most of the keys (A-Z, space) on Windows are instantly released after executing Press method. Strangely, keys like shift stay pressed. Here is my code that I tested that on:

package main

import (
	"fmt"
	"runtime"
	"time"

	"github.com/micmonay/keybd_event"
)

func main() {
	kb, err := keybd_event.NewKeyBonding()
	if err != nil {
		panic(err)
	}

	// For linux, it is very important to wait 2 seconds
	if runtime.GOOS == "linux" {
		time.Sleep(2 * time.Second)
	}

	// Select keys to be pressed
	kb.SetKeys(keybd_event.VK_A)

	// Set shift to be pressed
	kb.HasSHIFT(true)

	// Give user a bit of time to switch windows
	fmt.Println("Switch windows now!")
	time.Sleep(5 * time.Second)

	// Press keys for 5 seconds
	fmt.Println("Pressing keys!")
	kb.Press()
	time.Sleep(5 * time.Second)
	kb.Release()
}

What do I expect?

Keys specified in SetKeys method stay pressed after invoking Press method until Release method is executed.

The keyboard input I would like to get:

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

What actually happens?

The keys specified in SetKeys method are instantly released after executing Press method. I can manually press the key before the Release function is called and it will be typed out as if that key wasn't pressed.

The keyboard input I actuallly get:

A

Strangely, the shift key stays pressed until the Release method is called and that's what it should be for all the other keys.

Now that I look at it in C++. It's how it works here as well, so I guess it's just how simulating keypresses on Windows is suppose to work. Disappointing 😥