1

Possible Duplicate:
Create an On-screen Keyboard

I'm trying to write a virtual keyboard. Can you tell me how I can get the descriptor hWnd of the focused window? (It can be for Word, Excel, Skype, etc.)

I'm using findWindow(), but for that I must know the name of the window.

IntPtr hWnd = FindWindow("Notepad", null);

        if (!hWnd.Equals(IntPtr.Zero))
        {
            MessageBox.Show("Tagil");
            IntPtr edithWnd = FindWindowEx(hWnd, IntPtr.Zero, "Edit", null);
            if (!edithWnd.Equals(IntPtr.Zero))
                SendMessage(hWnd, WM_SETTEXT, IntPtr.Zero, new StringBuilder("Hello World"));
        }
Community
  • 1
  • 1
Abbath
  • 1,002
  • 13
  • 30
  • are you wanting to get the name / handle of the Foreground Window..? – MethodMan Feb 14 '12 at 19:00
  • yes but i think not Foreground because foreground is my Apllictation. I want to get name or handing other window – Abbath Feb 15 '12 at 08:35
  • Would't it be best to work with the Windows [Accessability API](http://msdn.microsoft.com/en-us/library/windows/desktop/gg712214.aspx) to provide the extra keyboard? – Deanna Aug 09 '12 at 08:32

3 Answers3

2

For what it's worth, this is very likely the wrong approach to writing a virtual keyboard; you'd be better off to use SendInput to inject the keystrokes, and let Windows/USER32 handle routing the input to the current focused window itself - this way you don't even need to know the current focused window in the first place.

One issue is that while Edit/Richedit control will use WM_SETTEXT, many other real-world editable controls - like Word, Excel and so on - don't. Also, you can't use WM_SETTEXT to send arrow keys or other non-textual content.

If you still really need to find the current focused HWND, you can use GetGUIThreadInfo, passing 0 for the idThread, and then use the hwndFocus member of the GUITHREADINFO struct that is returned.

BrendanMcK
  • 14,252
  • 45
  • 54
0

HWND WINAPI GetForegroundWindow(void);

Retrieves a handle to the foreground window (the window with which the user is currently working). The system assigns a slightly higher priority to the thread that creates the foreground window than it does to other threads.

HWND WINAPI GetActiveWindow(void);

Retrieves the window handle to the active window attached to the calling thread's message queue.

One of those will probably do it.

vmpstr
  • 5,051
  • 2
  • 25
  • 25
0

if you want a more complex example here you can look at this as well

See example on how you can do this with full source code here:

http://www.csharphelp.com/2006/08/get-current-window-handle-and-caption-with-windows-api-in-c/

[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

private string GetActiveWindowTitle()
{
    const int nChars = 256;
    IntPtr handle = IntPtr.Zero;
    StringBuilder Buff = new StringBuilder(nChars);
    handle = GetForegroundWindow();

    if (GetWindowText(handle, Buff, nChars) > 0)
    {
        return Buff.ToString();
    }
    return null;
}
BrianH
  • 7,932
  • 10
  • 50
  • 71
MethodMan
  • 18,625
  • 6
  • 34
  • 52