locked
How do you use Sendkeys.Send? RRS feed

  • Question

  • I'm trying to use SendKeys on my form, so that when the user types a specific letter, it will send that letter to notepad or Word for them. I figured how how to indentify the letters ( 'ex. If e.Keycode = Keys.B Then... 'etc.) but I can't figure out how to do the SendKeys part.
    Saturday, September 30, 2006 9:43 PM

Answers

  • well since your application will be in focus, you need to get a handle to the other application and make it into the foreground (focused)

    you need to do some P/Invoking. There are alot of examples on these forums, so try a search and see what comes up.

    The problem you will have is that as soon as you focus on that application, like notepad, your application no longer recieves anymore inputs since its not in focus and I am not sure how to avoid this, or even if there is a way.

    For now, take a look at the FindWindow and SetForeGroundWindow API's and be sure to import the System.Runtime.Reflection namespace:

    http://www.pinvoke.net/default.aspx/user32/FindWindow.html

    http://www.pinvoke.net/default.aspx/user32/SetForegroundWindow.html

     

    VB.NET translation of SetForegroundWindow:

     

    <DllImport("user32.dll", SetLastError:=true)> public shared function SetForegroundWindow(ByVal hWnd as IntPtr) as boolean

    end function

     

    Sunday, October 1, 2006 12:56 AM
    Moderator

All replies

  • well since your application will be in focus, you need to get a handle to the other application and make it into the foreground (focused)

    you need to do some P/Invoking. There are alot of examples on these forums, so try a search and see what comes up.

    The problem you will have is that as soon as you focus on that application, like notepad, your application no longer recieves anymore inputs since its not in focus and I am not sure how to avoid this, or even if there is a way.

    For now, take a look at the FindWindow and SetForeGroundWindow API's and be sure to import the System.Runtime.Reflection namespace:

    http://www.pinvoke.net/default.aspx/user32/FindWindow.html

    http://www.pinvoke.net/default.aspx/user32/SetForegroundWindow.html

     

    VB.NET translation of SetForegroundWindow:

     

    <DllImport("user32.dll", SetLastError:=true)> public shared function SetForegroundWindow(ByVal hWnd as IntPtr) as boolean

    end function

     

    Sunday, October 1, 2006 12:56 AM
    Moderator
  • Private Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii = 13 Then
            KeyAscii = 0        ' tab
            SendKeys "{Tab}"
        End If
       
        'KeyAscii = Asc(UCase(Chr(KeyAscii))) 'upper
        Select Case KeyAscii
            'Case 48 To 57   'numbers
            'Case 8           ' back space
            'Case 65 To 122   ' let digits
            Case 97           ' a
            Case 65           ' A
            'Case 46          ' let dot
            'Case 32          ' space
            'Case 164         ' ñ
            'Case 165         ' Ñ
            Case Else
                KeyAscii = 0
                Beep
            End Select
    End Sub

     

     

    sample in VB 6,

     

    I don't know if it can you help

     

     

     

     

    Saturday, July 21, 2007 4:41 PM