Keyboard Commands

Is there a way to make a program launch a command that corresponds to a keyboard command?

For example, can I make a program launch the arrow key commands or the ctrl/alt keys? Maybe even mouse clicks?
Huh? What exactly do you mean by "Launch the arrow key commands"?? The arrows keys don't launch programs or anything...
I think he means he wants to receive keyboard/mouse events.

If you want to catch mouse clicks you can use WM_LBUTTONDOWN and WM_MBUTTONDOWN or WM_RBUTTONDOWN for the left mouse button, middle mouse button and right mouse buttons respectively.

You can use WM_KEYDOWN - then use a switch statement like this:
1
2
3
4
5
6
7
8
9
10
11
switch (message) /*this is the message you received after receiving a WM_KEYDOWN*/ {

case "VK_CONTROL": // (I'm not 100% if this is the actual keyname -- you can also use:)
//code
break;

case 'A': // This is not case sensitive.
// code
break;

}
Last edited on
Not exactly what I meant, but that info will help to. Thanks.

What I mean is instead of pressing the up arrow to do something, have the program send the same command to the computer that the up arrow would normally. Basically like I am pressing a button, but I'm really not, the program is doing it. Is that possible? I don't think it is, but someone told me they did it before, and I was curious as to how.

Edit: To put it in clearer language (I hope) I want to simulate keyboard and mouse events. These events have to be sent to a different window though.
Last edited on
closed account (z05DSL3A)
Go to MSDN Library and search for SendInput Function.

http://msdn.microsoft.com/en-us/library/default.aspx

edit:
If it is what you are looking for there are a few examples of using it here (but otherwise it is not very well documented):
http://www.codeguru.com/forum/showthread.php?t=377394
Last edited on
Oh, that it similar to what I am trying to do - have the program 'press' the key.

You can easily send mouse events. I wrote this for a program I am currently writing (with no current purpose); it clicks random places. I'm not sure if the mouse click works as I have not tested it, but it should do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void clickloop() { // This function generates random x, y coords for the mouse to move to and click on that co-ordinate.
int x, y;
    do {
        srand((unsigned)time(0)); 
        int random_integer; 
        int lowest=230, highest=730; 
        int range=(highest-lowest)+1; 
        x=lowest+int(range*rand()/(RAND_MAX + 1.0));			// Generate the x-coord...
        y=lowest+int(range*rand()/(RAND_MAX + 1.0));			// ... and the y-coord.
        SetCursorPos(x, y);                                                 // Move to the location demonstrated
        // Send a mouse click event. As this Window does not exist the active window will be affected instead. Hopefully.
        mouse_event(MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_LEFTUP, NULL, NULL, NULL, NULL);   
    } while (true);
}
closed account (z05DSL3A)
The 'mouse_event()' function has been superseded by 'SendInput()'

srand((unsigned)time(0)); should not be called more than once, so should be outside of your do...while loop.
SendInput()? I tried using that for text but couldn't figure it out.

Anyway this is not my thread. Apologies for hijacking it. Grey Wolf has given you the correct info.
Topic archived. No new replies allowed.