Visual Basic SendKeys is one very interesting and important function in Visual Studio. The post explains the Send Keys Syntax, Example and Common Errors in SendKeys. Previously in Visual Basic 6 and early, we were using the dll file to Send keystrokes to computer. However now with the inbuilt feature, we can send any key combination of keys in Visual Studio.
Visual Studio SendKeys Syntax & Example
Method 1
My.Computer.Keyboard.SendKeys(Char[] keys, Boolean Wait)
Char[] Keys is the set of characters(strings) keys that you want to send. Boolean Wait is as mentioned a boolean value, which specifies whether the computer must wait for the each character to complete transmission, before transmitting the next one.
Example: My.Computer.Keyboard.SendKeys(“Hello”,true)
This will transmit the word “Hello” to the computer without any overlapping of the characters.
Method 2
SendKeys.send(Char[] keys)
Char[] Keys is the set of characters that should be transmitted to the computer, there is no option to specify the Wait option in this method. However this is the widely used method in industry.
Example: SendKeys.Send(“Hello”)
Visual Studio SendKeys Common Errors
Not Specifying Delay Time
If you fail to specify the delay time, and you start the module directly, the keys will be sent to the active area which is at present focused. So before transmitting the text, the control must be transferred to the window in which we want the keys to be sent. To solve this problem , we can use the Thread.Sleep feature in Visual Studio and halt the execution for the specified time.
Syntax: Threading.Thread.Sleep(duration in milliseconds)
Example: Threading.Thread.Sleep(1000) <- This will wait for 1 second.