Solved

keyboard and mouse events - specific code

Posted on 2007-11-25
6
290 Views
Last Modified: 2010-04-01
http://www.experts-exchange.com/Programming/Languages/CPP/Q_22981302.html#a20346828

I tried to read these

http://msdn2.microsoft.com/en-us/library/ms646260.aspx
http://msdn2.microsoft.com/en-us/library/ms646304.aspx


VOID keybd_event(      
    BYTE bVk,
    BYTE bScan,
    DWORD dwFlags,
    PTR dwExtraInfo
);




VOID mouse_event(      
    DWORD dwFlags,
    DWORD dx,
    DWORD dy,
    DWORD dwData,
    ULONG_PTR dwExtraInfo
);


0
Comment
Question by:Troudeloup
  • 3
  • 3
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 20346974
E.g.

// Simulating a Alt+Tab keystroke
keybd_event(VK_MENU,0xb8,0 , 0); //Alt Press
keybd_event(VK_TAB,0x8f,0 , 0); // Tab Press
keybd_event(VK_TAB,0x8f, KEYEVENTF_KEYUP,0); // Tab Release
keybd_event(VK_MENU,0xb8,KEYEVENTF_KEYUP,0); // Alt Release

See the artivle at http://www.codeproject.com/system/keyboard.asp ("Keyboard Events Simulation using keybd_event() function") for more in-depth information.

For the mouse, that would be

 mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, GetMessageExtraInfo()); // Left Key Down
 mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, GetMessageExtraInfo()); // Left Key UP
 
 mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, GetMessageExtraInfo()); // Right Key Down
 mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, GetMessageExtraInfo()); // Right Key Up

You'd usually specify mouse coordinates as teh 2nd and 3rd parameter.
0
 

Author Comment

by:Troudeloup
ID: 20346988
is it possible to tell mouse to move across the screen, not an instance move?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 500 total points
ID: 20347006
Yes, for that you'd use 'SetCursorPos()' (http://msdn2.microsoft.com/en-us/library/ms648394.aspx), e.g.

for (int i = 0; i < 100; ++i) {

  SetCursorPos(i,200);
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Troudeloup
ID: 20347014
wow a loop

oh boy this will be fun

I have to write loops to move mouse in a straight line!
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 500 total points
ID: 20347026
Well, the above line would already do that vertically. to move horizontally, use

for (int i = 0; i < 100; ++i) {

  SetCursorPos(200,i);
}
0
 

Author Comment

by:Troudeloup
ID: 20352670
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question