473,725 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 32644
The I am using the "keybd_even t" 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_T RACK = 176
'Windows 2000/XP: Next Track key

VK_MEDIA_Prev_T RACK = 177
'Windows 2000/XP: Previous Track key

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

VK_MEDIA_PLAY_P AUSE = 179
'Windows 2000/XP: Stop Media key

End Enum

Public Sub PressKeyVK(keyP ress 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*******@hotm ail.com> skrev i meddelandet
news:po******** ************@ne wsb.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*******@hotm ail.com>
wrote:
The I am using the "keybd_even t" 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*****@nowher e.com> skrev i meddelandet
news:40******** ******@news.btc lick.com...
On Mon, 16 Feb 2004 14:37:00 GMT, "BadOmen" <ba*******@hotm ail.com>
wrote:
The I am using the "keybd_even t" 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 "RtlMoveMem ory" (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_LEF TDOWN = &H2
MOUSEEVENTF_LEF TUP = &H4
MOUSEEVENTF_RIG HTDOWN = &H8
MOUSEEVENTF_RIG HTUP = &H10
MOUSEEVENTF_MID DLEDOWN = &H20
MOUSEEVENTF_MID DLEUP = &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(Bu ttonPressed 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.mous eData = 0 ' not needed
mouseEvent.dwFl ags = ButtonPressed ' button down
mouseEvent.dwti me = 0 ' use the default
mouseEvent.dwEx traInfo = 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.mous eData = 0 ' not needed
mouseEvent.dwFl ags = ButtonRelise ' button up
mouseEvent.dwti me = 0 ' use the default
mouseEvent.dwEx traInfo = 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_T RACK = 176
'Windows 2000/XP: Next Track key

VK_MEDIA_Prev_T RACK = 177
'Windows 2000/XP: Previous Track key

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

VK_MEDIA_PLAY_P AUSE = 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 "FindWindow A" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long

WinAMPhWnd = FindWindow("Win amp 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*******@hotm ail.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
SetForegroundWi ndow 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 "GetClassNa meA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessag eA"
( _
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(Hn d, 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*****@nowher e.com> skrev i meddelandet
news:40******** ********@news.b tclick.com...
On Thu, 19 Feb 2004 16:25:34 GMT, "BadOmen" <ba*******@hotm ail.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
SetForegroundWi ndow 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 "GetClassNa meA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessag eA"
( _
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(Hn d, 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*******@hotm ail.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*****@nowher e.com> skrev i meddelandet
news:40******** ********@news.b tclick.com...
On Fri, 20 Feb 2004 14:11:17 GMT, "BadOmen" <ba*******@hotm ail.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("Win amp 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*******@hotm ail.com> skrev i meddelandet
news:2l******** ************@ne wsb.telia.net.. .
I have posted a new topic but i think I will have to do it like this than:
"J French" <er*****@nowher e.com> skrev i meddelandet
news:40******** ********@news.b tclick.com...
On Fri, 20 Feb 2004 14:11:17 GMT, "BadOmen" <ba*******@hotm ail.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*******@hotm ail.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("Win amp 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
22754
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 following are the problems and I hope that there is someone who can enlighten me or give me some pointers. Also, my testing code is attached at the end. And please don't ask me why I am doing this - it is one of functional requirements by all...
3
3607
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 purpose is to get the position of the mouse pointer on the bitmap. However, for some reason, the left (I also tried right) mouse clicks are not intercepted. I new to Python and wxWindows so any help would be greatly appreciated.
1
4521
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 it does not work. Would appreciate any help on this. Thanks in advance
3
2074
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 forecolor, backcolor and font. The labels also are rotatable and the text can also be flipped 180 degrees (the flipped text part is still being worked on). I have one context menu for all 30 labels that allows for the property changes to the labels....
5
4746
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 gets triggered and the double click is not identified at all. Is there any way I can invoke the double click when the mouse move also exists?
2
4407
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 seems a little silly. Anyone know how to circumvent this? Right now I am having the function that is bound to the double click event undo the action the single click event performs. This is annoying and it flashes the single click event for a...
19
5941
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 the page but I find that the iframes interfere with the getting the mouse coords from the screen which is essential in moving the scroll bar around. My test html is given below. With the iframe hidden the mouse coords are obtainable. With the...
5
574
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 left and right). When I select the form and go to the events tab (the bolt icon in properties), it gives me several options to choose from...
2
6975
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 reliable) way to have the link open in a new window/tab (using JS)? I have no control over the JS already on the page - this is for a selenium user extension that I'd like to write.
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9179
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.