Link to home
Start Free TrialLog in
Avatar of OnlineNoob
OnlineNoob

asked on

sendkeys alternative

hi,

I want to be able to send keys to another application, even while that application is not the active window.  How might I do this in vb.net?
Avatar of mmarinov
mmarinov

use
AppActivate(thenameofapplicatio) and then
SenKeys ( thekeys )

B..G
Avatar of OnlineNoob

ASKER

thank you for your comment, but that will not work for me.  i need a way to send keys to an application even when the application isn't activated.  thru the api or something.
Public Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Public Const KEYEVENTF_EXTENDEDKEY = &H1
Public Const KEYEVENTF_KEYUP = &H2

then whenever u want u can use it to automate keyboard strokes like

keybd_event(CByte(System.Windows.Forms.Keys.A), 0, 0, 0)
keybd_event(CByte(System.Windows.Forms.Keys.B), 0, 0, 0)
keybd_event(CByte(System.Windows.Forms.Keys.E), 0, 0, 0)

to automate A, B, E ... Is this is what yu are looking for?

skaykay!
ok, let me elaborate, maybe i was too unclear on my question and i'm sorry.

I need to have vb, activate notpad with something like app.activate "notepad".  (Please note that's vb6, i dont' know vb.net version)
HOWEVER, I do not want notepad to come to front of screen, i want notepad to stay hidden while my vb.net program sends keys to it in the specified window area.  So i don't want to use app.activate or the vb.net equivalent.  I need something i can specify to send to without it being the active window.
2 txt boxws - txtApplication - enter name of application
txtKeys - what keys to send


Public Class SendKeysForm
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

    ' API declares
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    Private Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Integer) As Integer

    ' send keys to selected app

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        SendKeysToApplication(txtApplication.Text, txtKeys.Text)
    End Sub

    ' a reusable routine that sends specified keys to a given app

    Sub SendKeysToApplication(ByVal appTitle As String, ByVal keys As String)
        ' Find the other application.
        Dim hWnd As Integer = FindWindow(Nothing, appTitle)
        ' Exit if not found.
        If hWnd <= 0 Then
            MessageBox.Show("Application not found", "Error", _
                MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If
        ' Make it the active application.
        SetForegroundWindow(hWnd)
        ' Send the keys and wait until app process the keystrokes
        SendKeys.SendWait(keys)
    End Sub

End Class
iboutchkine, that's close however the code still brings the window to the front.  Where can I learn more about this api you are suggesting?
That is th eold API that you can find in Dan Appleman book. The only thisn that for .NET you have to change datatype from Long to Integer.
Maybe if you won't be able to send keys on the background, you can bring your form to the front after sending keys
SOLUTION
Avatar of roverm
roverm
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what do i change ANY to?  vb.net doesn't support "ANY" type.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial