|
-
Sep 5th, 2012, 09:13 PM
#1
Thread Starter
Junior Member
sendkeys runs without a problem, but it doesn't actually send any key output
Below I've attached a simple code for sendkeys. I open a Notepad, then execute the code from Visual Studio 2008 (I'm using Win XP) by pressing F5. The program executes fine, and finishes without errors, but nothing is actually outputed to the Notepad. I'd appreciate anyone who can copy/paste this to their VB software and give it a quick run. Don't forget to open a Notepad first. The program will focus on the Notepad, pause for 2 second, then execute the sendkeys function, then pause for 2 seconds and END. Thanks. I've exhausted searching for answers elsewhere.
Module Module1
Public Class SendKeys
Public Shared Sub Send( _
ByVal keys As String _
)
End Sub
End Class
Sub Main()
AppActivate("Untitled - Notepad")
System.Threading.Thread.Sleep(2000)
SendKeys.Send("hello")
System.Threading.Thread.Sleep(2000)
End Sub
End Module
-
Sep 5th, 2012, 09:15 PM
#2
Thread Starter
Junior Member
Re: sendkeys runs without a problem, but it doesn't actually send any key output
Sorry for repeat thread, posting issue. Please remove this duplicate thread.
-
Sep 5th, 2012, 10:05 PM
#3
Re: sendkeys runs without a problem, but it doesn't actually send any key output
Your send sub has no code in it so it does not do anything
-
Sep 5th, 2012, 10:35 PM
#4
Thread Starter
Junior Member
Re: sendkeys runs without a problem, but it doesn't actually send any key output
What do I have to put in the sub? Will it fixate the sendkeys output? For example, in my example code I wrote sendkeys("hello"). If I added this to the sub then would the sendkeys function always send out "hello"? Because I need to send out different keyboard combinations.
-
Sep 6th, 2012, 11:38 AM
#5
Thread Starter
Junior Member
Re: sendkeys runs without a problem, but it doesn't actually send any key output
I think my problem might be in the declaration code. I made the following declaration code by copying it from the microsoft site:
Public Class SendKeys
Public Shared Sub Send( _
ByVal keys As String _
)
End Sub
End Class
The sendkeys class page is here:
http://msdn.microsoft.com/en-us/libr...keys.aspx#Y228
The send method is here:
http://msdn.microsoft.com/en-us/libr...keys.send.aspx
Am I supposed to add some more code in the sub?
...............................................
When I start typing sendkeys in VisualStudio it doesn't show a pop-up window with either send or sendkeys, as if the function is not recognized by the program. Does that mean sendkeys doesn't exist in my version of VB?
Last edited by themanwhocouldnotpre; Sep 6th, 2012 at 11:44 AM.
-
Sep 6th, 2012, 12:12 PM
#6
Thread Starter
Junior Member
Re: sendkeys runs without a problem, but it doesn't actually send any key output
I'm starting to get the impression that sendkeys can't be used in the main program section Sub Main(), that it can only be used in a sub to assign it to some event, especially a mouseclick. Is this correct? I don't want this. I need a program that sends keystrokes by itself without the user touching or doing anything. Can you use sendkeys in Sub Main()? And if not, is there a function that can be used in Sub Main() to send any combination of keystrokes as output, like if it were a PRINT function?
EDIT: My program has to do some complicated copy/pasting and editing between html pages and txt files. I'm basically trying to automate myself doing this repetitive task over and over. When I run it, I should be able to leave it alone for 10 hours to do its thing. The computer would be simulating myself moving and clicking the mouse and pressing the keyboard.
Last edited by themanwhocouldnotpre; Sep 6th, 2012 at 12:16 PM.
-
Sep 6th, 2012, 12:29 PM
#7
Re: sendkeys runs without a problem, but it doesn't actually send any key output
sigh.... you misunderstood the code... the declarations they show you is HOW it is represented in the framework... SendKeys is a class that's already built and ready for you to use... inside of that class, there's a Send method... all ready to go... what you should do is go to the page on the Send method, then scroll down to the bottom where the Sample Code is... THAT shows you how it's used... All you should need to do is get rid of the SendKeys class declaration...
That said... SendKey is notoriously unpredictable at times... even under the best of conditions, it's not guaranteed that it will work as you want it to.
-tg
-
Sep 6th, 2012, 03:03 PM
#8
Thread Starter
Junior Member
Re: sendkeys runs without a problem, but it doesn't actually send any key output
techgnome, the example code in the send methods page places sendkeys inside a sub, from which it sends a predefined string whenever you click the mouse on a button. I thought sendkeys was a function that I could enter in any line in the Sub Main() section of the program, and each line could have whatever set of keys I wanted to be pressed. So it doesn't work that way? And if it doesn't work that way, is there any instruction that allows me to instruct the program to type keys?
I don't know if you've heard of automated mouse software. They allow you to record any combination of mouse clicks and keyboard input. Then you can run it and it presses the keys and mouse clicks like you instructed it to do, automating repetitive tasks that would take you forever. VB has the instructions for mouse positioning and clicking, I'm just missing the keyboard input. It's starting to look like sendkeys isn't what I need. Is there something else that fits my description?
-
Sep 6th, 2012, 03:34 PM
#9
Re: sendkeys runs without a problem, but it doesn't actually send any key output
 Originally Posted by themanwhocouldnotpre
techgnome, the example code in the send methods page places sendkeys inside a sub, from which it sends a predefined string whenever you click the mouse on a button. I thought sendkeys was a function that I could enter in any line in the Sub Main() section of the program, and each line could have whatever set of keys I wanted to be pressed. So it doesn't work that way? And if it doesn't work that way, is there any instruction that allows me to instruct the program to type keys?
I don't know if you've heard of automated mouse software. They allow you to record any combination of mouse clicks and keyboard input. Then you can run it and it presses the keys and mouse clicks like you instructed it to do, automating repetitive tasks that would take you forever. VB has the instructions for mouse positioning and clicking, I'm just missing the keyboard input. It's starting to look like sendkeys isn't what I need. Is there something else that fits my description?
I don't mean to be rude, but, did you scroll down the page to the Examples section and completely read it on the MSDN documentation? It shows you exactly how to use the method; what you are doing is just typing out the class and method again (in your code example), which does not need to be done as all the documentation is doing is showing you how the class is declared, and then how the method is declared, meaning all you need to do is:
Code:
SendKeys.Send("{ENTER}")
to send an Enter key, from any routine you want. It's just a simple misunderstanding of documentation which does happen.
Also
I don't know if you've heard of automated mouse software.
Macros I believe is what they're called. SendKeys will help you, but it isn't known for being 100% reliable, but it's a start.
-
Sep 6th, 2012, 03:47 PM
#10
Re: sendkeys runs without a problem, but it doesn't actually send any key output
It's starting to look like sendkeys isn't what I need.
On the contrary, it IS what you want.... I told you how to fix your code... all you need to do is remove part of your code... what's happened is that you have defined your OWN CUSTOM SendKeys class... and the rules of ... of... what ever... Scope I guess... says that the lowest defined level of some object rumps all above... so at the moment, it's as if SendKeys doesn't exist since you over wrote it with your own... and your own version does nothing...
This is what you need:
Code:
Module Module1
Sub Main()
AppActivate("Untitled - Notepad")
System.Threading.Thread.Sleep(2000)
SendKeys.Send("hello")
System.Threading.Thread.Sleep(2000)
End Sub
End Module
That's it... no more, no less... well you can have more if you want to send more than just "hello"... you get the idea... for all intents and purposes, that should work... Now... it COULD "fail" IF notepad doesn't get focus...and by fail, I mean the keys are sent, but not received by notepad. That's where the shortcommings of SendKeys usually become a problem.
-tg
-
Sep 6th, 2012, 03:56 PM
#11
Thread Starter
Junior Member
Re: sendkeys runs without a problem, but it doesn't actually send any key output
techgnome, when I enter the code you posted (post #10), SendKeys is underlined by VisualStudio and the error text says: "Name 'SendKeys' is not declared."
Usually, when I type an instruction, it starts to autocomplete and show a dropdown list of possible instructions, variables, etc. I noticed that sendkeys, or anything with send, doesn't register, apparently. I don't know if this might help.
-
Sep 6th, 2012, 04:07 PM
#12
Re: sendkeys runs without a problem, but it doesn't actually send any key output
 Originally Posted by themanwhocouldnotpre
techgnome, when I enter the code you posted (post #10), SendKeys is underlined by VisualStudio and the error text says: "Name 'SendKeys' is not declared."
Usually, when I type an instruction, it starts to autocomplete and show a dropdown list of possible instructions, variables, etc. I noticed that sendkeys, or anything with send, doesn't register, apparently. I don't know if this might help.
If you look at the documentation, you will see that SendKeys is in the System.Windows.Forms namespace. You obviously do not have a reference nor an import to that namespace so you will not have access to that class nor the accompanying methods. Fix the lack of reference and namespace import and you will be fine.
-
Sep 6th, 2012, 04:41 PM
#13
Thread Starter
Junior Member
Re: sendkeys runs without a problem, but it doesn't actually send any key output
formlesstreet, I think that's probably what's going on here. Your post and techgnome's #12 post were the most useful. I will report back how it went. Thank you all for bearing with me.
-
Sep 6th, 2012, 08:15 PM
#14
Re: sendkeys runs without a problem, but it doesn't actually send any key output
Ahhh.. yeah, I missed that... I bet you start the project off as a "Console Application" didn't you? :P Just add the reference and you should be good.
-tg
-
Sep 6th, 2012, 09:31 PM
#15
Thread Starter
Junior Member
Re: sendkeys runs without a problem, but it doesn't actually send any key output
 Originally Posted by techgnome
I bet you start the project off as a "Console Application" didn't you?
Indeed I did!
 Originally Posted by themanwhocouldnotpre
formlesstreet, your post and techgnome's #12 post were the most useful.
fix: techgnome's #10 post
-
Sep 7th, 2012, 05:42 PM
#16
Thread Starter
Junior Member
Re: sendkeys runs without a problem, but it doesn't actually send any key output
Woo-hoo! All systems go!
There's just one detail, I have to use sendkeys.sendwait, if I use sendkeys.send I get the error "sendkeys cannot run in this app because the app is not handling windows messages. Change the app to handle windows messages." How do you fix this? It's not a crucial question, I can do with sendwait, but if anyone can help, I'd still appreciate it.
Much thanks to everyone that responded to me. Very happy right now!
-
Sep 8th, 2012, 01:02 PM
#17
Thread Starter
Junior Member
Re: sendkeys runs without a problem, but it doesn't actually send any key output
I'm trying to use SetForegroundWindow, so I can hopefully use sendkeys.send instead of sendkeys.sendwait. I found out its namespace is Microsoft.VisualStudio.Shell.Interop. I tried to add it as a reference but it didn't appear in the .NET dropwdown list. The documentation said its file is interop.dll, so I searched my PC for interop.dll and found it in C:\Windows\Assembly. Then I went to the add reference window again, switched to the Browse tab, went to C:\Windows\Assembly, and a whole list of names appeared, including Microsoft.VisualStudio.Shell.Interop (but also Microsoft.VisualStudio.Shell.Interop 8.0 and 9.0). I selected it, pressed OK, and nothing happened. I tried writing the filename and pressing OK, or even its key token, but it would say file not found. How can I install it?
-
Sep 8th, 2012, 04:07 PM
#18
Re: sendkeys runs without a problem, but it doesn't actually send any key output
-
Sep 10th, 2012, 10:27 AM
#19
Thread Starter
Junior Member
Re: sendkeys runs without a problem, but it doesn't actually send any key output
SendKeys.sendwait works perfectly. I read so many comments about its unreliability, but my personal experience is that it's flawless and insanely fast.
-
Sep 10th, 2012, 11:17 AM
#20
Hyperactive Member
Re: sendkeys runs without a problem, but it doesn't actually send any key output
I actually agree with a lot of responses here, SendKey is very unpredictable. This is why I prefer to use keydb_event give it a look and see if this can suit your needs.
-
Sep 10th, 2012, 11:25 AM
#21
Re: sendkeys runs without a problem, but it doesn't actually send any key output
 Originally Posted by themanwhocouldnotpre
SendKeys.sendwait works perfectly. I read so many comments about its unreliability, but my personal experience is that it's flawless and insanely fast.
It's unpredictably comes from the fact that there is no way to really control where the keys are sent... If I send keys "hello" ... there's no way to consistently and accurately know (from the point of the app) if it ended up in Notepad like it's supposed to, or if the user clicked into VS and I end up with ello in the middle of my code. It would be much more accurate if one could grab the handle of the target and just send the messages straight there. But other than that, yes, I agree, it's fast and easy to work with and will send exactly what you tell it to send.
-tg
-
Oct 26th, 2012, 05:35 PM
#22
Thread Starter
Junior Member
Re: sendkeys runs without a problem, but it doesn't actually send any key output
Reviving my thread after 1 month.
Very rarely, sendkeys.sendwait will hang indefinitely. This happens when I tell it to send CTRL+C (copy) to a browser webpage and there's some sort of issue about the page not having loaded yet and is not going to load for whatever reason, so execution just stays there, waiting for something that will never happen, and my CPU temp approaches sickening high levels after a while. Is there some code to break this? Formally, it's not an error, the line is just waiting for input.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|