locked
simulate keyboard, mouse events RRS feed

  • Question

  • I am needing to come up with a way to program simulate ctrl v, and a mouse click.

     

    msdn states the best way is using hooks.

    other places say use sendinput, still others different ways.

     

    I have tried doing them all and as yet have no success.

     

    I am toying with having data on the clipboard. then with any of an assorted textboxs open , a mouse click will target whatever box its over. then i can paste using the ctrl v code.

     

    I can find no sample code at all. most microsoft links are dead even dealing with it.

     

    any ideas on best way to simulate ctrl v and a mouse click?

     

    Saturday, October 7, 2006 12:47 AM

All replies

  •  

    I think your questions need more specificity. Textboxes have those abilities built in for example. What kind of controls are you referring to?

    Saturday, October 7, 2006 12:59 AM
  • I am using dragon naturally speaking.

    Often I can not dictate directly into some programs I use, mostly due to citrex and them not being capable of speech reception. not select and say.

    Dragon has a dictation app built using vba you can use then a transfer button to copy and paste the text into whatever app you were in when called open this dictationbox.

    This is done in vba and easy to code.

    I am making a betetr more full featured dictationbox. instead of vba, i thought to learn more of vs 2005.

     

    And, I indeed have a much better app with a richtextbox to dictate into. works great.

     

    Now transfering the text into the app you were in when opened this dictationbox, grrrr.

    Very very easy using vba, very hard using vs 2005.

     

    I could be in a textbox, any textbox of any name. And i wont know the name of the textbox. the cursor is there when i call open my app, i dictate into it. then need to copy then paste the text into the original textbox, whose name can vary.

    I tried hooks to simulate ctrl v, sendinput, sendkeys, key events, etc

    I dont see how could just make a vba macro and use it from my app, i tried after reading of a way to use word macros from vs 2005.

     

    I am down to perhaps just making a voice macro and calling thta to act upon the vs 2005 app. i made one and indeed it works fine. but i would like to learn how to do this in vs2005.

     

     

     

     

    Saturday, October 7, 2006 1:59 AM
  •  

     

    Give me an hour... brb...

    I can only test it from the clipboard level of course but I think you'll like it. :)

    Renee

    Saturday, October 7, 2006 2:29 AM
  •  

    Charlie,

     

    I love this solution. It's very flexible.

    You should be able a create and empty form control-A, delete and clear everything out. Directly paste this in.

     

    It works beautfully. If there are any syntax errors, it the boards misrepresentation of the code, and I don't take responsibility for it. This was fun.

     

     

    Public Class Form1

        Protected Friend WithEvents tb1 As New TextBox

        Protected Friend WithEvents tb2 As New TextBox

        Protected Friend WithEvents tb3 As New TextBox

        Protected Friend WithEvents tb4 As New TextBox

        Protected Friend WithEvents ctxMenu As New ContextMenuStrip

        Protected Friend WithEvents ctxInsert As New ToolStripMenuItem("Insert")

        Protected Friend WithEvents ctxClearNInsert As New ToolStripMenuItem("Clear and Insert")

        Protected Friend WithEvents ctxPaste As New ToolStripMenuItem("Paste")

        Protected charpos As Integer

        Protected CurrentTextBox As TextBox

     

        Private Sub Form1_Load(ByVal sender As Object, _

                               ByVal e As System.EventArgs) Handles Me.Load

            Init()

        End Sub

        Private Sub textboxMouseDown(ByVal sender As Object, _

                                    ByVal e As System.Windows.Forms.MouseEventArgs) _

                                    Handles tb1.MouseDown, tb2.MouseDown, tb3.MouseDown, tb4.MouseDown

            CurrentTextBox = sender

            charpos = CurrentTextBox.GetCharIndexFromPosition(New Point(e.X, e.Y)) ' We have the character positon

            '                                                            in any of the textboxes

            ctxMenu.Items.Clear()

            Select Case CurrentTextBox.Text.Length

                Case 0

                    ctxMenu.Items.Add(ctxpaste)

                Case Else

                    ctxMenu.Items.Add(ctxInsert) : ctxMenu.Items.Add(New ToolStripSeparator)

                    ctxMenu.Items.Add(ctxClearNInsert)

            End Select

        End Sub

     

        Private Sub ctxMenuItems_Click(ByVal sender As Object, ByVal e As System.EventArgs) _

                Handles ctxInsert.Click, ctxClearNInsert.Click, ctxpaste.Click

     

            Select Case sender.name

                Case "ctxInsert"

                    CurrentTextBox.Text.Insert(charpos, My.Computer.Clipboard.GetText)

                    Dim s1 As String = CurrentTextBox.Text.Substring(0, charpos)

                    Dim s2 As String = CurrentTextBox.Text.Substring(charpos + 1)

                    CurrentTextBox.Text = s1 + My.Computer.Clipboard.GetText + s2

                Case "ctxClearNInsert"

                    CurrentTextBox.Clear()

                    CurrentTextBox.Text = My.Computer.Clipboard.GetText

                Case "ctxPaste"

                    CurrentTextBox.Text = My.Computer.Clipboard.GetText

            End Select

     

     

        End Sub

     

        Private Sub Init()

            Dim size As New Size(150, 80)

            tb1.Name = "tb1" : tb2.Name = "tb2" : tb3.Name = "tb3" : tb4.Name = "tb4"

            Me.Controls.Add(tb1) : Me.Controls.Add(tb2) : Me.Controls.Add(tb3) : Me.Controls.Add(tb4)

            Dim tb As TextBox

            For Each c As Control In Me.Controls

                tb = c

                tb.Size = size

                tb.Multiline = True

                tb.ContextMenuStrip = ctxMenu

                tb.ScrollBars = ScrollBars.Vertical

            Next

            tb1.Location = New Point(20, 20) : tb2.Location = New Point(180, 20)

            tb3.Location = New Point(20, 110) : tb4.Location = New Point(180, 110)

            ctxPaste.Name = "ctxPaste" : ctxClearNInsert.Name = "ctxClearNInsert"

            ctxInsert.Name = "ctxInsert"

     

        End Sub

    End Class

     

    Saturday, October 7, 2006 3:54 AM