Solved

keyboard and mouse events - specific code

Posted on 2007-11-25
6
279 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

911 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

20 Experts available now in Live!

Get 1:1 Help Now