MediatedCommunications / WindowsInput

Capture and Simulate Keyboard and Mouse Input
MIT License
111 stars 17 forks source link

Is Key pressed #1

Closed SolemnDucc closed 4 years ago

SolemnDucc commented 4 years ago

Hello, first of all i want to thank you. Really nice project. I tried several other things before but none let me actually simulate key presses that get accepted by the game that i want to write a bot for. So my bot can move my character thanks to your await WindowsInput.Simulate.Events() .Hold(KeyCode.W).Wait(100) .Release(KeyCode.W) .Invoke(); Methode. Now, the bot shall stop moving my character as soon as i press "W". So i need something like: if(!WindowsInput.IsKeyPressed(KeyCode.W) { await WindowsInput.Simulate.Events() .Hold(KeyCode.W).Wait(100) .Release(KeyCode.W) .Invoke(); } Is there a Methode like this or how can i archieve this? By the way my program is a console application. So i don't know if or how i can use your KeyEvent Methode you showed in your Capture Keys example.

TonyValenti commented 4 years ago

Hi @SolemnDucc - This is great! I’m really glad to hear how your bot is going!

I wanted to let you know I have your message and I’ll post a code sample later today (and an updated nugget).

What game?

TonyValenti commented 4 years ago

Hi @SolemnDucc - I wanted to let you know I just published an updated nuget package (6.1.1) that contains a lot of enhancements, fixes, and tweaks.

There are a couple of different ways to accomplish what you want to do and it will depend on how advanced you want to get with you code.

First, at a high level, this is what you'll want to do: When you simulate keystrokes, run it in an alternate thread. Pass a cancelation token into the Simulate method can cancel that token when you want simulation to stop.

In order to detect when the W key is pressed, you can do the following:

  1. Use the "Capture" class to listen for keys and then wait for "W" to be pressed.
  2. Use the WindowsInput.Native.KeyboardState.Current() to get the current Keyboard state of all keys. Then check the status of the W key.
  3. Use WindowsInput.Native.KeyboardState's GetAsyncKeyState / GetKeyState to check the status of the W key.

I hope this helps!