Results 1 to 20 of 20

Thread: Send keys to application what's not in focus.

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    9

    Arrow Send keys to application what's not in focus.

    Hello, guys.
    Im newbie in vb, so be nice to me pls.
    I want to know how to send keys to some application, for example,
    minimized notepad.
    I have searched in google, youtube and this forum too.
    Ppl saying that with Sendkeys.send you can only send keys to application what is in focus of screen. So, can anybody post code here for sending keys to application? Please.


    Thanks. Dyn.

  2. #2
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: Send keys to application what's not in focus.

    I think that you can find and give the focus to a specific application window using its window title.
    VB.NET Code:
    1. Dim HWND As IntPtr
    2. Dim windowTitle As String
    3. HWND = FindWindow(Nothing, windowTitle )
    4. If HWND.ToString() <> "0" Then
    5.     SetForegroundWindow(HWND)
    6. End If

    then you can send your keys using SendKeys.SendWait()
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    9

    Re: Send keys to application what's not in focus.

    oh, Im too big nub to understand this.
    Vb saying FindWindow is not declerated

  4. #4
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: Send keys to application what's not in focus.

    Sorry, I totally forgot that you need to declare those two functions globally:
    vb.net Code:
    1. Private Declare Auto Function FindWindow Lib "user32" ( _
    2. ByVal lpClassName As String, _
    3. ByVal lpWindowName As String) As IntPtr
    4.  
    5. Private Declare Function SetForegroundWindow Lib "user32" _
    6. (ByVal hwnd As System.IntPtr) As Integer
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  5. #5
    Hyperactive Member
    Join Date
    Jul 2009
    Posts
    489

    Re: Send keys to application what's not in focus.

    Quote Originally Posted by stlaural View Post
    Sorry, I totally forgot that you need to declare those two functions globally:
    vb.net Code:
    1. Private Declare Auto Function FindWindow Lib "user32" ( _
    2. ByVal lpClassName As String, _
    3. ByVal lpWindowName As String) As IntPtr
    4.  
    5. Private Declare Function SetForegroundWindow Lib "user32" _
    6. (ByVal hwnd As System.IntPtr) As Integer
    it depends where he declare them, i think.

    in your code it seems it was declared in a form, because it's local.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    9

    Re: Send keys to application what's not in focus.

    Yep, that's my next question xD
    Where I need to place this code?

    link for image - http://img715.imageshack.us/img715/9589/errorc.png

  7. #7
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: Send keys to application what's not in focus.

    I think it's ok where you declared it but you wrote :
    Code:
    Private Declare Function SetForegroundWindow Lib "user32" ()
    ByVal hwnd As System.IntPtr As Integer
    where you should have written :
    Code:
    Private Declare Function SetForegroundWindow Lib "user32" _
    (ByVal hwnd As System.IntPtr) As Integer
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    9

    Re: Send keys to application what's not in focus.

    Oh, sorry.
    Now it looks fine, but how to make it to send the keys to application?

  9. #9
    Hyperactive Member
    Join Date
    Jul 2009
    Posts
    489

    Re: Send keys to application what's not in focus.

    so now you need to find the window, set it foreground
    (as in the code above)
    then sendkeys
    the keys should be send to this window

  10. #10
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: Send keys to application what's not in focus.

    Now you can use SendKey methods.
    Have a look at those two msdn pages :

    SendKeys class

    SendKeys.SendWait Method
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  11. #11

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    9

    Re: Send keys to application what's not in focus.

    ooh, do this code will focus my window?
    Very big thanks for helping, but then this code isnt for me, because I want to send keys to minimized application.
    Hmm how to explain... For example, while Im writing a new message here, the program will keep sending keys to some application.

  12. #12

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    9

    Re: Send keys to application what's not in focus.

    eh, Im too big nub.
    looks like I cant do anything more than copy/paste the code.
    I rly dont understand what I should do now ;(
    To make a program what sends keys to application whats in focus is easy, but this is too hard for me.
    Sry all for wasting your time.

    If some1 can show me a video tutorial or upload vb project that would be awesome. Thanks anyway.
    Last edited by DynLV; Jan 21st, 2010 at 12:03 PM.

  13. #13
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Send keys to application what's not in focus.

    Quote Originally Posted by DynLV View Post
    ooh, do this code will focus my window?
    Very big thanks for helping, but then this code isnt for me, because I want to send keys to minimized application.
    Hmm how to explain... For example, while Im writing a new message here, the program will keep sending keys to some application.
    There are some things you need to know that have been assumed. First, in order to SendKeys to an application, it has to be in focus. That is why they have been showing you how to use the SetForegroundWindow API. Once it's in focus, you can send the required keys.

    I think it's ok where you declared it but you wrote :
    Code:
    Private Declare Function SetForegroundWindow Lib "user32" ()
    ByVal hwnd As System.IntPtr As Integer
    where you should have written :
    Code:
    Private Declare Function SetForegroundWindow Lib "user32" _
    (ByVal hwnd As System.IntPtr) As Integer
    The reason why the underscore must appear there, in this instance, is because the User is creating a new line of existing code. Basically he did it for readability. You could just continue the line like so:

    VB.NET Code:
    1. Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As System.IntPtr) As Integer

    From there, you can do as you need.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  14. #14
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Send keys to application what's not in focus.

    Quote Originally Posted by DynLV View Post
    eh, Im too big nub.
    looks like I cant do anything more than copy/paste the code.
    I rly dont understand what I should do now ;(
    To make a program what sends keys to application whats in focus is easy, but this is too hard for me.
    Sry all for wasting your time.
    It's never a waste of time. Using Windows' APIs can be difficult, especially if you're just starting. It's wise of you to know you've bitten off more than you can chew. Most people don't realize this, they get frustrated, and give up altogether.

    Pick an easier project and once you begin to learn, things like this will come naturally.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  15. #15

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    9

    Re: Send keys to application what's not in focus.

    I think there isnt anything hard if some1 has made a tutorial xD
    I made a program like this what send keys + have hotkeys + system tray icon + options in about 1h (my first program xD). All was easy cuz all what I needed i got in youtube videos. now only need to know how to send keys to minimized application.
    Im saw program like mine where you can choose process from list box and then the program send keys to that process. but I dont remember the program name so I tried to create myself xD

  16. #16
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: Send keys to application what's not in focus.

    @weirddemon: thanks for the clarifications.

    The only way I know to send keys to an application needs the said application to have focus.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  17. #17
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Send keys to application what's not in focus.

    Quote Originally Posted by stlaural View Post
    @weirddemon: thanks for the clarifications.

    The only way I know to send keys to an application needs the said application to have focus.
    That's the same as I thought. I wasn't aware of a method of sending keys to an application that isn't in focus. I've never seen it, but that doesn't mean it's impossible.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  18. #18

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    9

    Re: Send keys to application what's not in focus.

    Hmm, oke. I will try to search more for this thing.

    Anyway, if some1 know please send me PM or write here. Thanks

  19. #19
    Hyperactive Member Philly0494's Avatar
    Join Date
    Apr 2008
    Posts
    485

    Re: Send keys to application what's not in focus.

    what application? it depends alot on the design of the app you are sending to

  20. #20

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    9

    Re: Send keys to application what's not in focus.

    It's game. KalOnline.
    I think it not matter how looks the app, cuz my program will not write smth in chat but send keys for action in game.
    Like trainer for GTA VC is sending keys ASPIRINE to replenish health.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width