Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Keyboard Commands To Minimized Window

Posted on 2002-06-25
14
428 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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
 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 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