记录鼠标中键和滚轮滚动 [英] Record mouse Middle button and wheel scroll

查看:1061
本文介绍了记录鼠标中键和滚轮滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个类来记录鼠标动作,例如记录用鼠标任务(移动鼠标点击这里,点击左键那里......这一点。)

记录鼠标动作/任务后,我可以从类与线程我做了重现。

我需要的是实现鼠标和滚轮滚动太中间的按钮,但我不知道该怎么做了,心里有点难受,使用和理解的 GetAsyncKeyState ,我无法找到有关的信息的 GetAsyncKeyState中间的按钮状态或车轮滚动(滚动向下/向上)。

  #REGION记录鼠标类

[记录鼠标功能]

'//通过电力币^ h @克尔

例子:
Record_Mouse.Start_Record()
Record_Mouse.Stop_Record()
Record_Mouse.Play():虽然没有Record_Mouse.Play_Is_Completed:Application.DoEvents():结束在
Record_Mouse.Mouse_Speed​​ = 50

公共类Record_Mouse

'''<总结>
'''设置记录的速度/播放鼠标操作。
'''默认值是25。
'''< /总结>
公共共享Mouse_Speed​​作为的Int64 = 25

'''<总结>
'''获取PF当前鼠标播放状态。
'''假=鼠标任务仍在播放。
'''真=鼠标任务中发挥完成。
'''< /总结>
公共共享Play_Is_Completed由于布尔= FALSE

'其中鼠标coordenates将存储:
私人共享Coordenates_List作为新的列表(点)
当鼠标点击将被存储:
私人共享Clicks_Dictionary作为新词典(Int64的,MouseButton)
定时录制鼠标:
私人共享WithEvents就Record_Timer作为新的计时器
按钮点击次数来录制/回放浏览次数:
私人共享Click_Count作为的Int32 = 0
线程重现鼠标操作:
私人共享Thread_MousePlay_Var作为System.Threading.Thread =新Threading.Thread(AddressOf Thread_MousePlay)
API来记录当前的鼠标按钮的状态:
私人声明函数GetAsyncKeyState库USER32(BYVAL V键长)作为整数
API来重现一个鼠标按键点击:
私人声明子Mouse_Event库USER32别名mouse_event(BYVAL dwFlags中作为MouseButton,BYVAL DX作为整数,BYVAL DY作为整数,BYVAL dwData为整数,BYVAL dwExtraInfo作为整数)
GetAsyncKeyState按钮状态
私人共享Last_ClickState_Left作为的Int64 = -1
私人共享Last_ClickState_Right作为的Int64 = -1

枚举MouseButton

    Left_Down =安培; H2'左按钮(HOLD)
    Left_Up =安培; H4'左按钮(释放)

    Right_Down =安培; H8'右按钮(HOLD)
    Right_Up =安培; H10右键(释放)

    Middle_Down =安培; H20中键(HOLD)
    Middle_Up =安培; H40中键(释放)

    左'左键(preSS)
    右'右按钮(preSS)
    中东中键(preSS)

结束枚举

'''<总结>
'''开始录制在屏幕的鼠标操作。
'''它记录鼠标和左/右按键点击的位置。
'''< /总结>
公共共享子START_RECORD()
    Play_Is_Completed =假
    Record_Timer.Interval = Mouse_Speed
    Coordenates_List.Clear():Clicks_Dictionary.Clear():Click_Count = 0
    Record_Timer.Start()
结束小组

'''<总结>
'''停止录制鼠标操作。
'''< /总结>
公共共享子STOP_RECORD()
    Record_Timer.Stop()
结束小组

'''<总结>
'''重现的鼠标操作。
'''< /总结>
公共共享子播放()
    Thread_MousePlay_Var =新Threading.Thread(AddressOf Thread_MousePlay)
    Thread_MousePlay_Var.IsBackground = TRUE
    Thread_MousePlay_Var.Start()
结束小组

'程序用于存储鼠标操作
私人共享子Record_Timer_Tick(发送者为对象,E作为EventArgs的)把手Record_Timer.Tick

    Coordenates_List.Add(Control.MousePosition)

    如果尚未Last_ClickState_Left = GetAsyncKeyState(1)然后
        Last_ClickState_Left = GetAsyncKeyState(1)
        如果GetAsyncKeyState(1)= 32768然后
            Click_Count + = 1
            Coordenates_List.Add(没有)
            Clicks_Dictionary.Add(Click_Count,MouseButton.Left_Down)
        elseif的GetAsyncKeyState(1)= 0。然后
            Click_Count + = 1
            Coordenates_List.Add(没有)
            Clicks_Dictionary.Add(Click_Count,MouseButton.Left_Up)
        结束如果
    结束如果

    如果尚未Last_ClickState_Right = GetAsyncKeyState(2)然后
        Last_ClickState_Right = GetAsyncKeyState(2)
        如果GetAsyncKeyState(2)= 32768然后
            Click_Count + = 1
            Coordenates_List.Add(没有)
            Clicks_Dictionary.Add(Click_Count,MouseButton.Right_Down)
        elseif的GetAsyncKeyState(2)= 0。然后
            Click_Count + = 1
            Coordenates_List.Add(没有)
            Clicks_Dictionary.Add(Click_Count,MouseButton.Right_Up)
        结束如果
    结束如果

结束小组

过程中发挥鼠标中键(点击)
私人共享子Mouse_Click(BYVAL MouseButton作为MouseButton)
    选择案例MouseButton
        案MouseButton.Left:Mouse_Event(MouseButton.Left_Down,0,0,0,0):Mouse_Event(MouseButton.Left_Up,0,0,0,0)
        案MouseButton.Right:Mouse_Event(MouseButton.Right_Down,0,0,0,0):Mouse_Event(MouseButton.Right_Up,0,0,0,0)
        案MouseButton.Middle:Mouse_Event(MouseButton.Middle_Down,0,0,0,0):Mouse_Event(MouseButton.Middle_Up,0,0,0,0)
        情况下否则:Mouse_Event(MouseButton,0,0,0,0)
    最终选择
结束小组

用于螺纹再现鼠标操作
私人共享子Thread_MousePlay()

    Click_Count = 0

    对于每个Coordenate在Coordenates_List

        Threading.Thread.Sleep(Mouse_Speed​​)

        如果Coordenate =无再
            Click_Count + = 1
            如果Click_Count> 1然后Mouse_Click(Clicks_Dictionary.Item(Click_Count))
        其他
            Cursor.Position = Coordenate
        结束如果

        Application.DoEvents()

    下一个

    Play_Is_Completed = TRUE

结束小组

末级

#END地区
 

解决方案

您可以得到鼠标中键与GetAsyncKeyState(4)。但你永远不会得到这种方法鼠标滚动的消息,也没有任何办法,要求滚动按钮的位置。您的必须的依赖窗口消息,告诉您该鼠标已滚动,WM_MOUSEWHEEL。

这是一个记录时,即使你自己的窗口没有焦点,需要一个完全不同的方法要求,你需要使用一个低级别挂钩。钩子通过调用SetWindowsHookEx()设置,它会在鼠标事件发生在你的程序中调用的方法。

在张贴$ C $下,没有点,还有的许多的样本可用于在互联网上。正确的谷歌查询是vb.net调用SetWindowsHookEx WH_MOUSE_LL。 <一href="http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CCsQFjAA&url=http://support.microsoft.com/kb/319524&ei=J5qoUdXAFKiFywHg0oDwDw&usg=AFQjCNEFb9ELUYnY24YrIyVlUmRX5_Oonw&sig2=6nJraQxXlfZwFuM7E7Aa7w&bvm=bv.47244034,d.aWc">The先打已经是非常不错的,在微软知识库文章有你需要的几乎所有的东西。它使用SetWindowsHookEx函数的方式()不但是需要调整,如原来只是记录鼠标事件用于你自己的程序。相反,使用调用LoadLibrary(user32.dll中),并为第四个参数的返回值传递的第三个参数,0得到它来记录每一个鼠标事件。

I've made a Class to record the mouse actions, for example to record a task with the mouse (move mouse here and click left button there and... that.)

After recording the mouse actions/task, I can reproduce it from the Class with a thread I made.

What I need is to implement the Middle button of the mouse and the Wheel scrolls too, but I don't have idea how to do that, was a little hard for me to use and understand "GetAsyncKeyState" and I can't found information about "GetAsyncKeyState middle button state" or the wheel scroll (to scroll down/up).

#Region " Record Mouse Class "

' [ Record Mouse Functions ]
'
' // By Elektro H@cker
'
' Examples :
' Record_Mouse.Start_Record()
' Record_Mouse.Stop_Record()
' Record_Mouse.Play() : While Not Record_Mouse.Play_Is_Completed : Application.DoEvents() : End While
' Record_Mouse.Mouse_Speed = 50

Public Class Record_Mouse

''' <summary>
''' Sets the speed of recording/playing the mouse actions.
''' Default value is 25.
''' </summary>
Public Shared Mouse_Speed As Int64 = 25

''' <summary>
''' Gets the status pf the current mouse play.
''' False = mouse task is still playing.
''' True = Mouse task play is done.
''' </summary>
Public Shared Play_Is_Completed As Boolean = False

' Where the mouse coordenates will be stored:
Private Shared Coordenates_List As New List(Of Point)
' Where the mouse clicks will be stored:
Private Shared Clicks_Dictionary As New Dictionary(Of Int64, MouseButton)
' Timer to record the mouse:
Private Shared WithEvents Record_Timer As New Timer
' Button click count to rec/play clicks:
Private Shared Click_Count As Int32 = 0
' Thread to reproduce the mouse actions:
Private Shared Thread_MousePlay_Var As System.Threading.Thread = New Threading.Thread(AddressOf Thread_MousePlay)
' API to record the current mouse button state:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
' API to reproduce a mouse button click:
Private Declare Sub Mouse_Event Lib "User32" Alias "mouse_event" (ByVal dwFlags As MouseButton, ByVal dx As Integer, ByVal dy As Integer, ByVal dwData As Integer, ByVal dwExtraInfo As Integer)
' GetAsyncKeyState buttons status
Private Shared Last_ClickState_Left As Int64 = -1
Private Shared Last_ClickState_Right As Int64 = -1

Enum MouseButton

    Left_Down = &H2    ' Left button (hold)
    Left_Up = &H4      ' Left button (release)

    Right_Down = &H8   ' Right button (hold)
    Right_Up = &H10    ' Right button (release)

    Middle_Down = &H20 ' Middle button (hold)
    Middle_Up = &H40   ' Middle button (release)

    Left               ' Left   button (press)
    Right              ' Right  button (press)
    Middle             ' Middle button (press)

End Enum

''' <summary>
''' Starts recording the mouse actions over the screen.
''' It records the position of the mouse and left/right button clicks.
''' </summary>
Public Shared Sub Start_Record()
    Play_Is_Completed = False
    Record_Timer.Interval = Mouse_Speed
    Coordenates_List.Clear() : Clicks_Dictionary.Clear() : Click_Count = 0
    Record_Timer.Start()
End Sub

''' <summary>
''' Stop recording the mouse actions.
''' </summary>
Public Shared Sub Stop_Record()
    Record_Timer.Stop()
End Sub

''' <summary>
''' Reproduce the mouse actions.
''' </summary>
Public Shared Sub Play()
    Thread_MousePlay_Var = New Threading.Thread(AddressOf Thread_MousePlay)
    Thread_MousePlay_Var.IsBackground = True
    Thread_MousePlay_Var.Start()
End Sub

' Procedure used to store the mouse actions
Private Shared Sub Record_Timer_Tick(sender As Object, e As EventArgs) Handles Record_Timer.Tick

    Coordenates_List.Add(Control.MousePosition)

    If Not Last_ClickState_Left = GetAsyncKeyState(1) Then
        Last_ClickState_Left = GetAsyncKeyState(1)
        If GetAsyncKeyState(1) = 32768 Then
            Click_Count += 1
            Coordenates_List.Add(Nothing)
            Clicks_Dictionary.Add(Click_Count, MouseButton.Left_Down)
        ElseIf GetAsyncKeyState(1) = 0 Then
            Click_Count += 1
            Coordenates_List.Add(Nothing)
            Clicks_Dictionary.Add(Click_Count, MouseButton.Left_Up)
        End If
    End If

    If Not Last_ClickState_Right = GetAsyncKeyState(2) Then
        Last_ClickState_Right = GetAsyncKeyState(2)
        If GetAsyncKeyState(2) = 32768 Then
            Click_Count += 1
            Coordenates_List.Add(Nothing)
            Clicks_Dictionary.Add(Click_Count, MouseButton.Right_Down)
        ElseIf GetAsyncKeyState(2) = 0 Then
            Click_Count += 1
            Coordenates_List.Add(Nothing)
            Clicks_Dictionary.Add(Click_Count, MouseButton.Right_Up)
        End If
    End If

End Sub

' Procedure to play a mouse button (click)
Private Shared Sub Mouse_Click(ByVal MouseButton As MouseButton)
    Select Case MouseButton
        Case MouseButton.Left : Mouse_Event(MouseButton.Left_Down, 0, 0, 0, 0) : Mouse_Event(MouseButton.Left_Up, 0, 0, 0, 0)
        Case MouseButton.Right : Mouse_Event(MouseButton.Right_Down, 0, 0, 0, 0) : Mouse_Event(MouseButton.Right_Up, 0, 0, 0, 0)
        Case MouseButton.Middle : Mouse_Event(MouseButton.Middle_Down, 0, 0, 0, 0) : Mouse_Event(MouseButton.Middle_Up, 0, 0, 0, 0)
        Case Else : Mouse_Event(MouseButton, 0, 0, 0, 0)
    End Select
End Sub

' Thread used for reproduce the mouse actions
Private Shared Sub Thread_MousePlay()

    Click_Count = 0

    For Each Coordenate In Coordenates_List

        Threading.Thread.Sleep(Mouse_Speed)

        If Coordenate = Nothing Then
            Click_Count += 1
            If Click_Count > 1 Then Mouse_Click(Clicks_Dictionary.Item(Click_Count))
        Else
            Cursor.Position = Coordenate
        End If

        Application.DoEvents()

    Next

    Play_Is_Completed = True

End Sub

End Class

#End Region

解决方案

You can get the middle-mouse button with GetAsyncKeyState(4). But you'll never get mouse scroll messages with this approach, there isn't any way to ask for the scroll button position. You must rely on the Window message that tells you that the mouse was scrolled, WM_MOUSEWHEEL.

The requirement that a recording is made even if your own window doesn't have the focus requires a fundamentally different approach, you need to use a low-level hook. A hook set by SetWindowsHookEx(), it will call a method in your program when a mouse event occurs.

No point in posting the code for that, there are lots of samples available for that on the Internet. The proper Google query for it is "vb.net setwindowshookex wh_mouse_ll". The first hit is already very good, the Microsoft KB article has almost everything you need. The way it uses SetWindowsHookEx() does however need tweaking, as shown it will only record mouse events for your own program. Instead, use LoadLibrary("user32.dll") and pass the return value as the 3rd argument, 0 for the 4th argument to get it to record every mouse event.

这篇关于记录鼠标中键和滚轮滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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