473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Send a mouse click?

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

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: punkin | last post by:
I am trying to catch mouse position on the entire screen by dynamically generating mouse click event at every 100 ms. My code only works for IEs but not any Netscape or Gecko-based browsers. The...
3
by: mitsura | last post by:
Hi, I have included a small listing. The test program opens a panel and show a bitmap. What I want is to when the mouse is over the bitmap panel, I want to trap the left mouse click. The...
1
by: Raj | last post by:
Hi, I am trying to send message (WM_CLICK) to a button control in a PowerBuilder application using a C# spy program. This works for all other windows applications but in case of PB application...
3
by: jcrouse | last post by:
I have created a form designer type application (with a lot of you peoples helpJ). It has label controls that are draggable at runtime. The user is also allowed to change some properties such as...
5
by: Nick | last post by:
Hey guys, I have 2 events on a windows forms datagrid, the mouse move as well as the double click events. What's happening is that when I double click on a row in the grid, the mouse move event...
2
by: scott_gui | last post by:
I am creating a Windows application: The mouse event <Double-Button-1> has a conflict when the <Button-1> event also has a binding. Double clicks will first perform the single click action. This...
19
by: wmanzo | last post by:
I have a really professional conspiracy movie site and I use tons of layers and an external scroll bar assembly. I would like to put the various sections into MS Iframes and in order to clean up...
5
by: Adeel | last post by:
Hi group! I'm trying to learn C# on my own... I'd appreciate it very much if someone can help me out here. I have a basic form without any controls on it... I want to catch mouse clicks (both...
2
by: wpollans | last post by:
Hello, I need to able to write JS that will click on a link with the middle mouse button - so that the link target will open in a new window or tab - using firefox. Or is there a better (more...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.