开发者

pressing a button for X seconds?

I've tried

keybd_event(keyByte, 0, KEYEVENTF_KEYDOWN, 0);

Thread.Sleep(3000);

keybd_event(keyByte, 0, KEYEVENTF_KEYUP, 0);

But it only presses it once and pauses, I want it to simulate holding a button. Example if I'm simulating player two in a car game I've made and it wants to turn the car while moving forward. The curve is 90 so i then want to hold it for 3 seconds. How to i do this?

EDIT: tried

                DateTime start = DateTime.Now;
                TimeSpan timespan = DateTime.Now.Subtract(DateTime.Now);

                if (timespan.TotalMilliseconds < duration)
                {
                    ti开发者_如何学Pythonmespan = DateTime.Now.Subtract(start);

                    keybd_event(keyByte, 0, KEYEVENTF_KEYDOWN, 0);
                }

                keybd_event(keyByte, 0, KEYEVENTF_KEYUP, 0);

but without result


You should set a timer for 3 seconds and when it fires, you should release the button.


You could call it every update, this would simulate each update checking for input and finding it. It should also cause minimal change if you want to give the option between manual control and computer control.

But as far as I'm aware there is no option to say "generate this input until I say otherwise".

Also as a note, if you are trying to move a car on a track (say in a game) there is math for that, you generally simulate the user's controls using that instead of just having the computer press the button.


Did you try putting

keybd_event(keyByte, 0, KEYEVENTF_KEYDOWN, 0);

in a loop for 3 seconds?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜