I need a way to simulate keyboard keys if a certain condition is met, I also need to know if it's the simulated key that's currently pressed or the real key. This needs to work outside the main application.
This is how I would need it to work:
Dim UserDefinedKey As Keys = Keys.H
Do
If GetAsyncKeyState(UserDefinedKey) Then
Thread.Sleep(30)
'release the set key
Thread.Sleep(30)
'press/hold the set key once, continue the loop if the real key is still been held.
End If
Loop While GetAsyncKeyState(UserDefinedKey) '/ loop while real key is being held
'Real key is no longer held, release the simulated key press.
Any help would be greatly appreciated. (This code is to automate certain things inside of a game which is why it needs to work outside the main application)
I have certain things in place to allow the user to set their own key, this was just a small example of what I need, it's just the keyboard simulating part i'm stuck with and determining if the real key is still pressed or not.
So sorry for the long wait... Simulating keyboard input via Window Messages turned out to be far much more complicated compared to simulating mouse input in the same way.
Anyway, I am finally done with it so here's a complete InputHelper
class for simulating both mouse and keyboard input virtually via the mouse and keyboard input stream or via Window Messages.
Download at GitHub: https://github.com/Visual-Vincent/InputHelper/releases
(source code is too long to be pasted in the answer)
Dim UserDefinedKey As Keys = Keys.H
'Using a regular While-loop is better since you won't need your If-statement then.
While InputHelper.Keyboard.IsKeyDown(UserDefinedKey)
Dim ActiveWindow As IntPtr = InputHelper.WindowMessages.GetActiveWindow()
Thread.Sleep(30)
InputHelper.WindowMessages.SendKey(ActiveWindow, UserDefinedKey, False) 'False = Key up.
Thread.Sleep(30)
InputHelper.WindowMessages.SendKey(ActiveWindow, UserDefinedKey, True) 'True = Key down.
End While
A little information about InputHelper
's sub-classes:
InputHelper.Keyboard
GetAsyncKeyState()
).InputHelper.Mouse
InputHelper.WindowMessages
GetAsyncKeyState()
).Hope this helps!