0

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.

Elias Wick
  • 483
  • 6
  • 21
  • 1
    What do you mean? It doesn't send any key or it sends exactly ^v – CoderUni Jul 19 '19 at 11:46
  • 1
    I want to essentially trigger the paste command to paste the text that is copied to the clipboard. The shortcut for pasting is [ctrl + v], which is the reason for why I want to use sendkeys. – Elias Wick Jul 19 '19 at 11:55

1 Answers1

1

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

CoderUni
  • 5,474
  • 7
  • 26
  • 58
  • 1
    Unfortunately it doesn't work. I tried both: `SendKeys.Send("^(v)")` and `SendKeys.Send("^v")` It wont trigger the paste command. Thank you for trying to help me. – Elias Wick Jul 19 '19 at 11:56
  • 1
    I'm marking your answer as correct because it is. It did indeed work. My own code worked as well. The problem was that apparently the alt key triggered a part of the context menu in applications which removed the selected area where I was about to post my work. – Elias Wick Jul 19 '19 at 12:24