send a keystroke

chidambaram

Well-known member
Joined
Dec 27, 2007
Messages
62
Location
Chennai,India
Programming Experience
Beginner
hi,

I am working in VB.NET 2003(.NET Framework 1.1).


I want to send the key Enter or Space to the process.

My code is

<CODE>

Dim p1 As Process() = Process.GetProcesses()
MsgBox(p1.Length)
For i = 0 To p1.Length - 1
If p1(i).MainWindowTitle() <> "" Then
MsgBox("p1(i).MainWindowTitle()" & p1(i).MainWindowTitle())
title = p1(i).MainWindowTitle()

If title = "JPCMS Web Client - Microsoft Internet Explorer" Then
Application.DoEvents()
SendKeys.Send("{ENTER}")
End If
End If
Next
End

</CODE>

when SendKeys.Send("{ENTER}") line is executed, the program will struck in that line.

How can i send the enter key to the process?

Urgent plz and thanks in advance....
 
SendKeys sends to active window, you haven't activated the target, try:
VB.NET:
AppActivate("JPCMS Web Client") '(need only start of window title)
SendKeys.Send("{ENTER}")
 
Sir,

i received this error

System.invalidOperationException: Sendkeys cannot run inside this application
because the application is not handling window messages. Either change the application to handle messages, or use the sendkeys.wait method
 
You have to as they say, either change the application to handle messages (you're using a Console for this??) or use the SendKeys.SendWait method.
 
sir,

I am using threading concept in my program.

The above program is working finely without using threading concept.

But when i use threading concept it shows this error.

This is my code. Plz tell what error i made here.....

' Declaration

Public Shared thread1 As New Thread(AddressOf method1)
Public Shared thread2 As New Thread(AddressOf method2)
Public Shared thread3 As New Thread(AddressOf method3)

' code

Public Shared Sub processing1()
Dim i, k, j As Integer
thread2.Start()
thread3.Start()
End Sub
Public Shared Sub processing()
Dim gbWindow&
Dim ok&
Try
Dim inm As Integer
Dim title As String
Dim p1 As Process() = Process.GetProcesses()
MsgBox(p1.Length)
For inm = 0 To p1.Length - 1
If p1(inm).MainWindowTitle() <> "" Then
'MsgBox("p1(inm).MainWindowTitle()" & p1(inm).MainWindowTitle())
title = p1(inm).MainWindowTitle()
MsgBox(p1(inm).Id)
If title = "JPCMS Web Client - Microsoft Internet Explorer" Then
MsgBox(title)
AppActivate(p1(inm).Id)
Application.DoEvents()
SendKeys.Send("{ENTER}")
End If

End If
Next
End
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Shared Sub Main()
thread1.Start()
End Sub
Private Shared Sub method1()
processing1()
Thread.CurrentThread.Sleep(250)
End Sub

Private Shared Sub method2()
MsgBox("Second thread started")
End Sub
Private Shared Sub method3()
processing()
MsgBox("Third thread started")
End Sub


Thanks in advance
 
Have you tried SendKeys.SendWait?
Are you using a Console application for this?
 
I have confirmed the code works, the reason you needed SendWait was because of the threading. I have also tested it with a VB2003 application which is much slower, so you need to wait between the activate and the send. The thread code boils down to this:
VB.NET:
Sub thr()
    AppActivate("JPCMS Web Client")
    Threading.Thread.Sleep(500)
    SendKeys.SendWait("{ENTER}")
End Sub
Related to one of your previous questions I have also checked that this works for pressing the active button when the webpage is displaying a confirmation dialog.
 
Back
Top