4 Replies - 5284 Views - Last Post: 17 April 2015 - 04:27 AM Rate Topic: -----

#1 Zuher Laith  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 133
  • Joined: 10-February 15

Sendkeys to Selected active Application

Posted 16 April 2015 - 05:02 PM

Hello Everyone..
i have a question about sending keys to selected active application

Firstable ,, the Active application title is in combo box or something else..
Here is what i tried:
    ''' <summary>The GetForegroundWindow function returns a handle to the foreground window.</summary>
    ''' <returns>The return value is a handle to the foreground window. The foreground window can be NULL in certain circumstances, such as when a window is losing activation. </returns>
    <DllImport("user32.dll", SetLastError:=True)> _
    Private Shared Function GetForegroundWindow() As IntPtr
    End Function

    <DllImport("user32.dll", SetLastError:=True)> _
    Private Shared Function GetWindowThreadProcessId(ByVal hwnd As IntPtr, _
                      ByRef lpdwProcessId As Integer) As Integer
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        KeySender()
    End Sub

    Private Sub KeySender()
        While (True)

            Dim fgWin = GetForegroundWindow()
            Dim fgPid As New Integer()
            GetWindowThreadProcessId(fgWin, fgPid)

            Dim proc = System.Diagnostics.Process.GetProcessById(fgPid)
            If (proc.ProcessName = SelectedApp.Text) Then
                SendKeys.Send("a")
            End If

            System.Threading.Thread.Sleep(1000)
            End While
    End Sub


the Problem:
it's not working when the Active Application is Minimized..

Someone can Help?
Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Sendkeys to Selected active Application

#2 djjeavons  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 114
  • View blog
  • Posts: 417
  • Joined: 09-January 09

Re: Sendkeys to Selected active Application

Posted 17 April 2015 - 12:29 AM

Hi

I haven't looked at your code in detail but unless I am grossly mistaken, how will a minimised application be active?
Was This Post Helpful? 0
  • +
  • -

#3 maceysoftware  Icon User is offline

  • Foo
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,530
  • Joined: 07-September 13

Re: Sendkeys to Selected active Application

Posted 17 April 2015 - 02:39 AM

Hello Zuher Laith

djjeavons is completely right, a minimised application can never be in focus, so the question was not correct. the correct question is how do i make a window that is minimised to be the active application. the code below shows how you can do this.

HOWEVER

from the code you have sent i would STRONGLY discourage you doing it this way. this is because your code will just lock the UI thread up meaning nothing else can happen on your form. including actually closing the application.

when i was testing the code below in the format that i strongly discourage it wasn't just the application that was being locked but my whole computer.

in my second example i show you how i recommend doing this using a thread and calling back to the calling thread (in this case the UI) thread to do the processing.

Spoiler


Thread Example

Spoiler

Was This Post Helpful? 0
  • +
  • -

#4 Zuher Laith  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 133
  • Joined: 10-February 15

Re: Sendkeys to Selected active Application

Posted 17 April 2015 - 04:26 AM

look @maceysoftware
So The Code is all working ,
but still the question >> is it possible to sendkey when the window is minimized or not focused?
because i saw a program (MurGee Auto Keyboard) Can do this , But it's not free...
& waiting ..
Thanks for help ..^^
Was This Post Helpful? 0
  • +
  • -

#5 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1490
  • View blog
  • Posts: 3,771
  • Joined: 01-February 13

Re: Sendkeys to Selected active Application

Posted 17 April 2015 - 04:27 AM

It will all depend on the window that you want to send the key(s) to. Not every window handles the keys the same way. It will also depend on what key(s) you want to send to the window.

As has been mentioned, a minimized window can not be the foreground window so, do you want the window to stay minimized when you send the key(s) or do you want it to be restored and then send the key(s)?

If you want it to stay minimized when you send the key(s) then your life just got a lot more difficult. Depending on the window you may be able to just use the Process class to get the main window handle and send certain keys to it using SendMessage or PostMessage.

However, if it is a window like Notepad and you want to send text to it then you need to get the handle of the "Edit" child window. Then you can actually set the text of the window instead of sending keys. Doing any of this while the window is minimized will require the use of a few different Win32 Api`s and a fairly good knowledge of how to use them.

If you want the window to be restored to the foreground window then you just made your life a little easier.

So, can you explain what window (program) you want to send key(s) to or if it is actually a text string instead of just a key or two? If it can be any program window then explain how you would know what key(s) or text you want to send to it.

Also, explain if it should be restored to the foreground or if it should stay minimized. There are a lot of variables you have not explained which each variable will or may change the way you need to do it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1