Solved

Keyboard Commands To Minimized Window

Posted on 2002-06-25
14
426 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
ID: 7107801
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
ID: 7108148
And how would I go about doing this?
Can I send commands via the procID?
0
 

Author Comment

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

Expert Comment

by:DreamMaster
ID: 7108339
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
ID: 7108404
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
ID: 7108782
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
ID: 7110426
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 28

Expert Comment

by:AzraSound
ID: 7110535
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
ID: 7111765
But was notepad minimized?

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

Expert Comment

by:AzraSound
ID: 7111827
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
ID: 7112588
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
ID: 7112593
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
ID: 7112667
Try sending vbCrLf as part of your string
0
 

Author Comment

by:sean815
ID: 7114028
AzraSound, once again you are right :)  Thanks!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

914 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

21 Experts available now in Live!

Get 1:1 Help Now