Krombik / keysender

Node.js Desktop Automation for Windows.
MIT License
75 stars 6 forks source link

Any way to wait for a key #9

Closed ch3rn1k closed 1 year ago

ch3rn1k commented 1 year ago

Hey ya, any way to add a function like to wait for a key press? Smth like waitKey, waitClick, waitMove, waitScroll

Dat will be awesome <3

Krombik commented 1 year ago

if you mean pressing a key outside of keysender - it's not possible, winapi doesn't have methods for that

ch3rn1k commented 1 year ago

if you mean pressing a key outside of keysender - it's not possible, winapi doesn't have methods for that

I mean to make a wait function, you already have GlobalHotkey that detects key press

ch3rn1k commented 1 year ago

Like await obj.keyboard.waitKey("a");

Krombik commented 1 year ago

in 2.1.0 the LowLevelHook class was added with which this logic can be implemented

ch3rn1k commented 1 year ago

in 2.1.0 the LowLevelHook class was added with which this logic can be implemented

Nice one, thanks, but is there a way to use async/await with LowLevelHook.on()? Like await LowLevelHook.on('keyboard', 'a', true)

Krombik commented 1 year ago

no, it works like addEventListener in browser

ch3rn1k commented 1 year ago

well, still awesome lib, hope you will implement async/await later

Krombik commented 1 year ago

but you can write something like whis

const waitForKey = (device, button, state) =>
  new Promise((resolve) => {
    const unlisten = LowLevelHook.on(device, button, state, () => {
      unlisten();

      resolve();
    });
  });
ch3rn1k commented 1 year ago

but you can write something like whis

const waitForKey = (device, button, state) =>
  new Promise((resolve) => {
    const unlisten = LowLevelHook.on(device, button, state, () => {
      unlisten();

      resolve();
    });
  });

Looks like after unlistening next time .on ain't works

Krombik commented 1 year ago

thats what unlisten do

ch3rn1k commented 1 year ago

thats what unlisten do

I mean another call in new function with waitForKey ain't works. I can only 1 single time call waitForKey in a code. Until i reload my script .on will not call again. It's not reusable :(

ch3rn1k commented 1 year ago

If we talking about addEventListener it can be added and removed. In another function I can call addEventListener again and it will work

ch3rn1k commented 1 year ago

After calling LowLevelHook.deleteAll(); it can be called again

Krombik commented 1 year ago

should be fixed in 2.1.1

ch3rn1k commented 1 year ago

should be fixed in 2.1.1

Thanks

ch3rn1k commented 1 year ago

Is there a way to listen keys combination? ["ctrl", "e"]

Krombik commented 1 year ago

no, but you can do it by yourself