发送鼠标点击? [英] Send a mouse click?

查看:166
本文介绍了发送鼠标点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的程序鼠标点击鼠标点击当前鼠标的窗口

位置,我该怎么做?


示例:

我将鼠标放在Word中的一个按钮上,然后我的程序发送了鼠标左键单击鼠标,单击鼠标下的按钮。

你的,Jonas

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

推荐答案

我正在使用keybd_event但鼠标按钮不起作用,

VK_LBUTTON和VK_RBUTTON不起作用,方式???

其余接缝工作正常:-)

这就是我伪造按键的方式:


在.bas文件中:


私有声明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''/ *不与L& RBUTTON * /


VK_VOLUME_MUTE = 173

''Windows 2000 / XP:音量静音键

VK_VOLUME_DOWN = & HAE

''Windows 2000 / XP:Volume Down key


VK_VOLUME_UP =& HAF

''Windows 2000 / XP:音量增大键


VK_MEDIA_NEXT_TRACK = 176

''Windows 2000 / XP:下一曲目键


VK_MEDIA_Prev_TRACK = 177

''Windows 2000 / XP:上一曲目键

VK_MEDIA_STOP = 178

''Windows 2000 / XP:停止媒体密钥


VK_MEDIA_PLAY_PAUSE = 179

''Windows 2000 / XP:停止媒体密钥


结束枚举


Public Sub PressKeyVK(keyPress为enumKeys,可选bHold为布尔值,_

可选b发布为布尔值,可选bCompatible为布尔值)


keybd_event keyPress,0,0,0

keybd_event keyPress,0,2,0


End Sub


在表单文件中:


''让媒体播放器停止

PressKeyVK VK_MEDIA_STOP''***工作正常


' '左键鼠标

PressKeyVK VK_LBUTTON''***根本不工作!


''鼠标右键

PressKeyVK VK_RBUTTON''***根本不工作!

你的,Jonas

" BadOmen" < BA ******* @ hotmail.com> skrev i meddelandet

news:po ******************** @ newsb.telia.net ...
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...
我希望我的程序在当前鼠标位置发送鼠标点击窗口,我该怎么做?

示例:
我的鼠标悬停在一个按钮上在Word中然后我的程序发送鼠标左键单击并点击鼠标下的按钮。

你的,Jonas
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



2004年2月16日星期一14:37:00 GMT,BadOmen < ba ******* @ hotmail.com>

写道:
On Mon, 16 Feb 2004 14:37:00 GMT, "BadOmen" <ba*******@hotmail.com>
wrote:
我使用的是keybd_event。但鼠标按钮不起作用,
VK_LBUTTON和VK_RBUTTON不起作用,方式???
其余接缝工作正常: - )
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用于鼠标点击


Keybd_event用于按键



Mouse_event is for mouse clicks

Keybd_event is for key presses




" J法国" < ER ***** @ nowhere.com> skrev i meddelandet

news:40 ************** @ news.btclick.com ...

"J French" <er*****@nowhere.com> skrev i meddelandet
news:40**************@news.btclick.com...
2月16日星期一2004年14:37:00 GMT,BadOmen < ba ******* @ hotmail.com>
写道:
On Mon, 16 Feb 2004 14:37:00 GMT, "BadOmen" <ba*******@hotmail.com>
wrote:
我使用的是keybd_event。但鼠标按钮不起作用,
VK_LBUTTON和VK_RBUTTON不起作用,方式???
其余接缝工作正常: - )
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是用于鼠标点击

Keybd_event用于按键



Mouse_event is for mouse clicks

Keybd_event is for key presses




行动,比这更多


现在我这样做Keybd_event和Mouse_event被取代。我不是真的知道这意味着什么,但我认为最好避免它。

我正在使用SendInput功能,但我不知道我真的明白

是如何运作的。我认为msdn并不是那么简单,加上我在上面找到的所有东西
SendInput是用C / C ++编写的。


这就是我的做法(我找到了一个例子,并稍微改了一下)

选项明确


私有声明Sub CopyMemory Lib" kernel32"别名RtlMoveMemory (pDst As

Any,pSrc As Any,ByVal ByteLen As)

私有声明函数SendInput Lib" user32.dll" (ByVal nInputs As Long,

pInputs as INPUT_TYPE,ByVal cbSize As Long)As long


Public Enum MouseClick''设置鼠标按钮的常量

MOUSEEVENTF_LEFTDOWN =& H2

MOUSEEVENTF_LEFTUP =& H4

MOUSEEVENTF_RIGHTDOWN =& H8

MOUSEEVENTF_RIGHTUP =& H10

MOUSEEVENTF_MIDDLEDOWN =& H20

MOUSEEVENTF_MIDDLEUP =& H40

结束枚举


私人Const INPUT_MOUSE = 0

私有Const INPUT_KEYBOARD = 1

私有Const INPUT_HARDWARE = 2


类型MOUSEINPUT

dx As Long

dy As Long

mouseData As Long

dwFlags As Long

dwtime As Long

dwExtraInfo As Long

结束类型


''***这个我会吝啬制作一个KeyPress,但我不知道如何...

键入KEYBDINPUT

wVk作为整数

wScan作为整数

dwFlags As Long

时间为长

dwExtraInfo为长

结束类型


类型INPUT_TYPE

dwType As Long

xi(0到23)As Byte

结束类型


Public Sub ClickDaMouse(ButtonPressed As MouseClick, ButtonRelise As

MouseClick)


Dim intX As Integer

Dim inputEvents(0 To 1)由于INPUT_TYPE''包含有关的信息每个

事件

Dim mouseEvent由于MOUSEINPUT''暂时保存鼠标输入信息


''加载合成按下所需的信息鼠标按钮。

mouseEvent.dx = 0''没有水平移动

mouseEvent.dy = 0''没有垂直移动

mouseEvent。 mouseData = 0''不需要

mouseEvent.dwFlags = ButtonPressed''按钮

mouseEvent.dwtime = 0''使用默认值

mouseEvent.dwExtraInfo = 0''不需要

''将结构复制到输入数组的缓冲区。

inputEvents(0).dwType = INPUT_MOUSE''鼠标输入

CopyMemory inputEvents(0).xi(0),mouseEvent,Len( mouseEvent)


''和上面一样,但是释放鼠标按钮。

mouseEvent.dx = 0''没有水平移动
mouseEvent.dy = 0''没有垂直移动

mouseEvent.mouseData = 0''不需要

mouseEvent.dwFlags = ButtonRelise''按钮向上

mouseEvent.dwtime = 0''使用默认值

mouseEvent.dwExtraInfo = 0''不需要

''将结构复制到输入中数组的缓冲区。

inputEvents(1).dwType = INPUT_MOUSE''鼠标输入

CopyMemory inputEvents(1).xi(0),mouseEvent,Len(mouseEvent) )


''现在已经将2个输入事件的所有信息都放入数组中,最后将它发送到输入流中。

intX = SendInput(2, inputEvents(0),Len(inputEvents(0)))''将

事件放入流中


End Sub

+++++++


我想我可以使用这个键因为有了它们我不需要指定一个

窗口将信息发送到:


VK_VOLUME_MUTE = 173

''Windows 2000 / XP:音量静音键

VK_VOLUME_DOWN =& HAE


''Windows 2000 / XP:降低音量键


VK_VOLUME_UP =& HAF

''Windows 2000 / XP:增加音量键

VK_MEDIA_NEXT_TRACK = 176

''Windows 2000 / XP:下一曲目键


VK_MEDIA_Prev_TRACK = 177

''Windows 2000 / XP:上一曲目键

VK_MEDIA_STOP = 178

''Windows 2000 / XP:停止媒体密钥


VK_MEDIA_PLAY_PAUSE = 179

''Windows 2000 / XP:停止媒体密钥

但我希望能够发送Alt + X或CTRL + 4和Shift + a或somethin g

到特定窗口而不是那个有焦点的窗口。

也许我可以用这个:


声明函数FindWindow Lib" user32"别名FindWindowA (ByVal

lpClassName As String,ByVal lpWindowName As String)As long


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


我认为如果我理解WinAMPhWnd在这种情况下持有WinAmp的

唯一ID是正确的,这就是hWnd代表某些

代码???


不,我不知道如何使用该ID将密钥发送到该窗口...... ???


你的,Jonas



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


这篇关于发送鼠标点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆