Solved

API access to the Mouse and Keybord control for VFP

Posted on 2004-08-19
22
1,141 Views
Last Modified: 2007-12-19
In Visual FoxPro I need to be able to programmatically move the mouse and click on EXTERNAL applications, just the way that VB does it.  Once I click on an external app I also need to be able to stuff Keys into the controls on that External app's controls.  Currently VFP allows me to move the mouse with the MOUSE command, but clicking on an EXTERNAL APP is a different thing.  It does not work on External Apps.  Keyboard stuffing too woks internally with the KEYBOARD command, but that only applies to the VFP session.  I suspect that an API exists for this, since VB Keyboard Macro apps do this , but which APIs is it???  

TIA


Dennis Kean
0
Comment
Question by:DennisKean
  • 12
  • 9
22 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11843271
This is VB6 code to simulate keypress:

Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Sub SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long)

Private Sub Command1_Click()
    keybd_event vbKeyControl, MapVirtualKey(vbKeyControl, 0), 0, 0  ' CTRL down
    SendKeys "asdasdasdasd"
    SetCursorPos 200, 200
    keybd_event vbKeyControl, MapVirtualKey(vbKeyControl, 0), 2, 0  ' CTRL up
End Sub

Bob
0
 

Author Comment

by:DennisKean
ID: 11843564
Many thanx for your reply, Bob, but being that I am not a VB expert some things are yet hazy to me.  In VFP syntax for the SetCursoPos function the syntax is as follows:

DECLARE integer SetCursorPos in WIN32API integer, integer
=SetCursorPos(20,20)

From that I understand your VB syntax

   SendKeys "asdasdasdasd"
   SetCursorPos 200, 200

The Mouse Down and Up is what confuses me...  I do not know how to convert this hybrid of two functions as you have inscribed it..  " keybd_event vbKeyControl, MapVirtualKey(vbKeyControl, 0), 0, 0  ' CTRL down"
The "vbKeyControl" clearly is some VB syntax...  Can you give me a tip as to what it represents in VB, so I can translate it in VFP?

TIA

Dennis Kean
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11844009
Const vbKeyControl = 17 (Decimal)

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11844046
0
 

Author Comment

by:DennisKean
ID: 11844180
So this is still a Keyboard command.  So, how can I click and doubleclick on a control of a form or perhaps first activate the form and then click on a control via an API?  Can you give me an example of that ignoring the keyboard commands entirely, in this example?

TIA  


Dennis Kean

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11844219
The keyboard APIs were what I had laying around from something else.  I don't have anything for the mouse simulation in my back pocket, I would have to find it.

Bob
0
 

Author Comment

by:DennisKean
ID: 11844276
While on the Keyboard issue, why is it needed to call the two UP and DOWN calls??

   keybd_event vbKeyControl, MapVirtualKey(vbKeyControl, 0), 0, 0  ' CTRL down

   keybd_event vbKeyControl, MapVirtualKey(vbKeyControl, 0), 0, 0  ' CTRL up

Why not just make the other two calls directly?  Is that some VB requirement?  


Dennis Kean

 
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11844287
Simulate mouse click:


Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, _
    ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, _
    ByVal dwExtraInfo As Long)

Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, _
    ByVal y As Long) As Long

Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4

Private Sub Form_Load()
    Timer1.Interval = 1000 ' one second
End Sub

Public Sub MouseClick(x As Long, y As Long)
    SetCursorPos x, y
    mouse_event MOUSEEVENTF_LEFTDOWN, x, y, 0, 0
    mouse_event MOUSEEVENTF_LEFTUP, x, y, 0, 0
End Sub

Bob
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 125 total points
ID: 11844306
No, that is some Windows thing.  A keypress is actually two movements, key up and key down.  It is the same for the mouse--mouse up and mouse down.

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11844547
I am curious about something.  I gather that you are new to this site, and I love to point people in the right direction.  

Grading questions:
http://www.experts-exchange.com/help.jsp#hi55

I only ask to make sure that you understand the grading policy:  Why would the help that I gave you only deserve a "B"?  What is your criteria?  Unfair grading is a quick way of alienating those experts that are out there to help you.

Bob
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:DennisKean
ID: 11844605
I thought I pressed excellent, at least I meant to click that button...  I do not understand how it got flipped???   Maybe I was clicking too fast!  Can I redo this to correct it???


Dennis Kean
0
 

Author Comment

by:DennisKean
ID: 11844712
I read the rules and WOW!  Looks like I walked right into the middle of a highway with fast racing cars!  I will have to be more careful about things...  And yes...  I am quite new at this.   I just subscribed a few hours ago.  Thanx so much for your help.  

Let me know if I can correct that mark I gave you by mistake...  Your replies were all excellent!  

Sincerely,


Dennis Kean
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11845316
Can I get a grade changed?
http://www.experts-exchange.com/help.jsp#hi18

Bob
0
 

Author Comment

by:DennisKean
ID: 11845567
Okay I give up...  I spent too much time trying to figure out how to get a hold of the moderator!  It says:

 Can I get a grade changed?

If you are the person who asked the question, then yes, you can, by asking the Moderators to change the grade -- although they won't be inclined to lower it.


Where do I contact those Moderators?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11845602
I think it is the one thing that nobody really understands when they come here for the first time.  Moderators are volunteers, like the rest of us, and they don't have the time to monitor the site 100% of the day.  To contact a moderator, post a free question in Community Support, and give them the URL to this question, and ask for the grade to be changed.

Community Support:
http://www.experts-exchange.com/Community_Support/

BTW, I have put in a suggestion that the bottom section of the page needs a link to 'Close a question' to automate this process better, since many people have struggled to find it.

Bob
0
 

Author Comment

by:DennisKean
ID: 11845736
I sent this post to the moderators, Bob!


Moderators

Please award a grade of EXCELLENT for the following question, which I mistakenly labeled as GOOD...  I'm an amateur at this, since I subscribed only hours ago!  Here is the Message URL

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21099984.html#11845602


Dennis Kean
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11845772
Did you post this into Community Support?

Bob
0
 

Author Comment

by:DennisKean
ID: 11845860
I did now!


Dennis Kean
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11845910
Good luck, and welcome to E-E!  Hope you have fun with Visual FoxPro.  I gave it up for lent years ago, and never went back.  They keep trying to pull me back in here, with our legacy FoxPro system, but I flat out refused, so someone else is working on that now. *GRIN*

Bob
0
 

Author Comment

by:DennisKean
ID: 11845950
What are you programmin in now???

Dennis
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11845974
Visual Basic.NET, VB6, and SQL Server 2000.

Bob
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 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