Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 434
  • Last Modified:

Keyboard Commands To Minimized Window

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
sean815
Asked:
sean815
  • 8
  • 4
  • 2
1 Solution
 
DreamMasterCommented:
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
 
sean815Author Commented:
And how would I go about doing this?
Can I send commands via the procID?
0
 
sean815Author Commented:
And how would I go about doing this?
Can I send commands via the procID?
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
DreamMasterCommented:
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
 
sean815Author Commented:
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
 
AzraSoundCommented:
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
 
sean815Author Commented:
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
 
AzraSoundCommented:
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
 
sean815Author Commented:
But was notepad minimized?

I will try it again to see. Will let you know tomorrow.
0
 
AzraSoundCommented:
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
 
sean815Author Commented:
I was succesful sending strings to notepad while in a hidden state. This was what I was looking for.


Thanks AzraSound!
0
 
sean815Author Commented:
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
 
AzraSoundCommented:
Try sending vbCrLf as part of your string
0
 
sean815Author Commented:
AzraSound, once again you are right :)  Thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now