Vb.net 使用sendmessage模拟按键

Vb.net 使用sendmessage模拟按键,vb.net,Vb.net,我想要一个按键,但我有个小问题。。我用十六进制做什么。。。它甚至不起作用:这里是我得到十六进制虚拟键的地方: 这是我的密码: <DllImport("user32.dll", CharSet:=CharSet.Auto)> _ Private Shared Function SendMessage2( _ ByVal hwnd As IntPtr, _ ByVal wMsg As Integer, _ ByVal wParam As Integer, _ &l

我想要一个按键,但我有个小问题。。我用十六进制做什么。。。它甚至不起作用:这里是我得到十六进制虚拟键的地方:

这是我的密码:

  <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
  Private Shared Function SendMessage2( _
  ByVal hwnd As IntPtr, _
  ByVal wMsg As Integer, _
  ByVal wParam As Integer, _
  <MarshalAs(UnmanagedType.LPTStr)> ByVal lParam As String) As Integer
  End Function

SendMessage2(WindowHandle, 256, 0x0D, 0)
SendMessage2(WindowHandle, 257, 0x0D, 65539)

因为传递的是十六进制,所以在VB.NET中,每个十六进制都以
&H
开头,然后是
0x

因此,您的
SendMessage2(WindowHandle,256,0x0D,0)
需要是
SendMessage2(WindowHandle,256,&H0D,0)


十六进制是要发送的键。通过更改,您可以更改发送到窗口的键。

合成击键的另一种方法是使用该方法,我创建了一个帮助器类,以简单的方式发送击键(需要扩展它,以便使用特殊字符作为字符串,如ñ或Ç)

示例用法:

AppActivate(Process.GetProcessesByName("notepad").First.Id)

Dim c As Char = Convert.ToChar(Keys.Oemtilde) ' Ñ
Dim Result As Integer = SendInputs.SendKey(Convert.ToChar(c.ToString.ToLower))
MessageBox.Show(String.Format("Successfull events: {0}", CStr(Result)))

SendInputs.SendKey(Keys.Enter)
SendInputs.SendKey(Convert.ToChar(Keys.Back))
SendInputs.SendKeys("Hello World", True)
SendInputs.SendKey(Convert.ToChar(Keys.D0))
SendInputs.SendKeys(Keys.Insert, BlockInput:=True)

SendInputs.MouseClick(SendInputs.MouseButton.RightPress, False)
SendInputs.MouseMove(5, -5)
SendInputs.MousePosition(New Point(100, 500))
这里是我删除了MouseInput方法的部分类,如果您想查看完整类的话

'***********************************************************************
作者:Elektro
修改日期:2014年2月21日
' ***********************************************************************
' 
版权所有(c)Elektro工作室。版权所有。
' 
' ***********************************************************************
#区域“进口”
导入System.Runtime.InteropServices
导入System.ComponentModel
#末端区域
''' 
''合成击键、鼠标移动和按钮点击。
''' 
公共类输入
#区域“P/调用”
Friend类NativeMethods
#区域“方法”
''' 
''阻止键盘和鼠标输入事件到达应用程序。
''有关更多信息,请参见此处:
''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646290%28v=vs.85%29.aspx
''' 
''' 
''函数的目的。
''如果此参数为'TRUE',键盘和鼠标输入事件将被阻止。
''如果此参数为'FALSE',键盘和鼠标事件将被解除阻止。
''' 
''' 
''如果函数成功,则返回值为非零。
''如果输入已被阻止,则返回值为零。
''' 
''' 
''请注意,只有阻止输入的线程才能成功取消阻止输入。
''' 
朋友共享功能块输入(
ByVal fBlockIt作为布尔值
)作为整数
端函数
''' 
''合成击键、鼠标移动和按钮点击。
''有关更多信息,请参见此处:
''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx
''' 
''' 
''表示pInputs数组中的结构数。
''' 
''' 
''表示“输入”结构的数组。
''每个结构表示要插入键盘或鼠标输入流的事件。
''' 
''' 
''“输入”结构的大小,以字节为单位。
''如果'cbSize'不是'INPUT'结构的大小,则函数失败。
''' 
''' 
''函数返回它成功完成的事件数
''插入键盘或鼠标输入流。
''如果函数返回零,则输入已被另一个线程阻止。
''' 
好友共享函数SendInput(
ByVal作为整数,
ByVal pInputs作为输入(),
ByVal cbSize为整数
)作为整数
端函数
#末端区域
#区域“枚举”
''' 
''VirtualKey代码。
''' 
Friend Enum VirtualKeys简称
''' 
''换档键。
''VK_移位
''' 
移位=&H10S
''' 
''德尔钥匙。
''VK_删除
''' 
删除=46S
''' 
''输入键。
''VK_返回
''' 
[返回]=13S
结束枚举
''' 
''输入事件的类型。
''有关更多信息,请参见此处:
''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646270%28v=vs.85%29.aspx
''' 
朋友枚举InputType为整数
''' 
''该事件是一个键盘事件。
''使用工会的ki结构。
''' 
键盘=1
结束枚举
''' 
''指定击键的各个方面。
''此成员可以是以下值的某些组合。
''有关更多信息,请参见此处:
''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646271%28v=vs.85%29.aspx
''' 
朋友枚举键盘输入_标志为整数
''' 
''如果指定,扫描代码前面会有一个前缀字节,该字节的值为'0xE0'(224)。
''' 
扩展键=&H1
''' 
''如果指定,则按键正在按下。
''' 
KeyDown=&H0
''' 
''如果指定,则释放密钥。
''如果未指定,则表示正在按下该键。
''' 
KeyUp=&H2
''' 
''如果指定,'wScan'标识密钥,忽略'wVk'。
''' 
扫描码=&H8
''' 
''如果指定,系统将合成“VK_数据包”击键。
''wVk'参数必须为'0'。
''此标志只能与'KEYEVENTF_KEYUP'标志组合使用。
''' 
Unicode=&H4
结束枚举
#末端区域
#区域“结构”
''' 
“SendInput”函数使用的“”
''存储用于合成输入事件的信息,如击键、鼠标移动和鼠标单击。
''有关更多信息,请参见此处:
''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646270%28v=vs.85%29.aspx
'''
AppActivate(Process.GetProcessesByName("notepad").First.Id)

Dim c As Char = Convert.ToChar(Keys.Oemtilde) ' Ñ
Dim Result As Integer = SendInputs.SendKey(Convert.ToChar(c.ToString.ToLower))
MessageBox.Show(String.Format("Successfull events: {0}", CStr(Result)))

SendInputs.SendKey(Keys.Enter)
SendInputs.SendKey(Convert.ToChar(Keys.Back))
SendInputs.SendKeys("Hello World", True)
SendInputs.SendKey(Convert.ToChar(Keys.D0))
SendInputs.SendKeys(Keys.Insert, BlockInput:=True)

SendInputs.MouseClick(SendInputs.MouseButton.RightPress, False)
SendInputs.MouseMove(5, -5)
SendInputs.MousePosition(New Point(100, 500))
' ***********************************************************************
' Author   : Elektro
' Modified : 02-21-2014
' ***********************************************************************
' <copyright file="SendInputs.vb" company="Elektro Studios">
'     Copyright (c) Elektro Studios. All rights reserved.
' </copyright>
' ***********************************************************************

#Region " Imports "

Imports System.Runtime.InteropServices
Imports System.ComponentModel

#End Region

''' <summary>
''' Synthesizes keystrokes, mouse motions, and button clicks.
''' </summary>
Public Class SendInputs

#Region " P/Invoke "

   Friend Class NativeMethods

#Region " Methods "

       ''' <summary>
       ''' Blocks keyboard and mouse input events from reaching applications.
       ''' For more info see here:
       ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646290%28v=vs.85%29.aspx
       ''' </summary>
       ''' <param name="fBlockIt">
       ''' The function's purpose.
       ''' If this parameter is 'TRUE', keyboard and mouse input events are blocked.
       ''' If this parameter is 'FALSE', keyboard and mouse events are unblocked.
       ''' </param>
       ''' <returns>
       ''' If the function succeeds, the return value is nonzero.
       ''' If input is already blocked, the return value is zero.
       ''' </returns>
       ''' <remarks>
       ''' Note that only the thread that blocked input can successfully unblock input.
       ''' </remarks>
       <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall,
       SetLastError:=True)>
       Friend Shared Function BlockInput(
              ByVal fBlockIt As Boolean
       ) As Integer
       End Function

       ''' <summary>
       ''' Synthesizes keystrokes, mouse motions, and button clicks.
       ''' For more info see here:
       ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx
       ''' </summary>
       ''' <param name="nInputs">
       ''' Indicates the number of structures in the pInputs array.
       ''' </param>
       ''' <param name="pInputs">
       ''' Indicates an Array of 'INPUT' structures.
       ''' Each structure represents an event to be inserted into the keyboard or mouse input stream.
       ''' </param>
       ''' <param name="cbSize">
       ''' The size, in bytes, of an 'INPUT' structure.
       ''' If 'cbSize' is not the size of an 'INPUT' structure, the function fails.
       ''' </param>
       ''' <returns>
       ''' The function returns the number of events that it successfully
       ''' inserted into the keyboard or mouse input stream.
       ''' If the function returns zero, the input was already blocked by another thread.
       ''' </returns>
       <DllImport("user32.dll", SetLastError:=True)>
       Friend Shared Function SendInput(
              ByVal nInputs As Integer,
              <MarshalAs(UnmanagedType.LPArray), [In]> ByVal pInputs As INPUT(),
              ByVal cbSize As Integer
       ) As Integer
       End Function

#End Region

#Region " Enumerations "

       ''' <summary>
       ''' VirtualKey codes.
       ''' </summary>
       Friend Enum VirtualKeys As Short

           ''' <summary>
           ''' The Shift key.
           ''' VK_SHIFT
           ''' </summary>
           SHIFT = &H10S

           ''' <summary>
           ''' The DEL key.
           ''' VK_DELETE
           ''' </summary>
           DELETE = 46S

           ''' <summary>
           ''' The ENTER key.
           ''' VK_RETURN
           ''' </summary>
           [RETURN] = 13S

       End Enum

       ''' <summary>
       ''' The type of the input event.
       ''' For more info see here:
       ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646270%28v=vs.85%29.aspx
       ''' </summary>
       <Description("Enumeration used for 'type' parameter of 'INPUT' structure")>
       Friend Enum InputType As Integer

           ''' <summary>
           ''' The event is a keyboard event.
           ''' Use the ki structure of the union.
           ''' </summary>
           Keyboard = 1

       End Enum

       ''' <summary>
       ''' Specifies various aspects of a keystroke.
       ''' This member can be certain combinations of the following values.
       ''' For more info see here:
       ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646271%28v=vs.85%29.aspx
       ''' </summary>
       <Description("Enumeration used for 'dwFlags' parameter of 'KeyboardInput' structure")>
       <Flags>
       Friend Enum KeyboardInput_Flags As Integer

           ''' <summary>
           ''' If specified, the scan code was preceded by a prefix byte that has the value '0xE0' (224).
           ''' </summary>
           ExtendedKey = &H1

           ''' <summary>
           ''' If specified, the key is being pressed.
           ''' </summary>
           KeyDown = &H0

           ''' <summary>
           ''' If specified, the key is being released.
           ''' If not specified, the key is being pressed.
           ''' </summary>
           KeyUp = &H2

           ''' <summary>
           ''' If specified, 'wScan' identifies the key and 'wVk' is ignored.
           ''' </summary>
           ScanCode = &H8

           ''' <summary>
           ''' If specified, the system synthesizes a 'VK_PACKET' keystroke.
           ''' The 'wVk' parameter must be '0'.
           ''' This flag can only be combined with the 'KEYEVENTF_KEYUP' flag.
           ''' </summary>
           Unicode = &H4

       End Enum

#End Region

#Region " Structures "

       ''' <summary>
       ''' Used by 'SendInput' function
       ''' to store information for synthesizing input events such as keystrokes, mouse movement, and mouse clicks.
       ''' For more info see here:
       ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646270%28v=vs.85%29.aspx
       ''' </summary>
       <Description("Structure used for 'INPUT' parameter of 'SendInput' API method")>
       <StructLayout(LayoutKind.Explicit)>
       Friend Structure Input

           ' ******
           '  NOTE
           ' ******
           ' Field offset for 32 bit machine: 4
           ' Field offset for 64 bit machine: 8

           ''' <summary>
           ''' The type of the input event.
           ''' </summary>
           <FieldOffset(0)>
           Public type As InputType

           ''' <summary>
           ''' The information about a simulated mouse event.
           ''' </summary>
           <FieldOffset(8)>
           Public mi As MouseInput

           ''' <summary>
           ''' The information about a simulated keyboard event.
           ''' </summary>
           <FieldOffset(8)>
           Public ki As KeyboardInput

           ''' <summary>
           ''' The information about a simulated hardware event.
           ''' </summary>
           <FieldOffset(8)>
           Public hi As HardwareInput

       End Structure

       ''' <summary>
       ''' Contains information about a simulated mouse event.
       ''' For more info see here:
       ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646273%28v=vs.85%29.aspx
       ''' </summary>
       <Description("Structure used for 'mi' parameter of 'INPUT' structure")>
       Friend Structure MouseInput

           ''' <summary>
           ''' The absolute position of the mouse,
           ''' or the amount of motion since the last mouse event was generated,
           ''' depending on the value of the dwFlags member.
           ''' Absolute data is specified as the 'x' coordinate of the mouse;
           ''' relative data is specified as the number of pixels moved.
           ''' </summary>
           Public dx As Integer

           ''' <summary>
           ''' The absolute position of the mouse,
           ''' or the amount of motion since the last mouse event was generated,
           ''' depending on the value of the dwFlags member.
           ''' Absolute data is specified as the 'y' coordinate of the mouse;
           ''' relative data is specified as the number of pixels moved.
           ''' </summary>
           Public dy As Integer

           ''' <summary>
           ''' If 'dwFlags' contains 'MOUSEEVENTF_WHEEL',
           ''' then 'mouseData' specifies the amount of wheel movement.
           ''' A positive value indicates that the wheel was rotated forward, away from the user;
           ''' a negative value indicates that the wheel was rotated backward, toward the user.
           ''' One wheel click is defined as 'WHEEL_DELTA', which is '120'.
           '''
           ''' If 'dwFlags' does not contain 'MOUSEEVENTF_WHEEL', 'MOUSEEVENTF_XDOWN', or 'MOUSEEVENTF_XUP',
           ''' then mouseData should be '0'.
           ''' </summary>
           Public mouseData As Integer

           ''' <summary>
           ''' A set of bit flags that specify various aspects of mouse motion and button clicks.
           ''' The bits in this member can be any reasonable combination of the following values.
           ''' The bit flags that specify mouse button status are set to indicate changes in status,
           ''' not ongoing conditions.
           ''' For example, if the left mouse button is pressed and held down,
           ''' 'MOUSEEVENTF_LEFTDOWN' is set when the left button is first pressed,
           ''' but not for subsequent motions.
           ''' Similarly, 'MOUSEEVENTF_LEFTUP' is set only when the button is first released.
           '''
           ''' You cannot specify both the 'MOUSEEVENTF_WHEE'L flag
           ''' and either 'MOUSEEVENTF_XDOWN' or 'MOUSEEVENTF_XUP' flags simultaneously in the 'dwFlags' parameter,
           ''' because they both require use of the 'mouseData' field.
           ''' </summary>
           Public dwFlags As MouseInput_Flags

           ''' <summary>
           ''' The time stamp for the event, in milliseconds.
           ''' If this parameter is '0', the system will provide its own time stamp.
           ''' </summary>
           Public time As Integer

           ''' <summary>
           ''' An additional value associated with the mouse event.
           ''' An application calls 'GetMessageExtraInfo' to obtain this extra information.
           ''' </summary>
           Public dwExtraInfo As IntPtr

       End Structure

       ''' <summary>
       ''' Contains information about a simulated keyboard event.
       ''' For more info see here:
       ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646271%28v=vs.85%29.aspx
       ''' </summary>
       <Description("Structure used for 'ki' parameter of 'INPUT' structure")>
       Friend Structure KeyboardInput

           ''' <summary>
           ''' A virtual-key code.
           ''' The code must be a value in the range '1' to '254'.
           ''' If the 'dwFlags' member specifies 'KEYEVENTF_UNICODE', wVk must be '0'.
           ''' </summary>
           Public wVk As Short

           ''' <summary>
           ''' A hardware scan code for the key.
           ''' If 'dwFlags' specifies 'KEYEVENTF_UNICODE',
           ''' 'wScan' specifies a Unicode character which is to be sent to the foreground application.
           ''' </summary>
           Public wScan As Short

           ''' <summary>
           ''' Specifies various aspects of a keystroke.
           ''' </summary>
           Public dwFlags As KeyboardInput_Flags

           ''' <summary>
           ''' The time stamp for the event, in milliseconds.
           ''' If this parameter is '0', the system will provide its own time stamp.
           ''' </summary>
           Public time As Integer

           ''' <summary>
           ''' An additional value associated with the keystroke.
           ''' Use the 'GetMessageExtraInfo' function to obtain this information.
           ''' </summary>
           Public dwExtraInfo As IntPtr

       End Structure

       ''' <summary>
       ''' Contains information about a simulated message generated by an input device other than a keyboard or mouse.
       ''' For more info see here:
       ''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms646269%28v=vs.85%29.aspx
       ''' </summary>
       <Description("Structure used for 'hi' parameter of 'INPUT' structure")>
       Friend Structure HardwareInput

           ''' <summary>
           ''' The message generated by the input hardware.
           ''' </summary>
           Public uMsg As Integer

           ''' <summary>
           ''' The low-order word of the lParam parameter for uMsg.
           ''' </summary>
           Public wParamL As Short

           ''' <summary>
           ''' The high-order word of the lParam parameter for uMsg.
           ''' </summary>
           Public wParamH As Short

       End Structure

#End Region

   End Class

#End Region

#Region " Enumerations "

#End Region

#Region " Public Methods "

   ''' <summary>
   ''' Sends a keystroke.
   ''' </summary>
   ''' <param name="key">
   ''' Indicates the keystroke to simulate.
   ''' </param>
   ''' <param name="BlockInput">
   ''' If set to <c>true</c>, the keyboard and mouse are blocked until the keystroke is sent.
   ''' </param>
   ''' <returns>
   ''' The function returns the number of events that it successfully inserted into the keyboard input stream.
   ''' If the function returns zero, the input was already blocked by another thread.
   ''' </returns>
   Public Shared Function SendKey(ByVal key As Char,
                                  Optional BlockInput As Boolean = False) As Integer

       ' Block Keyboard and mouse.
       If BlockInput Then NativeMethods.BlockInput(True)

       ' The inputs structures to send.
       Dim Inputs As New List(Of NativeMethods.INPUT)

       ' The current input to add into the Inputs list.
       Dim CurrentInput As New NativeMethods.INPUT

       ' Determines whether a character is an alphabetic letter.
       Dim IsAlphabetic As Boolean = Not (key.ToString.ToUpper = key.ToString.ToLower)

       ' Determines whether a character is an uppercase alphabetic letter.
       Dim IsUpperCase As Boolean =
           (key.ToString = key.ToString.ToUpper) AndAlso Not (key.ToString.ToUpper = key.ToString.ToLower)

       ' Determines whether the CapsLock key is pressed down.
       Dim CapsLockON As Boolean = My.Computer.Keyboard.CapsLock

       ' Set the passed key to upper-case.
       If IsAlphabetic AndAlso Not IsUpperCase Then
           key = Convert.ToChar(key.ToString.ToUpper)
       End If

       ' If character is alphabetic and is UpperCase and CapsLock is pressed down,
       ' OrElse character is alphabetic and is not UpperCase and CapsLock is not pressed down,
       ' OrElse character is not alphabetic.
       If (IsAlphabetic AndAlso IsUpperCase AndAlso CapsLockON) _
       OrElse (IsAlphabetic AndAlso Not IsUpperCase AndAlso Not CapsLockON) _
       OrElse (Not IsAlphabetic) Then

           ' Hold the character key.
           With CurrentInput
               .type = NativeMethods.InputType.Keyboard
               .ki.wVk = Convert.ToInt16(CChar(key))
               .ki.dwFlags = NativeMethods.KeyboardInput_Flags.KeyDown
           End With : Inputs.Add(CurrentInput)

           ' Release the character key.
           With CurrentInput
               .type = NativeMethods.InputType.Keyboard
               .ki.wVk = Convert.ToInt16(CChar(key))
               .ki.dwFlags = NativeMethods.KeyboardInput_Flags.KeyUp
           End With : Inputs.Add(CurrentInput)

           ' If character is alphabetic and is UpperCase and CapsLock is not pressed down,
           ' OrElse character is alphabetic and is not UpperCase and CapsLock is pressed down.
       ElseIf (IsAlphabetic AndAlso IsUpperCase AndAlso Not CapsLockON) _
       OrElse (IsAlphabetic AndAlso Not IsUpperCase AndAlso CapsLockON) Then

           ' Hold the Shift key.
           With CurrentInput
               .type = NativeMethods.InputType.Keyboard
               .ki.wVk = NativeMethods.VirtualKeys.SHIFT
               .ki.dwFlags = NativeMethods.KeyboardInput_Flags.KeyDown
           End With : Inputs.Add(CurrentInput)

           ' Hold the character key.
           With CurrentInput
               .type = NativeMethods.InputType.Keyboard
               .ki.wVk = Convert.ToInt16(CChar(key))
               .ki.dwFlags = NativeMethods.KeyboardInput_Flags.KeyDown
           End With : Inputs.Add(CurrentInput)

           ' Release the character key.
           With CurrentInput
               .type = NativeMethods.InputType.Keyboard
               .ki.wVk = Convert.ToInt16(CChar(key))
               .ki.dwFlags = NativeMethods.KeyboardInput_Flags.KeyUp
           End With : Inputs.Add(CurrentInput)

           ' Release the Shift key.
           With CurrentInput
               .type = NativeMethods.InputType.Keyboard
               .ki.wVk = NativeMethods.VirtualKeys.SHIFT
               .ki.dwFlags = NativeMethods.KeyboardInput_Flags.KeyUp
           End With : Inputs.Add(CurrentInput)

       End If ' UpperCase And My.Computer.Keyboard.CapsLock is...

       ' Send the input key.
       Return NativeMethods.SendInput(Inputs.Count, Inputs.ToArray,
                                      Marshal.SizeOf(GetType(NativeMethods.Input)))

       ' Unblock Keyboard and mouse.
       If BlockInput Then NativeMethods.BlockInput(False)

   End Function

   ''' <summary>
   ''' Sends a keystroke.
   ''' </summary>
   ''' <param name="key">
   ''' Indicates the keystroke to simulate.
   ''' </param>
   ''' <param name="BlockInput">
   ''' If set to <c>true</c>, the keyboard and mouse are blocked until the keystroke is sent.
   ''' </param>
   ''' <returns>
   ''' The function returns the number of events that it successfully inserted into the keyboard input stream.
   ''' If the function returns zero, the input was already blocked by another thread.
   ''' </returns>
   Public Shared Function SendKey(ByVal key As Keys,
                                  Optional BlockInput As Boolean = False) As Integer

       Return SendKey(Convert.ToChar(key), BlockInput)

   End Function

   ''' <summary>
   ''' Sends a string.
   ''' </summary>
   ''' <param name="String">
   ''' Indicates the string to send.
   ''' </param>
   ''' <param name="BlockInput">
   ''' If set to <c>true</c>, the keyboard and mouse are blocked until the keystroke is sent.
   ''' </param>
   ''' <returns>
   ''' The function returns the number of events that it successfully inserted into the keyboard input stream.
   ''' If the function returns zero, the input was already blocked by another thread.
   ''' </returns>
   Public Shared Function SendKeys(ByVal [String] As String,
                                   Optional BlockInput As Boolean = False) As Integer

       Dim SuccessCount As Integer = 0

       ' Block Keyboard and mouse.
       If BlockInput Then NativeMethods.BlockInput(True)

       For Each c As Char In [String]
           SuccessCount += SendKey(c, BlockInput:=False)
       Next c

       ' Unblock Keyboard and mouse.
       If BlockInput Then NativeMethods.BlockInput(False)

       Return SuccessCount

   End Function

#End Region

End Class