Hey, I want to make a program so everytime I hit 'q' on my keyboard it left clicks my mouse.. I want it to work exactly as if I hit the mouse button myself.. is this possible?
Printable View
Hey, I want to make a program so everytime I hit 'q' on my keyboard it left clicks my mouse.. I want it to work exactly as if I hit the mouse button myself.. is this possible?
You will need a keyboard hook for that. Following api's might help you to start off :
setwindowshookex
unhookwindowshookex
keyboardproc
The forum has threads on this particular issue. Search for it.
Ty very much
How would I do the mouse click part? i dont want to use
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
cause for some reason they arent working on one application, they work every where else on the screen, but not on that application
You could use sendkeys to send vbKeyLButton to your application. Or do you want to click the button in some other application?
Yes I want to have it click on other applications
in a new form with this code you can move mouse with keyboard
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Dim myPos As POINTAPI
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
GetCursorPos myPos
Select Case KeyCode
Case 37, vbKeyA: myPos.x = myPos.x - 10 'Left key or A
Case 38, vbKeyW: myPos.y = myPos.y - 10 'Top key or W
Case 39, vbKeyD: myPos.x = myPos.x + 10 'Right Key or D
Case 40, vbKeyS: myPos.y = myPos.y + 10 'Down key or S
End Select
SetCursorPos myPos.x, myPos.y
End Sub
for click part i 'm learning and so I don't know....
i hope to explain you....eheh hi!
this might help you.
srisa, that could is useful but it sends the click to a handle, I dont want to do that, I want to just send the click whereever the cursor is
In post 6 you said you want to send the click event to another application. If that is the case you have to get a handle to the window of that application and send the message.
I want to send the click to a button on the application but none of the buttons have handles, the application only has 1 handle, its written in java or something, so I need to move the cursor there and click the button.. moving the cursor to the button is easy.. clicking it under normal circumstances would be easy, however this application somehow catches virtual mouse clicks and ignores them.. so I need a way simulate a mouse click that tricks the program and makes it think its a real click
So , what you want to do is to programatically click a command button in an application. With my limited knowledge , I think that is different from mouse_click event.
That being the case, you need to get the handle of the application using findwindow api. After getting the handle of the application window, you can get the handle to the button using findwindowex api, which gets the handle of the child window .(command button is considered to be a child window)
this code is from api guide.VB 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 tWnd = FindWindow("Shell_TrayWnd", vbNullString) 'Get the start-button's window handle bWnd = FindWindowEx(tWnd, ByVal 0&, "BUTTON", vbNullString)
After getting handle to the button you can use sendmessage api.
this might be of use.
If it's running in Windows, the button has a handle (it's an object that was drawn on the screen).Quote:
Originally Posted by bail3yz
You have to get the handle of the application whose window is under the cursor, then you have to get the handle of that button in that application, then you send the button a mouse left (or right) click.
You can't just send the application a click unless the application is subclassing all clicks and knows that a click means to execute that button's code - or the application checks the actual location of the cursor relative to itself before processing clicks. Do you do that in your click event? I don't.
No, the button does not have to have a handle.. for example a flash application buttons dont have handles, nor does a Java application... which is why I wanted to move the cursor to the location and click..
Something like this
Public Function ClickCoords(X As Long, Y As Long)
SetCursorPos X, Y
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
mouse_event MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0
End Function
Should work.. it works on every application, including mine and I dont check the location of the cursor before processing clicks..
How about this : Set the cursor position and then use sendmessage with
HWND_BROADCAST as the parameter for the window handle. Then all the top_level windows will receive the message.
I'll give that a shot, thank you.. Ill let you know if it works
nope, no luck there.. once again that method worked on every window except this one application.. i swear they are capturing the simulated mouse clicks somehow and blocking them
I am sorry, but I don't know much. Most of what I have posted is done by searching through google. That is one option left. Or if one of the experienced guys around here has a look at your post, they might help you out.
this link uses sendinput. See if that helps.
ya I tried that too lol no luck :(, thanks for all the help tho srisa
use AutoIT
and heres the code (should work)
When you use this you wont be able to use your mouse till you pause the script with w
VB Code:
HotKeySet("q","Click") HotKeySet("w","Waiting") Call("Waiting") Func Waiting() while 1 = 1 sleep(1000) wend EndFunc Func Click() $Pos = MouseGetPos() while 1 = 1 MouseClick("left",$Pos[0], $Pos[1], 1, 0) wend EndFunc
i will give that a shot, but if autoit could do it, shouldnt there be away to code it into a VB app?
that actually worked Bladez :D wish I knew how to do that in VB without 3rd party software..
i see autoit has a DLL version, is there anyway to load that into vb and get it to work via my VB app?
Heres a thread i found that has a example of how to do it
Thread
dude, you are my hero :D ty so much