ChrisBettencourt

Can anyone tell me why the SendKeys() function would not be working for me I have a sneaking suspicion that one possibility could be that the process has not fully opened before my.computer.keyboard is performing sendkeys().

I'm getting fairly desperate here, partly because I don't know what I'm doing. I'd appreciate any help!



Re: Visual Basic Language SendKeys() not working

kleinma

why dont you set up a testing app, that you can test out sendkeys() when you are sure the target application is open




Re: Visual Basic Language SendKeys() not working

ChrisBettencourt

thats interesting....................I changed the script to read like so:

Dim MyPSI As New ProcessStartInfo("C:\FlowMaster 6.0\flowcut.exe")

MyPSI.WindowStyle = ProcessWindowStyle.Normal

MyPSI.WorkingDirectory = "C:\FlowMaster 6.0\"

Dim MyProcess As Process = Process.Start(MyPSI)

Dim notes As New ProcessStartInfo("notepad.exe")

notes.WindowStyle = ProcessWindowStyle.Normal

Dim Process2 As Process = Process.Start(notes)

My.Computer.Keyboard.SendKeys("This is a new text file.")

My.Computer.Keyboard.SendKeys("{enter}")

My.Computer.Keyboard.SendKeys("Read it and weep.")

And the end result was

One instance of flowcut, two blank notepads, and one that said:

new text file.
Read it and weep.This is a new text file.
Read it and weep.This is a new text file.
Read it and weep.

So it looks like its sending the keys, but multiple times. This code is attached to a button, and i'm definately only clicking it once.





Re: Visual Basic Language SendKeys() not working

kleinma

perhaps set a break point so you can see how many times the code is actually running It sounds odd that the code you posted would open 2 instances of notepad..




Re: Visual Basic Language SendKeys() not working

ChrisBettencourt

Could the fact that I'm running NT be causing any problems I just can't figure out why everything is so buggy! When I added a breakpoint and stepped through manually, the form stopped responding. Also, it seems that any random number of notepad windows can be opened up with that code above, the lowest was 2, the greatest was 9. The text can be repeated any random number of times and spread across the open notepads, with some instances of notepad being blank, some having all the text, some having only partial statements. None of this is a problem if I change the SendKeys message from words seperated by spaces to just one word with no space, or just one letter. If I do that, only one instance of notepad opens up, and it is blank.





Re: Visual Basic Language SendKeys() not working

kleinma

NT as in Windows NT 4

If so what service pack I didn't even think you could install visual studio on NT 4






Re: Visual Basic Language SendKeys() not working

ChrisBettencourt

Oh. nevermind. I forgot that was the old machine and I just went through getting this version of Flowcut that will run on XP. I'm on XP pro service pack 2. I think i've been staring at this for too long.

so it seems like its really looking at the text within the quotation marks. i've put a little message box after the send keys line just to see if it finished. when i have a space in there, (abc ABC), it never types anything and it doens' t display the message box. when i have no space (abcABC), it displays the message box, but still doesn't type anything in.

i thought that maybe it just needed a longer line to buy time, but the whole alphabet twice isn't showing, even though common sence states that the line is being executed, since the msgbox is being displayed.

it seems like the multiple documents and text actually showing happened as a result of multiple spaces in the line.

none of this makes sense to me and i'm getting quite frustrated. SendKeys() isn't supposed to be brain surgery, is it Smile





Re: Visual Basic Language SendKeys() not working

PEng1

The code below works, I believe your problem lies in the fact that SendKeys Sends the keystrokes to the active window, so the keystrokes could be sent to your application if it is active and not notepad or what ever other program you are trying to run. Long story short I don't know exactly why this works I just know that it does.

Hope this helps.

Code Snippet

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim notes As New ProcessStartInfo("notepad.exe")

notes.WindowStyle = ProcessWindowStyle.Normal

Process.Start(notes)

System.Threading.Thread.Sleep(5000)

My.Computer.Keyboard.SendKeys("This is a New Text File!", True)

MsgBox("The keys have been sent")

End Sub

=== Edited by PEng1 @ 21 May 2007 9:54 PM UTC===
Edit: On further examination the Thread Sleep time must be there but can be as small as 20 mS and still work. Again, Don't know why, only know that it works.






Re: Visual Basic Language SendKeys() not working

Swade

Ok, here's the problem.

SendKeys() sends keys to the active app. So, since you're not declaring an activeapp statement in your code, what happens is so:

You click a button, event starts....

Notepad is opened

Keys are "sent"

Focus may or may not return to your app, or it may focus on notepad for a split second...

Key "Enter" is sent to your app kicking of the event again....

Loops forever or until there is enough paging done that the SendKeys("{ENTER}") is sent to notepad before focus is returned to your app

Here's how you solve it:

Code Snippet

Dim ProcID As Integer

ProcID = Shell("notepad.exe", AppWinStyle.NormalFocus, True, 6000)

Do Until I = 10

Try

AppActivate(ProcID)

I = 10

Catch Aex As ArgumentException

I += 1

End Try

Loop

My.Computer.Keyboard.SendKeys("Hello There", True)

My.Computer.Keyboard.SendKeys("{ENTER}", True)

The key thing is the ProcID = and the AppActivate(ProcID)

You can avoid the shell if you use

Code Snippet

Dim Prog As String = "C:\Program Files\Microsoft Office\Office10\MSACCESS.EXE"

Dim Cmd As New StringBuilder()

Cmd.AppendFormat("{0} /xAutoUpdateTables", "C:\db1.mdb")

Dim Proc As New Process()

With Proc.StartInfo

.FileName = Prog

.Arguments = Cmd.ToString()

.CreateNoWindow = True

.WindowStyle = ProcessWindowStyle.Hidden

.UseShellExecute = True

End With

Proc.Start()

AppActivate(Proc.ID)

.....






Re: Visual Basic Language SendKeys() not working

ChrisBettencourt

thank you all! All of these solutions are working for me in my various situations. I really appreciate your time with this. I think that I'm starting to get why things are happening now too.

thanks again!