Advertisement
PiToLoKo

Send Inputs

Jun 27th, 2014
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 34.78 KB | None | 0 0
  1. ' ***********************************************************************
  2. ' Author   : Elektro
  3. ' Modified : 25-05-2014
  4. ' ***********************************************************************
  5. ' <copyright file="SendInputs.vb" company="Elektro Studios">
  6. '     Copyright (c) Elektro Studios. All rights reserved.
  7. ' </copyright>
  8. ' ***********************************************************************
  9.  
  10. #Region " Usage Examples "
  11.  
  12. 'Private Sub Test() Handles Button1.Click
  13.  
  14. '    AppActivate(Process.GetProcessesByName("notepad").First.Id)
  15.  
  16. '    Dim c As Char = Convert.ToChar(Keys.Oemtilde) ' Ñ
  17. '    Dim Result As Integer = SendInputs.SendKey(key:=Convert.ToChar(c.ToString.ToLower), KeyAction:=SendInputs.KeyAction.Press)
  18. '    MessageBox.Show(String.Format("Successfull events: {0}", CStr(Result)))
  19.  
  20. '    SendInputs.SendKey(key:=Keys.Enter, KeyAction:=SendInputs.KeyAction.Press)
  21. '    SendInputs.SendKey(key:=Convert.ToChar(Keys.Back), KeyAction:=SendInputs.KeyAction.Press)
  22. '    SendInputs.SendKey(key:=Convert.ToChar(Keys.D0), KeyAction:=SendInputs.KeyAction.Press, BlockInput:=False)
  23.  
  24. '    SendInputs.SendKeys([String]:="Hello World", BlockInput:=True)
  25. '    SendInputs.SendKeys([String]:=Keys.Insert, BlockInput:=False)
  26.  
  27. '    SendInputs.MouseClick(MouseAction:=SendInputs.MouseButton.RightPress, BlockInput:=False)
  28.  
  29. '    SendInputs.MouseMove(X:=5, Y:=-5, BlockInput:=False)
  30. '    SendInputs.MouseMove(Offset:=New Point With {.X = 5, .Y = -5}, BlockInput:=False)
  31.  
  32. '    SendInputs.MousePosition(Position:=New Point With {.X = 100, .Y = -500}, BlockInput:=False)
  33.  
  34. 'End Sub
  35.  
  36. #End Region
  37.  
  38. #Region " Imports "
  39.  
  40. Imports System.Runtime.InteropServices
  41. Imports System.ComponentModel
  42.  
  43. #End Region
  44.  
  45. ''' <summary>
  46. ''' Synthesizes keystrokes, mouse motions, and button clicks.
  47. ''' </summary>
  48. Public Class SendInputs
  49.  
  50. #Region " P/Invoke "
  51.  
  52.     Friend Class NativeMethods
  53.  
  54. #Region " Methods "
  55.  
  56.         ''' <summary>
  57.         ''' Blocks keyboard and mouse input events from reaching applications.
  58.         ''' For more info see here:
  59.         ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646290%28v=vs.85%29.aspx
  60.         ''' </summary>
  61.         ''' <param name="fBlockIt">
  62.         ''' The function's purpose.
  63.         ''' If this parameter is 'TRUE', keyboard and mouse input events are blocked.
  64.         ''' If this parameter is 'FALSE', keyboard and mouse events are unblocked.
  65.         ''' </param>
  66.         ''' <returns>
  67.         ''' If the function succeeds, the return value is nonzero.
  68.         ''' If input is already blocked, the return value is zero.
  69.         ''' </returns>
  70.         ''' <remarks>
  71.         ''' Note that only the thread that blocked input can successfully unblock input.
  72.         ''' </remarks>
  73.         <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall,
  74.         SetLastError:=True)>
  75.         Friend Shared Function BlockInput(
  76.               ByVal fBlockIt As Boolean
  77.        ) As Integer
  78.         End Function
  79.  
  80.         ''' <summary>
  81.         ''' Synthesizes keystrokes, mouse motions, and button clicks.
  82.         ''' For more info see here:
  83.         ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx
  84.         ''' </summary>
  85.         ''' <param name="nInputs">
  86.         ''' Indicates the number of structures in the pInputs array.
  87.         ''' </param>
  88.         ''' <param name="pInputs">
  89.         ''' Indicates an Array of 'INPUT' structures.
  90.         ''' Each structure represents an event to be inserted into the keyboard or mouse input stream.
  91.         ''' </param>
  92.         ''' <param name="cbSize">
  93.         ''' The size, in bytes, of an 'INPUT' structure.
  94.         ''' If 'cbSize' is not the size of an 'INPUT' structure, the function fails.
  95.         ''' </param>
  96.         ''' <returns>
  97.         ''' The function returns the number of events that it successfully
  98.         ''' inserted into the keyboard or mouse input stream.
  99.         ''' If the function returns zero, the input was already blocked by another thread.
  100.         ''' </returns>
  101.         <DllImport("user32.dll", SetLastError:=True)>
  102.         Friend Shared Function SendInput(
  103.               ByVal nInputs As Integer,
  104.               <MarshalAs(UnmanagedType.LPArray), [In]> ByVal pInputs As INPUT(),
  105.               ByVal cbSize As Integer
  106.        ) As Integer
  107.         End Function
  108.  
  109. #End Region
  110.  
  111. #Region " Enumerations "
  112.  
  113.         ''' <summary>
  114.         ''' VirtualKey codes.
  115.         ''' </summary>
  116.         Friend Enum VirtualKeys As Short
  117.  
  118.             ''' <summary>
  119.             ''' The Shift key.
  120.             ''' VK_SHIFT
  121.             ''' </summary>
  122.             SHIFT = &H10S
  123.  
  124.             ''' <summary>
  125.             ''' The DEL key.
  126.             ''' VK_DELETE
  127.             ''' </summary>
  128.             DELETE = 46S
  129.  
  130.             ''' <summary>
  131.             ''' The ENTER key.
  132.             ''' VK_RETURN
  133.             ''' </summary>
  134.             [RETURN] = 13S
  135.  
  136.         End Enum
  137.  
  138.         ''' <summary>
  139.         ''' The type of the input event.
  140.         ''' For more info see here:
  141.         ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646270%28v=vs.85%29.aspx
  142.         ''' </summary>
  143.         <Description("Enumeration used for 'type' parameter of 'INPUT' structure")>
  144.         Friend Enum InputType As Integer
  145.  
  146.             ''' <summary>
  147.             ''' The event is a mouse event.
  148.             ''' Use the mi structure of the union.
  149.             ''' </summary>
  150.             Mouse = 0
  151.  
  152.             ''' <summary>
  153.             ''' The event is a keyboard event.
  154.             ''' Use the ki structure of the union.
  155.             ''' </summary>
  156.             Keyboard = 1
  157.  
  158.             ''' <summary>
  159.             ''' The event is a hardware event.
  160.             ''' Use the hi structure of the union.
  161.             ''' </summary>
  162.             Hardware = 2
  163.  
  164.         End Enum
  165.  
  166.         ''' <summary>
  167.         ''' Specifies various aspects of a keystroke.
  168.         ''' This member can be certain combinations of the following values.
  169.         ''' For more info see here:
  170.         ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646271%28v=vs.85%29.aspx
  171.         ''' </summary>
  172.         <Description("Enumeration used for 'dwFlags' parameter of 'KeyboardInput' structure")>
  173.         <Flags>
  174.         Friend Enum KeyboardInput_Flags As Integer
  175.  
  176.             ''' <summary>
  177.             ''' If specified, the scan code was preceded by a prefix byte that has the value '0xE0' (224).
  178.             ''' </summary>
  179.             ExtendedKey = &H1
  180.  
  181.             ''' <summary>
  182.             ''' If specified, the key is being pressed.
  183.             ''' </summary>
  184.             KeyDown = &H0
  185.  
  186.             ''' <summary>
  187.             ''' If specified, the key is being released.
  188.             ''' If not specified, the key is being pressed.
  189.             ''' </summary>
  190.             KeyUp = &H2
  191.  
  192.             ''' <summary>
  193.             ''' If specified, 'wScan' identifies the key and 'wVk' is ignored.
  194.             ''' </summary>
  195.             ScanCode = &H8
  196.  
  197.             ''' <summary>
  198.             ''' If specified, the system synthesizes a 'VK_PACKET' keystroke.
  199.             ''' The 'wVk' parameter must be '0'.
  200.             ''' This flag can only be combined with the 'KEYEVENTF_KEYUP' flag.
  201.             ''' </summary>
  202.             Unicode = &H4
  203.  
  204.         End Enum
  205.  
  206.         ''' <summary>
  207.         ''' A set of bit flags that specify various aspects of mouse motion and button clicks.
  208.         ''' The bits in this member can be any reasonable combination of the following values.
  209.         ''' For more info see here:
  210.         ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646273%28v=vs.85%29.aspx
  211.         ''' </summary>
  212.         <Description("Enumeration used for 'dwFlags' parameter of 'MouseInput' structure")>
  213.         <Flags>
  214.         Friend Enum MouseInput_Flags As Integer
  215.  
  216.             ''' <summary>
  217.             ''' The 'dx' and 'dy' members contain normalized absolute coordinates.
  218.             ''' If the flag is not set, 'dx' and 'dy' contain relative data
  219.             ''' (the change in position since the last reported position).
  220.             ''' This flag can be set, or not set,
  221.             ''' regardless of what kind of mouse or other pointing device, if any, is connected to the system.
  222.             ''' </summary>
  223.             Absolute = &H8000I
  224.  
  225.             ''' <summary>
  226.             ''' Movement occurred.
  227.             ''' </summary>
  228.             Move = &H1I
  229.  
  230.             ''' <summary>
  231.             ''' The 'WM_MOUSEMOVE' messages will not be coalesced.
  232.             ''' The default behavior is to coalesce 'WM_MOUSEMOVE' messages.
  233.             ''' </summary>
  234.             Move_NoCoalesce = &H2000I
  235.  
  236.             ''' <summary>
  237.             ''' The left button was pressed.
  238.             ''' </summary>
  239.             LeftDown = &H2I
  240.  
  241.             ''' <summary>
  242.             ''' The left button was released.
  243.             ''' </summary>
  244.             LeftUp = &H4I
  245.  
  246.             ''' <summary>
  247.             ''' The right button was pressed.
  248.             ''' </summary>
  249.             RightDown = &H8I
  250.  
  251.             ''' <summary>
  252.             ''' The right button was released.
  253.             ''' </summary>
  254.             RightUp = &H10I
  255.  
  256.             ''' <summary>
  257.             ''' The middle button was pressed.
  258.             ''' </summary>
  259.             MiddleDown = &H20I
  260.  
  261.             ''' <summary>
  262.             ''' The middle button was released.
  263.             ''' </summary>
  264.             MiddleUp = &H40I
  265.  
  266.             ''' <summary>
  267.             ''' Maps coordinates to the entire desktop.
  268.             ''' Must be used in combination with 'Absolute'.
  269.             ''' </summary>
  270.             VirtualDesk = &H4000I
  271.  
  272.             ''' <summary>
  273.             ''' The wheel was moved, if the mouse has a wheel.
  274.             ''' The amount of movement is specified in 'mouseData'.
  275.             ''' </summary>
  276.             Wheel = &H800I
  277.  
  278.             ''' <summary>
  279.             ''' The wheel was moved horizontally, if the mouse has a wheel.
  280.             ''' The amount of movement is specified in 'mouseData'.
  281.             ''' </summary>
  282.             HWheel = &H1000I
  283.  
  284.             ''' <summary>
  285.             ''' An X button was pressed.
  286.             ''' </summary>
  287.             XDown = &H80I
  288.  
  289.             ''' <summary>
  290.             ''' An X button was released.
  291.             ''' </summary>
  292.             XUp = &H100I
  293.  
  294.         End Enum
  295.  
  296. #End Region
  297.  
  298. #Region " Structures "
  299.  
  300.         ''' <summary>
  301.         ''' Used by 'SendInput' function
  302.         ''' to store information for synthesizing input events such as keystrokes, mouse movement, and mouse clicks.
  303.         ''' For more info see here:
  304.         ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646270%28v=vs.85%29.aspx
  305.         ''' </summary>
  306.         <Description("Structure used for 'INPUT' parameter of 'SendInput' API method")>
  307.         <StructLayout(LayoutKind.Explicit)>
  308.         Friend Structure Input
  309.  
  310.             ' ******
  311.             '  NOTE
  312.             ' ******
  313.             ' Field offset for 32 bit machine: 4
  314.             ' Field offset for 64 bit machine: 8
  315.  
  316.             ''' <summary>
  317.             ''' The type of the input event.
  318.             ''' </summary>
  319.             <FieldOffset(0)>
  320.             Public type As InputType
  321.  
  322.             ''' <summary>
  323.             ''' The information about a simulated mouse event.
  324.             ''' </summary>
  325.             <FieldOffset(8)>
  326.             Public mi As MouseInput
  327.  
  328.             ''' <summary>
  329.             ''' The information about a simulated keyboard event.
  330.             ''' </summary>
  331.             <FieldOffset(8)>
  332.             Public ki As KeyboardInput
  333.  
  334.             ''' <summary>
  335.             ''' The information about a simulated hardware event.
  336.             ''' </summary>
  337.             <FieldOffset(8)>
  338.             Public hi As HardwareInput
  339.  
  340.         End Structure
  341.  
  342.         ''' <summary>
  343.         ''' Contains information about a simulated mouse event.
  344.         ''' For more info see here:
  345.         ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646273%28v=vs.85%29.aspx
  346.         ''' </summary>
  347.         <Description("Structure used for 'mi' parameter of 'INPUT' structure")>
  348.         Friend Structure MouseInput
  349.  
  350.             ''' <summary>
  351.             ''' The absolute position of the mouse,
  352.             ''' or the amount of motion since the last mouse event was generated,
  353.             ''' depending on the value of the dwFlags member.
  354.             ''' Absolute data is specified as the 'x' coordinate of the mouse;
  355.             ''' relative data is specified as the number of pixels moved.
  356.             ''' </summary>
  357.             Public dx As Integer
  358.  
  359.             ''' <summary>
  360.             ''' The absolute position of the mouse,
  361.             ''' or the amount of motion since the last mouse event was generated,
  362.             ''' depending on the value of the dwFlags member.
  363.             ''' Absolute data is specified as the 'y' coordinate of the mouse;
  364.             ''' relative data is specified as the number of pixels moved.
  365.             ''' </summary>
  366.             Public dy As Integer
  367.  
  368.             ''' <summary>
  369.             ''' If 'dwFlags' contains 'MOUSEEVENTF_WHEEL',
  370.             ''' then 'mouseData' specifies the amount of wheel movement.
  371.             ''' A positive value indicates that the wheel was rotated forward, away from the user;
  372.             ''' a negative value indicates that the wheel was rotated backward, toward the user.
  373.             ''' One wheel click is defined as 'WHEEL_DELTA', which is '120'.
  374.             '''
  375.             ''' If 'dwFlags' does not contain 'MOUSEEVENTF_WHEEL', 'MOUSEEVENTF_XDOWN', or 'MOUSEEVENTF_XUP',
  376.             ''' then mouseData should be '0'.
  377.             ''' </summary>
  378.             Public mouseData As Integer
  379.  
  380.             ''' <summary>
  381.             ''' A set of bit flags that specify various aspects of mouse motion and button clicks.
  382.             ''' The bits in this member can be any reasonable combination of the following values.
  383.             ''' The bit flags that specify mouse button status are set to indicate changes in status,
  384.             ''' not ongoing conditions.
  385.             ''' For example, if the left mouse button is pressed and held down,
  386.             ''' 'MOUSEEVENTF_LEFTDOWN' is set when the left button is first pressed,
  387.             ''' but not for subsequent motions.
  388.             ''' Similarly, 'MOUSEEVENTF_LEFTUP' is set only when the button is first released.
  389.             '''
  390.             ''' You cannot specify both the 'MOUSEEVENTF_WHEE'L flag
  391.             ''' and either 'MOUSEEVENTF_XDOWN' or 'MOUSEEVENTF_XUP' flags simultaneously in the 'dwFlags' parameter,
  392.             ''' because they both require use of the 'mouseData' field.
  393.             ''' </summary>
  394.             Public dwFlags As MouseInput_Flags
  395.  
  396.             ''' <summary>
  397.             ''' The time stamp for the event, in milliseconds.
  398.             ''' If this parameter is '0', the system will provide its own time stamp.
  399.             ''' </summary>
  400.             Public time As Integer
  401.  
  402.             ''' <summary>
  403.             ''' An additional value associated with the mouse event.
  404.             ''' An application calls 'GetMessageExtraInfo' to obtain this extra information.
  405.             ''' </summary>
  406.             Public dwExtraInfo As IntPtr
  407.  
  408.         End Structure
  409.  
  410.         ''' <summary>
  411.         ''' Contains information about a simulated keyboard event.
  412.         ''' For more info see here:
  413.         ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646271%28v=vs.85%29.aspx
  414.         ''' </summary>
  415.         <Description("Structure used for 'ki' parameter of 'INPUT' structure")>
  416.         Friend Structure KeyboardInput
  417.  
  418.             ''' <summary>
  419.             ''' A virtual-key code.
  420.             ''' The code must be a value in the range '1' to '254'.
  421.             ''' If the 'dwFlags' member specifies 'KEYEVENTF_UNICODE', wVk must be '0'.
  422.             ''' </summary>
  423.             Public wVk As Short
  424.  
  425.             ''' <summary>
  426.             ''' A hardware scan code for the key.
  427.             ''' If 'dwFlags' specifies 'KEYEVENTF_UNICODE',
  428.             ''' 'wScan' specifies a Unicode character which is to be sent to the foreground application.
  429.             ''' </summary>
  430.             Public wScan As Short
  431.  
  432.             ''' <summary>
  433.             ''' Specifies various aspects of a keystroke.
  434.             ''' </summary>
  435.             Public dwFlags As KeyboardInput_Flags
  436.  
  437.             ''' <summary>
  438.             ''' The time stamp for the event, in milliseconds.
  439.             ''' If this parameter is '0', the system will provide its own time stamp.
  440.             ''' </summary>
  441.             Public time As Integer
  442.  
  443.             ''' <summary>
  444.             ''' An additional value associated with the keystroke.
  445.             ''' Use the 'GetMessageExtraInfo' function to obtain this information.
  446.             ''' </summary>
  447.             Public dwExtraInfo As IntPtr
  448.  
  449.         End Structure
  450.  
  451.         ''' <summary>
  452.         ''' Contains information about a simulated message generated by an input device other than a keyboard or mouse.
  453.         ''' For more info see here:
  454.         ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646269%28v=vs.85%29.aspx
  455.         ''' </summary>
  456.         <Description("Structure used for 'hi' parameter of 'INPUT' structure")>
  457.         Friend Structure HardwareInput
  458.  
  459.             ''' <summary>
  460.             ''' The message generated by the input hardware.
  461.             ''' </summary>
  462.             Public uMsg As Integer
  463.  
  464.             ''' <summary>
  465.             ''' The low-order word of the lParam parameter for uMsg.
  466.             ''' </summary>
  467.             Public wParamL As Short
  468.  
  469.             ''' <summary>
  470.             ''' The high-order word of the lParam parameter for uMsg.
  471.             ''' </summary>
  472.             Public wParamH As Short
  473.  
  474.         End Structure
  475.  
  476. #End Region
  477.  
  478.     End Class
  479.  
  480. #End Region
  481.  
  482. #Region " Enumerations "
  483.  
  484.     ''' <summary>
  485.     ''' Indicates a mouse button.
  486.     ''' </summary>
  487.     <Description("Enumeration used for 'MouseAction' parameter of 'MouseClick' function.")>
  488.     Public Enum MouseButton As Integer
  489.  
  490.         ''' <summary>
  491.         ''' Hold the left button.
  492.         ''' </summary>
  493.         LeftDown = &H2I
  494.  
  495.         ''' <summary>
  496.         ''' Release the left button.
  497.         ''' </summary>
  498.         LeftUp = &H4I
  499.  
  500.         ''' <summary>
  501.         ''' Hold the right button.
  502.         ''' </summary>
  503.         RightDown = &H8I
  504.  
  505.         ''' <summary>
  506.         ''' Release the right button.
  507.         ''' </summary>
  508.         RightUp = &H10I
  509.  
  510.         ''' <summary>
  511.         ''' Hold the middle button.
  512.         ''' </summary>
  513.         MiddleDown = &H20I
  514.  
  515.         ''' <summary>
  516.         ''' Release the middle button.
  517.         ''' </summary>
  518.         MiddleUp = &H40I
  519.  
  520.         ''' <summary>
  521.         ''' Press the left button.
  522.         ''' ( Hold + Release )
  523.         ''' </summary>
  524.         LeftPress = LeftDown + LeftUp
  525.  
  526.         ''' <summary>
  527.         ''' Press the Right button.
  528.         ''' ( Hold + Release )
  529.         ''' </summary>
  530.         RightPress = RightDown + RightUp
  531.  
  532.         ''' <summary>
  533.         ''' Press the Middle button.
  534.         ''' ( Hold + Release )
  535.         ''' </summary>
  536.         MiddlePress = MiddleDown + MiddleUp
  537.  
  538.     End Enum
  539.  
  540.     ''' <summary>
  541.     ''' Indicates a keyboard key action.
  542.     ''' </summary>
  543.     Public Enum KeyAction As Integer
  544.  
  545.         ''' <summary>
  546.         ''' Holds the key.
  547.         ''' </summary>
  548.         Hold = &H0
  549.  
  550.         ''' <summary>
  551.         ''' Releases the key.
  552.         ''' </summary>
  553.  
  554.         Release = &H2
  555.  
  556.         ''' <summary>
  557.         ''' Press he key.
  558.         ''' </summary>
  559.         Press = Hold Or Release
  560.  
  561.     End Enum
  562.  
  563. #End Region
  564.  
  565. #Region " Public Methods "
  566.  
  567.     ''' <summary>
  568.     ''' Sends a keystroke.
  569.     ''' </summary>
  570.     ''' <param name="key">
  571.     ''' Indicates the keystroke to simulate.
  572.     ''' </param>
  573.     ''' <param name="BlockInput">
  574.     ''' If set to <c>true</c>, the keyboard and mouse are blocked until the keystroke is sent.
  575.     ''' </param>
  576.     ''' <returns>
  577.     ''' The function returns the number of events that it successfully inserted into the keyboard input stream.
  578.     ''' If the function returns zero, the input was already blocked by another thread.
  579.     ''' </returns>
  580.     Public Shared Function SendKey(ByVal key As Char,
  581.                                    ByVal KeyAction As KeyAction,
  582.                                    Optional BlockInput As Boolean = False) As Integer
  583.  
  584.         ' Block Keyboard and mouse.
  585.         If BlockInput Then NativeMethods.BlockInput(True)
  586.  
  587.         ' The inputs structures to send.
  588.         Dim Inputs As New List(Of NativeMethods.Input)
  589.  
  590.         ' The current input to add into the Inputs list.
  591.         Dim CurrentInput As New NativeMethods.Input
  592.  
  593.         ' Determines whether a character is an alphabetic letter.
  594.         Dim IsAlphabetic As Boolean = Not (key.ToString.ToUpper = key.ToString.ToLower)
  595.  
  596.         ' Determines whether a character is an uppercase alphabetic letter.
  597.         Dim IsUpperCase As Boolean =
  598.             (key.ToString = key.ToString.ToUpper) AndAlso Not (key.ToString.ToUpper = key.ToString.ToLower)
  599.  
  600.         ' Determines whether the CapsLock key is pressed down.
  601.         Dim CapsLockON As Boolean = My.Computer.Keyboard.CapsLock
  602.  
  603.         ' Set the passed key to upper-case.
  604.         If IsAlphabetic AndAlso Not IsUpperCase Then
  605.             key = Convert.ToChar(key.ToString.ToUpper)
  606.         End If
  607.  
  608.         ' If character is alphabetic and is UpperCase and CapsLock is pressed down,
  609.         ' OrElse character is alphabetic and is not UpperCase and CapsLock is not pressed down,
  610.         ' OrElse character is not alphabetic.
  611.         If (IsAlphabetic AndAlso IsUpperCase AndAlso CapsLockON) _
  612.         OrElse (IsAlphabetic AndAlso Not IsUpperCase AndAlso Not CapsLockON) _
  613.         OrElse (Not IsAlphabetic) Then
  614.  
  615.             If KeyAction = SendInputs.KeyAction.Press Then
  616.  
  617.                 ' Hold the character key.
  618.                 With CurrentInput
  619.                     .type = NativeMethods.InputType.Keyboard
  620.                     .ki.wVk = Convert.ToInt16(CChar(key))
  621.                     .ki.dwFlags = NativeMethods.KeyboardInput_Flags.KeyDown
  622.                 End With : Inputs.Add(CurrentInput)
  623.  
  624.                 ' Release the character key.
  625.                 With CurrentInput
  626.                     .type = NativeMethods.InputType.Keyboard
  627.                     .ki.wVk = Convert.ToInt16(CChar(key))
  628.                     .ki.dwFlags = NativeMethods.KeyboardInput_Flags.KeyUp
  629.                 End With : Inputs.Add(CurrentInput)
  630.  
  631.             Else
  632.  
  633.                 With CurrentInput
  634.                     .type = NativeMethods.InputType.Keyboard
  635.                     .ki.wVk = Convert.ToInt16(CChar(key))
  636.                     .ki.dwFlags = KeyAction
  637.                 End With : Inputs.Add(CurrentInput)
  638.  
  639.             End If
  640.  
  641.             ' If character is alphabetic and is UpperCase and CapsLock is not pressed down,
  642.             ' OrElse character is alphabetic and is not UpperCase and CapsLock is pressed down.
  643.         ElseIf (IsAlphabetic AndAlso IsUpperCase AndAlso Not CapsLockON) _
  644.         OrElse (IsAlphabetic AndAlso Not IsUpperCase AndAlso CapsLockON) Then
  645.  
  646.             ' Hold the Shift key.
  647.             With CurrentInput
  648.                 .type = NativeMethods.InputType.Keyboard
  649.                 .ki.wVk = NativeMethods.VirtualKeys.SHIFT
  650.                 .ki.dwFlags = NativeMethods.KeyboardInput_Flags.KeyDown
  651.             End With : Inputs.Add(CurrentInput)
  652.  
  653.             If KeyAction = SendInputs.KeyAction.Press Then
  654.  
  655.                 ' Hold the character key.
  656.                 With CurrentInput
  657.                     .type = NativeMethods.InputType.Keyboard
  658.                     .ki.wVk = Convert.ToInt16(CChar(key))
  659.                     .ki.dwFlags = NativeMethods.KeyboardInput_Flags.KeyDown
  660.                 End With : Inputs.Add(CurrentInput)
  661.  
  662.                 ' Release the character key.
  663.                 With CurrentInput
  664.                     .type = NativeMethods.InputType.Keyboard
  665.                     .ki.wVk = Convert.ToInt16(CChar(key))
  666.                     .ki.dwFlags = NativeMethods.KeyboardInput_Flags.KeyUp
  667.                 End With : Inputs.Add(CurrentInput)
  668.  
  669.             Else
  670.  
  671.                 With CurrentInput
  672.                     .type = NativeMethods.InputType.Keyboard
  673.                     .ki.wVk = Convert.ToInt16(CChar(key))
  674.                     .ki.dwFlags = KeyAction
  675.                 End With : Inputs.Add(CurrentInput)
  676.  
  677.             End If
  678.  
  679.             ' Release the Shift key.
  680.             With CurrentInput
  681.                 .type = NativeMethods.InputType.Keyboard
  682.                 .ki.wVk = NativeMethods.VirtualKeys.SHIFT
  683.                 .ki.dwFlags = NativeMethods.KeyboardInput_Flags.KeyUp
  684.             End With : Inputs.Add(CurrentInput)
  685.  
  686.         End If ' UpperCase And My.Computer.Keyboard.CapsLock is...
  687.  
  688.         ' Send the input key.
  689.         Return NativeMethods.SendInput(Inputs.Count, Inputs.ToArray,
  690.                                        Marshal.SizeOf(GetType(NativeMethods.Input)))
  691.  
  692.         ' Unblock Keyboard and mouse.
  693.         If BlockInput Then NativeMethods.BlockInput(False)
  694.  
  695.     End Function
  696.  
  697.     ''' <summary>
  698.     ''' Sends a keystroke.
  699.     ''' </summary>
  700.     ''' <param name="key">
  701.     ''' Indicates the keystroke to simulate.
  702.     ''' </param>
  703.     ''' <param name="BlockInput">
  704.     ''' If set to <c>true</c>, the keyboard and mouse are blocked until the keystroke is sent.
  705.     ''' </param>
  706.     ''' <returns>
  707.     ''' The function returns the number of events that it successfully inserted into the keyboard input stream.
  708.     ''' If the function returns zero, the input was already blocked by another thread.
  709.     ''' </returns>
  710.     Public Shared Function SendKey(ByVal key As Keys,
  711.                                    ByVal KeyAction As KeyAction,
  712.                                    Optional BlockInput As Boolean = False) As Integer
  713.  
  714.         Return SendKey(Convert.ToChar(key), KeyAction, BlockInput)
  715.  
  716.     End Function
  717.  
  718.     ''' <summary>
  719.     ''' Sends a string.
  720.     ''' </summary>
  721.     ''' <param name="String">
  722.     ''' Indicates the string to send.
  723.     ''' </param>
  724.     ''' <param name="BlockInput">
  725.     ''' If set to <c>true</c>, the keyboard and mouse are blocked until the keystroke is sent.
  726.     ''' </param>
  727.     ''' <returns>
  728.     ''' The function returns the number of events that it successfully inserted into the keyboard input stream.
  729.     ''' If the function returns zero, the input was already blocked by another thread.
  730.     ''' </returns>
  731.     Public Shared Function SendKeys(ByVal [String] As String,
  732.                                     Optional BlockInput As Boolean = False) As Integer
  733.  
  734.         Dim SuccessCount As Integer = 0
  735.  
  736.         ' Block Keyboard and mouse.
  737.         If BlockInput Then NativeMethods.BlockInput(True)
  738.  
  739.         For Each c As Char In [String]
  740.             SuccessCount += SendKey(c, BlockInput:=False, KeyAction:=KeyAction.Press)
  741.         Next c
  742.  
  743.         ' Unblock Keyboard and mouse.
  744.         If BlockInput Then NativeMethods.BlockInput(False)
  745.  
  746.         Return SuccessCount
  747.  
  748.     End Function
  749.  
  750.     ''' <summary>
  751.     ''' Slices the mouse position.
  752.     ''' </summary>
  753.     ''' <param name="Offset">
  754.     ''' Indicates the offset, in coordinates.
  755.     ''' </param>
  756.     ''' <param name="BlockInput">
  757.     ''' If set to <c>true</c>, the keyboard and mouse are blocked until the mouse movement is sent.
  758.     ''' </param>
  759.     ''' <returns>
  760.     ''' The function returns the number of events that it successfully inserted into the mouse input stream.
  761.     ''' If the function returns zero, the input was already blocked by another thread.
  762.     ''' </returns>
  763.     Public Shared Function MouseMove(ByVal Offset As Point,
  764.                                      Optional BlockInput As Boolean = False) As Integer
  765.  
  766.         ' Block Keyboard and mouse.
  767.         If BlockInput Then NativeMethods.BlockInput(True)
  768.  
  769.         ' The inputs structures to send.
  770.         Dim Inputs As New List(Of NativeMethods.Input)
  771.  
  772.         ' The current input to add into the Inputs list.
  773.         Dim CurrentInput As New NativeMethods.Input
  774.  
  775.         ' Add a mouse movement.
  776.         With CurrentInput
  777.             .type = NativeMethods.InputType.Mouse
  778.             .mi.dx = Offset.X
  779.             .mi.dy = Offset.Y
  780.             .mi.dwFlags = NativeMethods.MouseInput_Flags.Move
  781.         End With : Inputs.Add(CurrentInput)
  782.  
  783.         ' Send the mouse movement.
  784.         Return NativeMethods.SendInput(Inputs.Count, Inputs.ToArray,
  785.                                        Marshal.SizeOf(GetType(NativeMethods.Input)))
  786.  
  787.         ' Unblock Keyboard and mouse.
  788.         If BlockInput Then NativeMethods.BlockInput(False)
  789.  
  790.     End Function
  791.  
  792.     ''' <summary>
  793.     ''' Slices the mouse position.
  794.     ''' </summary>
  795.     ''' <param name="X">
  796.     ''' Indicates the 'X' offset.
  797.     ''' </param>
  798.     ''' <param name="Y">
  799.     ''' Indicates the 'Y' offset.
  800.     ''' </param>
  801.     ''' <param name="BlockInput">
  802.     ''' If set to <c>true</c>, the keyboard and mouse are blocked until the mouse movement is sent.
  803.     ''' </param>
  804.     ''' <returns>
  805.     ''' The function returns the number of events that it successfully inserted into the mouse input stream.
  806.     ''' If the function returns zero, the input was already blocked by another thread.
  807.     ''' </returns>
  808.     Public Shared Function MouseMove(ByVal X As Integer, ByVal Y As Integer,
  809.                                      Optional BlockInput As Boolean = False) As Integer
  810.  
  811.         Return MouseMove(New Point(X, Y), BlockInput)
  812.  
  813.     End Function
  814.  
  815.     ''' <summary>
  816.     ''' Moves the mouse hotspot to an absolute position, in coordinates.
  817.     ''' </summary>
  818.     ''' <param name="Position">
  819.     ''' Indicates the absolute position.
  820.     ''' </param>
  821.     ''' <param name="BlockInput">
  822.     ''' If set to <c>true</c>, the keyboard and mouse are blocked until the mouse movement is sent.
  823.     ''' </param>
  824.     ''' <returns>
  825.     ''' The function returns the number of events that it successfully inserted into the mouse input stream.
  826.     ''' If the function returns zero, the input was already blocked by another thread.
  827.     ''' </returns>
  828.     Public Shared Function MousePosition(ByVal Position As Point,
  829.                                          Optional BlockInput As Boolean = False) As Integer
  830.  
  831.         ' Block Keyboard and mouse.
  832.         If BlockInput Then NativeMethods.BlockInput(True)
  833.  
  834.         ' The inputs structures to send.
  835.         Dim Inputs As New List(Of NativeMethods.Input)
  836.  
  837.         ' The current input to add into the Inputs list.
  838.         Dim CurrentInput As New NativeMethods.Input
  839.  
  840.         ' Transform the coordinates.
  841.         Position.X = CInt(Position.X * 65535 / (Screen.PrimaryScreen.Bounds.Width - 1))
  842.         Position.Y = CInt(Position.Y * 65535 / (Screen.PrimaryScreen.Bounds.Height - 1))
  843.  
  844.         ' Add an absolute mouse movement.
  845.         With CurrentInput
  846.             .type = NativeMethods.InputType.Mouse
  847.             .mi.dx = Position.X
  848.             .mi.dy = Position.Y
  849.             .mi.dwFlags = NativeMethods.MouseInput_Flags.Absolute Or NativeMethods.MouseInput_Flags.Move
  850.             .mi.time = 0
  851.         End With : Inputs.Add(CurrentInput)
  852.  
  853.         ' Send the absolute mouse movement.
  854.         Return NativeMethods.SendInput(Inputs.Count, Inputs.ToArray,
  855.                                        Marshal.SizeOf(GetType(NativeMethods.Input)))
  856.  
  857.         ' Unblock Keyboard and mouse.
  858.         If BlockInput Then NativeMethods.BlockInput(False)
  859.  
  860.     End Function
  861.  
  862.     ''' <summary>
  863.     ''' Moves the mouse hotspot to an absolute position, in coordinates.
  864.     ''' </summary>
  865.     ''' <param name="X">
  866.     ''' Indicates the absolute 'X' coordinate.
  867.     ''' </param>
  868.     ''' <param name="Y">
  869.     ''' Indicates the absolute 'Y' coordinate.
  870.     ''' </param>
  871.     ''' <param name="BlockInput">
  872.     ''' If set to <c>true</c>, the keyboard and mouse are blocked until the mouse movement is sent.
  873.     ''' </param>
  874.     ''' <returns>
  875.     ''' The function returns the number of events that it successfully inserted into the mouse input stream.
  876.     ''' If the function returns zero, the input was already blocked by another thread.
  877.     ''' </returns>
  878.     Public Shared Function MousePosition(ByVal X As Integer, ByVal Y As Integer,
  879.                                          Optional BlockInput As Boolean = False) As Integer
  880.  
  881.         Return MousePosition(New Point(X, Y), BlockInput)
  882.  
  883.     End Function
  884.  
  885.     ''' <summary>
  886.     ''' Simulates a mouse click.
  887.     ''' </summary>
  888.     ''' <param name="MouseAction">
  889.     ''' Indicates the mouse action to perform.
  890.     ''' </param>
  891.     ''' <param name="BlockInput">
  892.     ''' If set to <c>true</c>, the keyboard and mouse are blocked until the mouse movement is sent.
  893.     ''' </param>
  894.     ''' <returns>
  895.     ''' The function returns the number of events that it successfully inserted into the mouse input stream.
  896.     ''' If the function returns zero, the input was already blocked by another thread.
  897.     ''' </returns>
  898.     Public Shared Function MouseClick(ByVal MouseAction As MouseButton,
  899.                                       Optional BlockInput As Boolean = False) As Integer
  900.  
  901.         ' Block Keyboard and mouse.
  902.         If BlockInput Then NativeMethods.BlockInput(True)
  903.  
  904.         ' The inputs structures to send.
  905.         Dim Inputs As New List(Of NativeMethods.Input)
  906.  
  907.         ' The current input to add into the Inputs list.
  908.         Dim CurrentInput As New NativeMethods.Input
  909.  
  910.         ' The mouse actions to perform.
  911.         Dim MouseActions As New List(Of MouseButton)
  912.  
  913.         Select Case MouseAction
  914.  
  915.             Case MouseButton.LeftPress ' Left button, hold and release.
  916.                 MouseActions.Add(MouseButton.LeftDown)
  917.                 MouseActions.Add(MouseButton.LeftUp)
  918.  
  919.             Case MouseButton.RightPress ' Right button, hold and release.
  920.                 MouseActions.Add(MouseButton.RightDown)
  921.                 MouseActions.Add(MouseButton.RightUp)
  922.  
  923.             Case MouseButton.MiddlePress ' Middle button, hold and release.
  924.                 MouseActions.Add(MouseButton.MiddleDown)
  925.                 MouseActions.Add(MouseButton.MiddleUp)
  926.  
  927.             Case Else ' Other
  928.                 MouseActions.Add(MouseAction)
  929.  
  930.         End Select ' MouseAction
  931.  
  932.         For Each Action As MouseButton In MouseActions
  933.  
  934.             ' Add the mouse click.
  935.             With CurrentInput
  936.                 .type = NativeMethods.InputType.Mouse
  937.                 '.mi.dx = Offset.X
  938.                 '.mi.dy = Offset.Y
  939.                 .mi.dwFlags = Action
  940.             End With : Inputs.Add(CurrentInput)
  941.  
  942.         Next Action
  943.  
  944.         ' Send the mouse click.
  945.         Return NativeMethods.SendInput(Inputs.Count, Inputs.ToArray,
  946.                                        Marshal.SizeOf(GetType(NativeMethods.Input)))
  947.  
  948.         ' Unblock Keyboard and mouse.
  949.         If BlockInput Then NativeMethods.BlockInput(False)
  950.  
  951.     End Function
  952.  
  953. #End Region
  954.  
  955. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement