micmonay / keybd_event

For simulate key press in Linux, Windows and Mac in golang
MIT License
380 stars 57 forks source link

Insert Linux delay into keybd_linux.go #25

Closed kmomberg closed 4 years ago

kmomberg commented 4 years ago

Hi Michaël.

Why the two seconds delay for Linux isn't inside initKeyBD() on keybd_linux.go? Given it's a hard requirement, I consider it a safer way to avoid issues when using this library.

And great job BTW ;-)

micmonay commented 4 years ago

Hi,

For minimal wait, you can create and use one instance of KeyBounding for all input. More information why the library need wait in this link : https://www.kernel.org/doc/html/latest/input/uinput.html#keyboard-events

Extract of link :

/*
 * On UI_DEV_CREATE the kernel will create the device node for this
 * device. We are inserting a pause here so that userspace has time
 * to detect, initialize the new device, and can start listening to
 * the event, otherwise it will not notice the event we are about
 * to send. This pause is only needed in our example code!
 */
sleep(1);

Best regard

kmomberg commented 4 years ago

Sorry but I didn't get it.

In your code you have this comment: `// For linux, it is very important to wait 2 seconds'.

I'm not questioning the delay, but thinking about the best place to put it. For example if someone forgot to use that delay, or originally developed something for a different platform (thus removed the delay) and then wanted to include Linux again, but forget to put it back.

Then, adding the delay inside that include file that's exclusive for Linux could be safer in my opinion. You can even use an env var with a default of 2sec, so if someone simply didn't specified it, will get the 2 secs..

Just a suggestion.

micmonay commented 4 years ago

Okay, I get it! If I add the delay in NewKeyBonding(), there would be a risk that someone would try to put NewKeyBonding() in a thread to avoid the delay on Linux or not understand the behaviour. If the user of the library chooses and introduces the delay himself, that guarantees that the person adding it will do it when and where he wants, being aware of its necessity.

In your example, the library user might not understand why his application reacts so slowly on Linux. Let's imagine he uses 30 libraries, and with each one using 5 libraries... :).

The 2 seconds or even 1 second is not negligible as a delay in an application. That's why I used this way.

To avoid this problem, it necessary to write a code that is thread-safety. With Coroutines and Channels.

kmomberg commented 4 years ago

ok. makes sense. Thanks dude 👍