Results 1 to 22 of 22

Thread: sendkeys runs without a problem, but it doesn't actually send any key output

  1. #1
    Junior Member
    Join Date
    Sep 12
    Posts
    18

    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

  2. #2
    Junior Member
    Join Date
    Sep 12
    Posts
    18

    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.

  3. #3
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,957

    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

  4. #4
    Junior Member
    Join Date
    Sep 12
    Posts
    18

    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.

  5. #5
    Junior Member
    Join Date
    Sep 12
    Posts
    18

    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.

  6. #6
    Junior Member
    Join Date
    Sep 12
    Posts
    18

    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.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 02
    Posts
    21,658

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

  8. #8
    Junior Member
    Join Date
    Sep 12
    Posts
    18

    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?

  9. #9
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 08
    Location
    On the Internet
    Posts
    2,870

    Re: sendkeys runs without a problem, but it doesn't actually send any key output

    Quote Originally Posted by themanwhocouldnotpre View Post
    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.

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 02
    Posts
    21,658

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

  11. #11
    Junior Member
    Join Date
    Sep 12
    Posts
    18

    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.

  12. #12
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 08
    Location
    On the Internet
    Posts
    2,870

    Re: sendkeys runs without a problem, but it doesn't actually send any key output

    Quote Originally Posted by themanwhocouldnotpre View Post
    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.

  13. #13
    Junior Member
    Join Date
    Sep 12
    Posts
    18

    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.

  14. #14
    PowerPoster techgnome's Avatar
    Join Date
    May 02
    Posts
    21,658

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

  15. #15
    Junior Member
    Join Date
    Sep 12
    Posts
    18

    Re: sendkeys runs without a problem, but it doesn't actually send any key output

    Quote Originally Posted by techgnome View Post
    I bet you start the project off as a "Console Application" didn't you?
    Indeed I did!

    Quote Originally Posted by themanwhocouldnotpre View Post
    formlesstreet, your post and techgnome's #12 post were the most useful.
    fix: techgnome's #10 post

  16. #16
    Junior Member
    Join Date
    Sep 12
    Posts
    18

    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!

  17. #17
    Junior Member
    Join Date
    Sep 12
    Posts
    18

    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?

  18. #18
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 08
    Location
    On the Internet
    Posts
    2,870

    Re: sendkeys runs without a problem, but it doesn't actually send any key output

    Take a look here.

  19. #19
    Junior Member
    Join Date
    Sep 12
    Posts
    18

    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.

  20. #20
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 11
    Location
    WNY
    Posts
    425

    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.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  21. #21
    PowerPoster techgnome's Avatar
    Join Date
    May 02
    Posts
    21,658

    Re: sendkeys runs without a problem, but it doesn't actually send any key output

    Quote Originally Posted by themanwhocouldnotpre View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

  22. #22
    Junior Member
    Join Date
    Sep 12
    Posts
    18

    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
  •