views:

376

answers:

1

My touch screen keyboard is highly customizable interface and has every component I need except for sending keys, anyone see a problem with this. Originally when I was creating it I was going to use the Forms.SendKeys.Send() but it's a WPF application... no go. For starters VB.Net in its infinite wisdom decided it would not handle default form messages. Go figure.

Or perhaps, this is my real problem, I can't get the WPF application to stop getting focus. I want it to act like the windows touch keyboard, but Every action that occurs in the transparent WPF makes that application the active one. I want events to still occur but I need the active window to be the window you wish to type into, like notepad.

Any Suggestions on what I should do, to make my WPF not focusable and to send keyboard buttons to the focused (or other) window?

PS, I'm using WPF in Visual Studio 2010 in Vb.Net (I can use C# Code to!)

+1  A: 

I think you will find the answer here usefull

There are a number of compilcated ways to achieve this, but the solution I posed that the above link is easy and only has one quirk, that to be honest can be worked around. When dragging the input window it does not provide feedback until the move is completed, but you can work around this by handling some non-client messages, I can spend sometime look at the work around if you need, but first confirm this solution is right for you.

Update: A sample of how a the approach above can be applied to a WPF form.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Runtime.InteropServices;

namespace WpfApplication1
{
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();
    }

    const int WS_EX_NOACTIVATE = 0x08000000;
    const int GWL_EXSTYLE = -20;

    [DllImport("user32", SetLastError = true)]
    private extern static int GetWindowLong(IntPtr hwnd, int nIndex);

    [DllImport("user32", SetLastError = true)]
    private extern static int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewValue);

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
      WindowInteropHelper wih = new WindowInteropHelper(this);
      int exstyle = GetWindowLong(wih.Handle, GWL_EXSTYLE);
      exstyle |= WS_EX_NOACTIVATE;
      SetWindowLong(wih.Handle, GWL_EXSTYLE, exstyle);
    }    
  }
}
Chris Taylor
This is exactly what I was going to try, it works well in the Windows Form Environment. In fact I'm thinking of going back to a windows form, but in WPF The CreateParams override does not exists. Since its of type System.Windows.Window instead of System.Windows.Forms.Form. My life would be so much easier if I did not want to use the new presentation layer. Its much more finicky than a windows form..., but it is the future.
Justin
@Justin, you can still apply the same principal to WPF. You just need to set the style after creation. I will post a quick sample shortly.
Chris Taylor
I've adapted it and it works fine, it allows the focus to remian on the other window while action is used. Thank you, this has solved one of my many problems. Now I just need to get it to send the keyboard key action to the other window. I tried SendKeys() no luck, I tried using the user32 dll to get the keys to send, no luck. I was using this example: http://pinvoke.net/default.aspx/user32/SendInput.html/ Any ideas? I'll keep trying but, I'm drawing a blank here.
Justin
@Justin, When I implemented the sample I tested with SendKeys.SendWait and it worked for me, did you try this?
Chris Taylor
I'll try send wait see what that does, now that I have the window not getting set to focus, I'll see if it goes well.
Justin
Thank you! That worked. Mabe I'll post a link to the source when its finished, thanks again.
Justin
@Justin, Great! I am glad that helped.
Chris Taylor