Solved

keyboard and mouse events - specific code

Posted on 2007-11-25
6
274 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now