Search code examples
.netvb.netwinformspastekeyhook

Can't paste string using (SendKeys.Send)


I am trying to create an application that allows me to run an event even though the application isn't in focus. When I trigger the event using [ctrl + alt + V], it will pick a random string in an array and paste it into the currently selected inputbox (This could be, in firefox, notepad, a game...). However, the application won't paste the information from clipboard. I am using SendKeys.Send("^v") to replicate [ctrl + V].

Any suggestion to improve the code or solve the problem will be appreciated.

Public Class Form

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Tmr.Interval = 100
    Tmr.Start()
End Sub

Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick

    If CBool(GetAsyncKeyState(Keys.ControlKey)) And CBool(GetAsyncKeyState(Keys.V)) Then

        ' Create Variables for Array
        Dim PhrasesArray() As String = {"1", "2", "3", "4"}
        Dim Rand As New Random()
        Dim Index As Integer = Rand.Next(0, PhrasesArray.Length - 1)
        Dim SelectedValue = PhrasesArray(Index)

        My.Computer.Clipboard.SetText(SelectedValue) ' Copy To clipboard
    End If

End Sub

End Class

Update: I tried both: SendKeys.Send("^(v)") and SendKeys.Send("^v") neither one of them will trigger the paste command. I tried SendKeys.Send("{ENTER}") to see if any SendKeys work, and it did work. Using SendKeys.Send("{ENTER}") the application triggered the enter key.


Solution

  • You can use SendKeys.Send("^(v)") for that, if you want to send a Ctrl + V.


    Check the docs for each different command key you want to send: https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/sendkeys-statement