Solved

Keyboard Commands To Minimized Window

Posted on 2002-06-25
14
425 Views
Last Modified: 2008-03-03
Can this be done? Im succesfully using the keybd_event API to send commands to my window, however it only works if the window is viewable with focus. I need to send keyboard commands to a MINIMIZED WINDOW with or without focus(preferrably without focus). Heres my existing code below:

'--------------------------Globals.vb
Public Class MyApi
    <DllImport("user32.dll")> Public Shared Function _
         FindWindow(ByVal strClassName As String, ByVal _ strWindowName As String) As Long
    End Function

    <DllImport("user32.dll")> Public Shared Sub _
        keybd_event(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    End Sub

End Class

Public Enum KEYLIST
'VK_Keys Here
End Enum

    Public keys As KEYLIST

    Public Sub presskey(ByVal keystroke As Int16)

        MyApi.keybd_event(keystroke, 0, 1, 0)
        delay(0.1)
        MyApi.keybd_event(keystroke, 0, 3, 0)

    End Sub
'----------------------End-Globals.vb

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call openWindow()
    End Sub

'-------------Opens Notepad Minimized
    Public Sub openWindow()

        Pid = Shell(shellPath,  AppWinStyle.MinimizedNoFocus)
        If Pid <= 0 Then
            MsgBox("Error starting the app")
            Exit Sub
        End If
        hWnd = MyApi.FindWindow(0&, windowName)
    End Sub

'-----------------Sends HELLO TO Notepad
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        AppActivate(Pid)
        delay(1)
        presskey(keys.VK_H) 'Print H
        presskey(keys.VK_E) 'Print E
        presskey(keys.VK_L) 'Print L
        presskey(keys.VK_L) 'Print L
        presskey(keys.VK_O) 'Print O

    End Sub
0
Comment
Question by:sean815
  • 8
  • 4
  • 2
14 Comments
 
LVL 19

Expert Comment

by:DreamMaster
Comment Utility
For that you will most likely need some code to get the process you need from the active processes. The way you are using it now, it won't work because the application has absolutely no idea that the application you need is actually running. As soon as it gets the focus though, it DOES know that..

Good luck,
Max.
0
 

Author Comment

by:sean815
Comment Utility
And how would I go about doing this?
Can I send commands via the procID?
0
 

Author Comment

by:sean815
Comment Utility
And how would I go about doing this?
Can I send commands via the procID?
0
 
LVL 19

Expert Comment

by:DreamMaster
Comment Utility
Normally that should be possible...

Applications like Notepad could be an exception to that rule, as you cannot type to a program while it is not in focus...same goes for when it's automated...

Max.
0
 

Author Comment

by:sean815
Comment Utility
Im just using notepad as an example, the program I will be controling is a third party terminal emulator.

Do you have any example code for sending command via a proc ID? Or any API's I should research?
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 200 total points
Comment Utility
If you can, you will want to enumerate child windows of the main application window (e.g., the actual text entry winow of Notepad) and find its handle such that you could send it text directly.  A sample showing how this was done prior to .NET:

http://www.thescarms.com/vbasic/PassString.asp
0
 

Author Comment

by:sean815
Comment Utility
that sample does what I'm looking for, however the recieve app is setup to do so. I don't have that luxury because the app Im sending data to is already a compiled 3rd party app.  I cant make it copy memory locations to grab my text.

Damn, This is getting diffucult :)
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
Perhaps that sample was too complicated...as a test, I found the handle to the text area in an open instance of Notepad and simply did:

Private Sub Command1_Click()
    Dim str1    As String
   
    str1 = "Testing"
   
    MsgBox SendMessage(394916, WM_SETTEXT, ByVal 0, ByVal str1)
End Sub


and the string "Testing" appeared in the Notepad application.
0
 

Author Comment

by:sean815
Comment Utility
But was notepad minimized?

I will try it again to see. Will let you know tomorrow.
0
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
I hadn't tried it minimized because it should not have been an issue.  I tried it again just now with Notepad minimized and it worked just fine.

It is not an issue because you are sending a system level message directly to the text entry window based on its window handle...that text entry should handle the message regardless of whether his parent container (be it, in this case, a "form") is minimized or not.
0
 

Author Comment

by:sean815
Comment Utility
I was succesful sending strings to notepad while in a hidden state. This was what I was looking for.


Thanks AzraSound!
0
 

Author Comment

by:sean815
Comment Utility
Although you fulfilled this question, maybe you can point me in the right direction to send a "Enter" command via Sendmessage. I can send the data string but I cant figure out how to drop down a line using a Enter command.
0
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
Try sending vbCrLf as part of your string
0
 

Author Comment

by:sean815
Comment Utility
AzraSound, once again you are right :)  Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now