1

hey i using inputSimulator and want to know how to simulate a Hold keydown for 10 seconds.if you know other instead of inputSimulator, then iam also fine with that. i cannot find any way in C# to do this. i tried it with a timer between or a stopwatch. but this seems not to work. it just press the Key, but not Holding it. also Thread.Sleep is not a good idea as the UI freezes. I need that async.

i cannot understand why this is not easy possible, because it seems to be a simple function.

Hope anyone have a other idea, that i can try :)

sim.Keyboard
// Simulate each key stroke
.KeyDown(VirtualKeyCode.VK_R)

/// Hold key down for 10 seconds!
/// and Release after that time.

.KeyUp(VirtualKeyCode.VK_R);
madreflection
  • 4,744
  • 3
  • 19
  • 29
  • Have you tried Task.Delay? See [Here](https://stackoverflow.com/questions/22158278/wait-some-seconds-without-blocking-ui-execution) – NullIfEmpty Apr 05 '22 at 18:23
  • If you've solved the problem, post an answer. Don't edit the question with the solution. I've rolled back your "SOLVED" edit because that's not how this community presents solutions. – madreflection Apr 05 '22 at 18:23
  • Again, putting "SOLVED" in the title is not appropriate. The fact that there's an answer (and eventually, you've *accepted* it, even if it's your own) is sufficient. The site will surface that information in search results and anywhere else it's needed. Please take some time to familiarize yourself with Stack Overflow's norms, as they differ from other sites. – madreflection Apr 05 '22 at 18:29

1 Answers1

0

My Problem is solved by this Code

WORKING CODE

var sim = new InputSimulator();
for (int i = 0; i < 500; i++)
{
    sim.Keyboard.KeyDown(VirtualKeyCode.VK_R);
    Thread.Sleep(10);
}

sim.Keyboard.KeyUp(VirtualKeyCode.VK_R);
  • 1
    this does not solve the actual problem of KeyDown not acting like holding the button, but just like a single press (KeyPress). And in this case you press r 500 times – Taki7o7 Nov 06 '22 at 22:39