Link to home
Start Free TrialLog in
Avatar of DennisKean
DennisKean

asked on

API access to the Mouse and Keybord control for VFP

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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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
Avatar of DennisKean
DennisKean

ASKER

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
Const vbKeyControl = 17 (Decimal)

Bob
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

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
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

 
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
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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:
https://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
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
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
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?
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:
https://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
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

https://www.experts-exchange.com/questions/21099984/API-access-to-the-Mouse-and-Keybord-control-for-VFP.html#11845602


Dennis Kean
Did you post this into Community Support?

Bob
I did now!


Dennis Kean
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
What are you programmin in now???

Dennis
Visual Basic.NET, VB6, and SQL Server 2000.

Bob