By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,960 Members | 2,459 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,960 IT Pros & Developers. It's quick & easy.

Send a mouse click?

P: n/a
I want my program to send a mouse click to the window at the current mouse
position, how do I do that?

Example:
I have my mouse over a button in Word and then my program is sending the
left mouse click and the button under the mouse is clicked.

Yours, Jonas
Jul 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
The I am using the "keybd_event" but the mouse buttons does not work,
VK_LBUTTON and VK_RBUTTON does not work, way???
The rest seams to work fine :-)
This is how i am faking a pressed key:

In a .bas file:

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal _
bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Public Enum enumKeys
VK_LBUTTON = 1
VK_RBUTTON = 2
VK_MBUTTON = 4 '/* NOT contiguous with L & RBUTTON */

VK_VOLUME_MUTE = 173
'Windows 2000/XP: Volume Mute key

VK_VOLUME_DOWN = &HAE
'Windows 2000/XP: Volume Down key

VK_VOLUME_UP = &HAF
'Windows 2000/XP: Volume Up key

VK_MEDIA_NEXT_TRACK = 176
'Windows 2000/XP: Next Track key

VK_MEDIA_Prev_TRACK = 177
'Windows 2000/XP: Previous Track key

VK_MEDIA_STOP = 178
'Windows 2000/XP: Stop Media key

VK_MEDIA_PLAY_PAUSE = 179
'Windows 2000/XP: Stop Media key

End Enum

Public Sub PressKeyVK(keyPress As enumKeys, Optional bHold As Boolean, _
Optional bRelease As Boolean, Optional bCompatible As Boolean)

keybd_event keyPress, 0, 0, 0

keybd_event keyPress, 0, 2, 0

End Sub

In a form File:

'To make Media Player Stop
PressKeyVK VK_MEDIA_STOP '***Works Fine

'Left Mouse botton
PressKeyVK VK_LBUTTON '*** Do NOT work at all!

'Right Mouse Button
PressKeyVK VK_RBUTTON '*** Do NOT work at all!
Yours, Jonas
"BadOmen" <ba*******@hotmail.com> skrev i meddelandet
news:po********************@newsb.telia.net...
I want my program to send a mouse click to the window at the current mouse
position, how do I do that?

Example:
I have my mouse over a button in Word and then my program is sending the
left mouse click and the button under the mouse is clicked.

Yours, Jonas

Jul 17 '05 #2

P: n/a
On Mon, 16 Feb 2004 14:37:00 GMT, "BadOmen" <ba*******@hotmail.com>
wrote:
The I am using the "keybd_event" but the mouse buttons does not work,
VK_LBUTTON and VK_RBUTTON does not work, way???
The rest seams to work fine :-)


Mouse_event is for mouse clicks

Keybd_event is for key presses
Jul 17 '05 #3

P: n/a

"J French" <er*****@nowhere.com> skrev i meddelandet
news:40**************@news.btclick.com...
On Mon, 16 Feb 2004 14:37:00 GMT, "BadOmen" <ba*******@hotmail.com>
wrote:
The I am using the "keybd_event" but the mouse buttons does not work,
VK_LBUTTON and VK_RBUTTON does not work, way???
The rest seams to work fine :-)


Mouse_event is for mouse clicks

Keybd_event is for key presses


Ops, thanx

Now I so that Keybd_event and Mouse_event is superseded. I don't really
know what it means, but I think It's best avoiding it.
I am using the SendInput function instead, but I don't really understand how
it works. The msdn is not that simple I think, plus everything I found on
SendInput was written in C/C++.

This is how I am doing it (I found an example and changed it a bit)
Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As
Any, pSrc As Any, ByVal ByteLen As Long)
Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long,
pInputs As INPUT_TYPE, ByVal cbSize As Long) As Long

Public Enum MouseClick 'Set the constans of the mouse buttons
MOUSEEVENTF_LEFTDOWN = &H2
MOUSEEVENTF_LEFTUP = &H4
MOUSEEVENTF_RIGHTDOWN = &H8
MOUSEEVENTF_RIGHTUP = &H10
MOUSEEVENTF_MIDDLEDOWN = &H20
MOUSEEVENTF_MIDDLEUP = &H40
End Enum

Private Const INPUT_MOUSE = 0
Private Const INPUT_KEYBOARD = 1
Private Const INPUT_HARDWARE = 2

Type MOUSEINPUT
dx As Long
dy As Long
mouseData As Long
dwFlags As Long
dwtime As Long
dwExtraInfo As Long
End Type

'***This I would be niding to make a KeyPress, But I dont know how...
Type KEYBDINPUT
wVk As Integer
wScan As Integer
dwFlags As Long
time As Long
dwExtraInfo As Long
End Type

Type INPUT_TYPE
dwType As Long
xi(0 To 23) As Byte
End Type

Public Sub ClickDaMouse(ButtonPressed As MouseClick, ButtonRelise As
MouseClick)

Dim intX As Integer
Dim inputEvents(0 To 1) As INPUT_TYPE ' holds information about each
event
Dim mouseEvent As MOUSEINPUT ' temporarily hold mouse input info

' Load the information needed to synthesize pressing the mouse button.
mouseEvent.dx = 0 ' no horizontal movement
mouseEvent.dy = 0 ' no vertical movement
mouseEvent.mouseData = 0 ' not needed
mouseEvent.dwFlags = ButtonPressed ' button down
mouseEvent.dwtime = 0 ' use the default
mouseEvent.dwExtraInfo = 0 ' not needed
' Copy the structure into the input array's buffer.
inputEvents(0).dwType = INPUT_MOUSE ' mouse input
CopyMemory inputEvents(0).xi(0), mouseEvent, Len(mouseEvent)

' Do the same as above, but for releasing the mouse button.
mouseEvent.dx = 0 ' no horizontal movement
mouseEvent.dy = 0 ' no vertical movement
mouseEvent.mouseData = 0 ' not needed
mouseEvent.dwFlags = ButtonRelise ' button up
mouseEvent.dwtime = 0 ' use the default
mouseEvent.dwExtraInfo = 0 ' not needed
' Copy the structure into the input array's buffer.
inputEvents(1).dwType = INPUT_MOUSE ' mouse input
CopyMemory inputEvents(1).xi(0), mouseEvent, Len(mouseEvent)

' Now that all the information for the 2 input events has been placed
' into the array, finally send it into the input stream.
intX = SendInput(2, inputEvents(0), Len(inputEvents(0))) ' place the
events into the stream

End Sub

++++++++

I think I can use this keys because with them I don't need to specified a
window to sent the information to:

VK_VOLUME_MUTE = 173
'Windows 2000/XP: Volume Mute key
VK_VOLUME_DOWN = &HAE

'Windows 2000/XP: Volume Down key

VK_VOLUME_UP = &HAF
'Windows 2000/XP: Volume Up key
VK_MEDIA_NEXT_TRACK = 176
'Windows 2000/XP: Next Track key

VK_MEDIA_Prev_TRACK = 177
'Windows 2000/XP: Previous Track key

VK_MEDIA_STOP = 178
'Windows 2000/XP: Stop Media key

VK_MEDIA_PLAY_PAUSE = 179
'Windows 2000/XP: Stop Media key
But I want to be able to send Alt+X or CTRL+4 and Shift+a or something like
that to a specific window not the one that have the focus.
maybe I can use this:

Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long

WinAMPhWnd = FindWindow("Winamp v1.x", vbNullString)

Than I think if I have understand it correct that WinAMPhWnd is holding the
unique ID for WinAmp in this case, which is what hWnd stands for in some
code???

No I don't know how to use that ID to send a key to that window...???

Yours, Jonas
Jul 17 '05 #4

P: n/a
On Thu, 19 Feb 2004 16:25:34 GMT, "BadOmen" <ba*******@hotmail.com>
wrote:

<snip>

Than I think if I have understand it correct that WinAMPhWnd is holding the
unique ID for WinAmp in this case, which is what hWnd stands for in some
code???

No I don't know how to use that ID to send a key to that window...???


I don't use the SendInput API as it is not supported on Win95
- I like to write for older platforms

However you will find an example in the downloadable API Guide from
www.AllAPI.net

I assume that it sends the data to the current Window with focus
- so you will need to find your target App's hWnd and use
SetForegroundWindow to it

I can't work out how much you know about Windows Handles, but assuming
it is very little, a hWnd is simply a unique number that Windows uses
to identify each 'window'
- that number is pretty random and can even (sometimes) change at
runtime
- to locate a specific window you need to know something about it,
which can be a painful process

This snooper may give you some hints:

Option Explicit

' Add one Timer

Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function GetCursorPos _
Lib "user32" _
(lpPoint As POINTAPI) As Long

Private Declare Function WindowFromPoint _
Lib "user32" _
(ByVal xPoint As Long, _
ByVal yPoint As Long) As Long

Private Declare Function GetClassName _
Lib "user32" _
Alias "GetClassNameA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) 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 Const WM_GETTEXT = &HD
Private Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 500
End Sub

Private Function WindowInf() As String
Dim Hnd As Long, Buff$, Q&
Dim PT As POINTAPI

GetCursorPos PT
Hnd = WindowFromPoint(PT.X, PT.Y)

WindowInf = Str$(Hnd)
' ---
Buff$ = Space$(255)
Q& = GetClassName(Hnd, Buff$, Len(Buff$))
WindowInf = WindowInf + ":" + Left$(Buff$, Q)
' ---
Q& = SendMessage(Hnd, WM_GETTEXT, Len(Buff$), ByVal Buff$)
WindowInf = WindowInf + ":" + Left$(Buff$, Q)

End Function
Private Sub Timer1_Timer()
Me.Caption = WindowInf
End Sub


Jul 17 '05 #5

P: n/a
Thanx that helped :)

I have posting a new message:
"Can I see if an app is bieng closed if I have it's hWnd?"
:-)

But I will use your method if I don't have the hWnd already, by searching
for the title I now know the program have by using your little program :)

Yours, Jonas
"J French" <er*****@nowhere.com> skrev i meddelandet
news:40****************@news.btclick.com...
On Thu, 19 Feb 2004 16:25:34 GMT, "BadOmen" <ba*******@hotmail.com>
wrote:

<snip>

Than I think if I have understand it correct that WinAMPhWnd is holding theunique ID for WinAmp in this case, which is what hWnd stands for in some
code???

No I don't know how to use that ID to send a key to that window...???


I don't use the SendInput API as it is not supported on Win95
- I like to write for older platforms

However you will find an example in the downloadable API Guide from
www.AllAPI.net

I assume that it sends the data to the current Window with focus
- so you will need to find your target App's hWnd and use
SetForegroundWindow to it

I can't work out how much you know about Windows Handles, but assuming
it is very little, a hWnd is simply a unique number that Windows uses
to identify each 'window'
- that number is pretty random and can even (sometimes) change at
runtime
- to locate a specific window you need to know something about it,
which can be a painful process

This snooper may give you some hints:

Option Explicit

' Add one Timer

Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function GetCursorPos _
Lib "user32" _
(lpPoint As POINTAPI) As Long

Private Declare Function WindowFromPoint _
Lib "user32" _
(ByVal xPoint As Long, _
ByVal yPoint As Long) As Long

Private Declare Function GetClassName _
Lib "user32" _
Alias "GetClassNameA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) 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 Const WM_GETTEXT = &HD
Private Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 500
End Sub

Private Function WindowInf() As String
Dim Hnd As Long, Buff$, Q&
Dim PT As POINTAPI

GetCursorPos PT
Hnd = WindowFromPoint(PT.X, PT.Y)

WindowInf = Str$(Hnd)
' ---
Buff$ = Space$(255)
Q& = GetClassName(Hnd, Buff$, Len(Buff$))
WindowInf = WindowInf + ":" + Left$(Buff$, Q)
' ---
Q& = SendMessage(Hnd, WM_GETTEXT, Len(Buff$), ByVal Buff$)
WindowInf = WindowInf + ":" + Left$(Buff$, Q)

End Function
Private Sub Timer1_Timer()
Me.Caption = WindowInf
End Sub

Jul 17 '05 #6

P: n/a
On Fri, 20 Feb 2004 14:11:17 GMT, "BadOmen" <ba*******@hotmail.com>
wrote:
Thanx that helped :)

I have posting a new message:
"Can I see if an app is bieng closed if I have it's hWnd?"
:-)

But I will use your method if I don't have the hWnd already, by searching
for the title I now know the program have by using your little program :)


You will certainly not get a notification from the hWnd or 'Hooking'
that an app is being closed

( well you sort of can, but it is dangerous )

I'm not entirely sure what you want to do
- and it is likely that others will know more

So I suggest that you post again,
stating where you are and what you want
Jul 17 '05 #7

P: n/a
I have posted a new topic but i think I will have to do it like this than:
"J French" <er*****@nowhere.com> skrev i meddelandet
news:40****************@news.btclick.com...
On Fri, 20 Feb 2004 14:11:17 GMT, "BadOmen" <ba*******@hotmail.com>
wrote:
Thanx that helped :)

I have posting a new message:
"Can I see if an app is bieng closed if I have it's hWnd?"
:-)

But I will use your method if I don't have the hWnd already, by searching
for the title I now know the program have by using your little program :)


You will certainly not get a notification from the hWnd or 'Hooking'
that an app is being closed

( well you sort of can, but it is dangerous )

I'm not entirely sure what you want to do
- and it is likely that others will know more

So I suggest that you post again,
stating where you are and what you want

Jul 17 '05 #8

P: n/a
I accidently hit a combination on my keyboard that just posted my message...
:-P
I was just about to past this:
WinAMPhWnd = FindWindow("Winamp v1.x", vbNullString)
If WinAMPhWnd = 0 Then
MsgBox "WinAMP doesn't appear to be open!", vbExclamation, "Ack!"

End If

But i w0uld like to have some thing that could use the hWnd instead of the
tilted...

"BadOmen" <ba*******@hotmail.com> skrev i meddelandet
news:2l********************@newsb.telia.net...
I have posted a new topic but i think I will have to do it like this than:
"J French" <er*****@nowhere.com> skrev i meddelandet
news:40****************@news.btclick.com...
On Fri, 20 Feb 2004 14:11:17 GMT, "BadOmen" <ba*******@hotmail.com>
wrote:
Thanx that helped :)

I have posting a new message:
"Can I see if an app is bieng closed if I have it's hWnd?"
:-)

But I will use your method if I don't have the hWnd already, by searchingfor the title I now know the program have by using your little program
:)
You will certainly not get a notification from the hWnd or 'Hooking'
that an app is being closed

( well you sort of can, but it is dangerous )

I'm not entirely sure what you want to do
- and it is likely that others will know more

So I suggest that you post again,
stating where you are and what you want


Jul 17 '05 #9

P: n/a
On Sat, 21 Feb 2004 15:02:53 GMT, "BadOmen" <ba*******@hotmail.com>
wrote:
I accidently hit a combination on my keyboard that just posted my message...
:-P
I was just about to past this:
WinAMPhWnd = FindWindow("Winamp v1.x", vbNullString)
If WinAMPhWnd = 0 Then
MsgBox "WinAMP doesn't appear to be open!", vbExclamation, "Ack!"

End If

But i w0uld like to have some thing that could use the hWnd instead of the
tilted...


I do not understand 'tilted'
.... Do you mean 'Title' ?

Look, let us sort one thing out /clearly/
- the hWnd is just an arbitrary number assigned by the OS
Jul 17 '05 #10

P: n/a

"J French" <er*****@nowhere.com> skrev i meddelandet
news:40***************@news.btclick.com...
On Sat, 21 Feb 2004 15:02:53 GMT, "BadOmen" <ba*******@hotmail.com>
wrote:
I accidently hit a combination on my keyboard that just posted my message...:-P
I was just about to past this:
WinAMPhWnd = FindWindow("Winamp v1.x", vbNullString)
If WinAMPhWnd = 0 Then
MsgBox "WinAMP doesn't appear to be open!", vbExclamation, "Ack!"

End If

But i w0uld like to have some thing that could use the hWnd instead of thetilted...


I do not understand 'tilted'
... Do you mean 'Title' ?

Look, let us sort one thing out /clearly/
- the hWnd is just an arbitrary number assigned by the OS


I meant title but the spell checking program ducked(fucked) it up :P

I know that hWnd is a unique Id that specifie a program or a window. So if I
had a programs unique ID (assign by the OS) than I hoped I could get the
messages from that program when the program is tolled to close by the user
so I know when I can't send commands to that program...
like a close command if I want.

Ok... Now I have reconfigured it and made it much simpler... I just make a
check if the program is still running before I send any commands of any
kind...

I make the check like this:

WinAMPhWnd = FindWindow("Winamp v1.x", vbNullString)
If WinAMPhWnd = 0 Then
MsgBox "WinAMP doesn't appear to be open!", vbExclamation, "Ack!"

End If

Some background info on what I am doing:

I have made a program that works with a Infra Read Remote I can as an
example move the mouse, click the mouse buttons or start WinAmp5 and
play/pause, next, back Fast Forward and rew and more.

Now if I start WinAmp with the remote control then I get WinAmps hWin if
WinAmp is started "by hand" then I use WinAMPhWnd = FindWindow("Winamp
v1.x", vbNullString) to get it's hWind.

Then I check before I send a command to WinAmp If WinAMPhWnd is True (or
rather not 0, False) then I send the command..

Now I want to be able to take the hWnd from the active window using the
GetActiveWindow API but it just returns 0 if I have WinAmp5 as the active
window. I don't know way???
Then I want to use this to quite the active program.

Dim ReturnState As Long

ReturnState = PostMessage(hWndHandler, WM_QUIT, 0&, 0&)

If I use this WinAMPhWnd = FindWindow("Winamp v1.x", vbNullString) to get
it's hWind an than use PostMessage it works fine and quits WinAmp. But I
want it to work on all active programs...

I have just used VB for one or maybe two month so I am a bit new to this...
I don't know "all the good things and all the bad thing, that may be"...
:-P

I have posted a new topic acing about under the subject "Quitting a window,
using GetActiveWindow() API, returns 0???" because I think it not about
sending a mouse click that this subject was in the beginning... :)

Yours, Jonas

Jul 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.