question

CodeWanderer-1205 avatar image
0 Votes"
CodeWanderer-1205 asked ·

How to ignore key and pass it next to the window?

I display WPF UserControl inside HwndSource. I want ignore certain keys from keyboard and ignore that keys, but pass it next to the host window or host application. That means, if spacebar key is pressed, WPF UsuerControl ignore it, but host application detect this key. For now, WPF UserControl eat all key inputs if UserControl is focused.

Here is image to help what I want to do.
6983-untitled.jpg



For now keyboard event stop on WPF UserControl. And I want to pass it to Host Application.

windows-wpf
untitled.jpg (29.9 KiB)
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AlexLi-MSFT avatar image
0 Votes"
AlexLi-MSFT answered ·

Hi,

Welcome to our Microsoft Q&A platform.

Do you want uesrcontrol to respond to keyboard event when it is focused?You can remove hook ,see my example:

  public partial class MainWindow : Window
     {
         Dictionary<string, short> hotKeyDic = new Dictionary<string, short>();
         HwndSource hWndSource;
         public MainWindow()
         {
             InitializeComponent();
             this.Loaded += (sender, e) =>
             {
                 var wpfHwnd = new WindowInteropHelper(this).Handle;
                  hWndSource = HwndSource.FromHwnd(wpfHwnd);
                 if (hWndSource != null) hWndSource.AddHook(MainWindowProc);
    
                 hotKeyDic.Add("Alt-S", Win32.GlobalAddAtom("Alt-S"));
                 hotKeyDic.Add("Alt-D", Win32.GlobalAddAtom("Alt-D"));
                 Win32.RegisterHotKey(wpfHwnd, hotKeyDic["Alt-S"], Win32.KeyModifiers.Alt, (int)System.Windows.Forms.Keys.S);
                 Win32.RegisterHotKey(wpfHwnd, hotKeyDic["Alt-D"], Win32.KeyModifiers.Alt, (int)System.Windows.Forms.Keys.D);
             };
         }
         private IntPtr MainWindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
         {
             switch (msg)
             {
                 case Win32.WmHotkey:
                     {
                         int sid = wParam.ToInt32();
                         if (sid == hotKeyDic["Alt-S"])
                         {
                             MessageBox.Show("按下Alt+S");
                         }
                         else if (sid == hotKeyDic["Alt-D"])
                         {
                             MessageBox.Show("按下Alt+D");
                         }
                         handled = true;
                         break;
                     }
             }
    
             return IntPtr.Zero;
         }
    
         private void MyUserControl_GotFocus(object sender, RoutedEventArgs e)
         {
             hWndSource.RemoveHook(MainWindowProc);
         }
    
         private void MyUserControl_LostFocus(object sender, RoutedEventArgs e)
         {
             hWndSource.AddHook(MainWindowProc);
         }
     }
     public class Win32
     {
         [DllImport("User32.Dll")]
         public static extern void SetWindowText(int h, String s);
    
         [DllImport("user32.dll", SetLastError = true)]
         public static extern bool RegisterHotKey(
             IntPtr hWnd,
             int id,
             KeyModifiers fsModifiers,
             int vk
             );
    
         [DllImport("user32.dll", SetLastError = true)]
         public static extern bool UnregisterHotKey(
             IntPtr hWnd,
             int id
             );
    
     
         [DllImport("kernel32", SetLastError = true)]
         public static extern short GlobalAddAtom(string lpString);
    
         [DllImport("kernel32", SetLastError = true)]
         public static extern short GlobalDeleteAtom(short nAtom);
    
    
         [Flags()]
         public enum KeyModifiers
         {
             None = 0,
             Alt = 1,
             Ctrl = 2,
             Shift = 4,
             WindowsKey = 8
         }
       
         public const int WmHotkey = 0x312;
     }
     public class MyUserControl : UserControl
     {
    
     }

xaml:

  <local:MyUserControl Focusable="True" GotFocus="MyUserControl_GotFocus"  LostFocus="MyUserControl_LostFocus"/>

Thanks.

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.