Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C++
#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");

    }
Posted
Comments
[no name] 1-Sep-14 18:42pm    
What does "nothing really happened" mean? What did happen? How was the result different than you expected? Your code runs just like it should on my VS2010 Win7 system.
Rebecca1995 1-Sep-14 18:51pm    
@Wes Aday:- Well, before running the code, i pressed the Alt+Tab key and saw the output. Now, after entering x = 2, i should be able to see the same thing that i did manually by pressing the Alt+Tab key. But nothing showed up.It seemed as if the if statement didnot even execute.
[no name] 1-Sep-14 18:54pm    
Did you press the "2" key or did you press 2->Enter? Did you debug your code to find out exactly what it was doing?
Rebecca1995 1-Sep-14 19:05pm    
@wes aday:- yeah i finally fixed the problem. But what should i do to simulate the arrow key down/up event?
[no name] 1-Sep-14 19:10pm    
The key codes are defined in WinUser.h, VK_UP and VK_DOWN is what you are looking for. And read more about this at http://msdn.microsoft.com/en-us/library/windows/desktop/gg153546(v=vs.85).aspx

1 solution

First of all, this is not DirectInput; this is Windows API.
Secondly, keybd_event has been superseded by SendInput: 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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900