Does anyone know how to press enter in another program. I need this because I use lots of OCX's and some of them have a window that pops up when ever I load my program, and if you press enter, it closes the window. Please Help
Printable View
Does anyone know how to press enter in another program. I need this because I use lots of OCX's and some of them have a window that pops up when ever I load my program, and if you press enter, it closes the window. Please Help
how do I do that?
For sendkey, you do:
[/code]
Sendkeys "{Enter}"
or
Sendkeys "~"
[/code]
For api, try:
[/code]
Declare Function sendmessagebynum Lib "User" Alias "SendMessage" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Long) As Integer
Global Const WM_CHAR = &H102
Sub key_enter(ByVal hWnd As Integer)
X = sendmessagebynum(hWnd%, WM_CHAR, 13, 0)
End Sub
[/code]
I recommend "SendMessage" over "SendKeys". Sendkeys is a unstable and not too reliable. It is use for more simple process.
Here is a sample of SendMessage, SendMessageByNum, and SendMessageString.
Code:Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function Findwindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SendMessageByNum Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Const WM_CLOSE = &H10
Sub p_Close_Dialog()
Dim lng_Dialog As Long
Dim lng_Button As Long
'PURPOSE: Get the handle of an application by the classname
lng_Dialog = Findwindow("#32770", vbNullString)
'lng_Dialog = Findwindow("XLMAIN", vbNullString)
'PURPOSE: Get extra information of that application
lng_Button = FindWindowEx(lng_Dialog, 0&, "Button", vbNullString)
'PURPOSE: Press the Buttons - Method #1
Call SendMessageByNum(lng_Button, WM_LBUTTONDOWN, 0, 0&)
Call SendMessageByNum(lng_Button, WM_LBUTTONUP, 0, 0&)
'PURPOSE: Press the Buttons - Method #2
Call SendMessage(lng_Button, WM_LBUTTONDOWN, 0, 0&)
Call SendMessage(lng_Button, WM_LBUTTONUP, 0, 0&)
'PURPOSE: Close the button
Call SendMessageByString(lng_Button, WM_CLOSE, 0, 0&)
'PURPOSE: Close the message box
Call SendMessageByString(lng_Dialog, WM_CLOSE, 0, 0&)
End Sub
If you want samples of "Sendkeys", hit F1 when you place your cursor on the word "Sendkeys". Keep in mind that "Sendkeys" is case sensitive. It is better to use capital letters.
how do I make the mouse click on the center of the screen?
Use the SetCursorPos API to move it to the center and use the SendMessage API to click the button.
any code that does not include API? because I don't know how to use that
It is pretty easy once you use it. Also you have to do is specify the X and Y coordinates for it.
Code:Declare Function SetCursorPos Lib "user32" Alias "SetCursorPos" (ByVal x As Long, ByVal y As Long) As Long
' To use it, just specify an X and Y value for the cursor.
' Put the below code in a CommandButton.
Private Sub Command1_Click
SetCursorPos 100, 100
End Sub
it didn't work
If you placed the codes in the form, then make sure you place the word "PRIVATE" infront of the API. Like this:
Drop a command button on the form and place these codes into the form too.Code:Private Declare Function SetCursorPos Lib "user32" Alias "SetCursorPos" (ByVal x As Long, ByVal y As Long) As Long
Code:Private Sub Command1_Click
SetCursorPos 100, 100
End Sub
That code moves the Mouse to (100,100). You can move it to the center of the screen by using some properties of the Screen object, for example, Width / 2 and Height / 2.
Then use the SendMessage to click the button. I believe the message is WM_LBUTTONDOWN.
Can someone please tell me what I did wrong, What happens is that it brings the mouse to the lower right corner of the screen:
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Sub Form_Load()
SetCursorPos Screen.Width / 2, Screen.Height / 2
End Sub
PS.... you know how you make the code look like how it looks in Visual Basic? how do you do that?
try this:
because the screen.width and height gives you the width and height in twips, and setCursorPos deals with Pixels, so you have to conver Twips into Pixels, thats it.Code:SetCursorPos (Screen.Width / 2) / Screen.TwipsPerPixelX, (Screen.Height / 2) / Screen.TwipsPerPixelY
and to show the code like vb see this link:
---> http://forums.vb-world.net/index.php?action=bbcode
Now when it gets to the middle, how do I make it click?