如何使用Directinput模拟Keypress事件? [英] How Can I Simulate A Keypress Event Using Directinput?
问题描述
我想创建一个程序,要求用户输入一个数字。然后我会写一个if else语句,如if(num == 2){按右箭头键} else {按左箭头键}。
现在,我在网上做了一点研究,发现了很多有用的链接。其中一个是http://www.codeproject.com/Articles/6819/SendKeys-in-C。文章解释了使用keybd_event()函数进行模拟(ALT + TAB)按键。我试着去做,但什么也没发生。我最后会添加我的代码。
另一篇文章解释了sendinput()的使用,这看起来很复杂。现在,有人可以解释一下我怎么能以简单的方式做到这一点?我真的不想让事情复杂化,因为我没有太多的事情要做。我只需要知道如何使用用户输入的值来模拟箭头键(RIGHT / LEFT / UP / DOWN)。谢谢。
以下是我试图模拟ALT + TAB键的代码,如上面链接的文章所述,但没有发生任何事情。我想知道我犯了什么错误,因为我对这个keybd_event()函数非常愚蠢。
I want to create a program where the user is asked to enter a number. Then i shall write a if else statement such as if(num ==2) { press the right arrow key} else {press the left arrow key}.
Now, i did a little research online and found a lot of useful links. One of them was http://www.codeproject.com/Articles/6819/SendKeys-in-C. The article explained the use of keybd_event() function for simulating (ALT+TAB) key press. I tried to do it but nothing happened. I shall add my code in the end.
Another article explained the use of sendinput() which sort of seems complicated. Now, can someone please explain me how can i do it in a simple way? I really don't want to complicate things as i don't have much things to do with it. I just need to know how to simulate the arrow keys(RIGHT/LEFT/UP/DOWN) using values entered by the user. Thanks.
Following is the code that i tried to simulate the ALT+TAB key as explained in the above linked article but nothing really happened. I wonder where i made the mistake as i am really dumb with this keybd_event() function and stuff.
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
int x;
cout<<"Enter the value of x"<<endl;
cin>>x;
if (x==2)
{
keybd_event(VK_MENU, 0, 0, 0);//press down the ALT key
keybd_event(VK_TAB, 0, 0, 0); //press down the TAB key
}
system("pause");
}
推荐答案
首先,这不是DirectInput;这是Windows API。
其次,keybd_event
已被SendInput
取代: http://msdn.microsoft.com/en-us/ library / windows / desktop / ms646310%28v = vs.85%29.aspx [ ^ ]。
完美无缺,只需阅读仔细描述并为功能提供足够的输入。
-SA
First of all, this is not DirectInput; this is Windows API.
Secondly,keybd_event
has been superseded bySendInput
: http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx[^].
It works perfectly, just read the description carefully and provide sufficient input to the function.
—SA
这篇关于如何使用Directinput模拟Keypress事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!