Canvas KeyDown event<p>I can't able to get the keyword events of the canvas....</p> <p>Any help would be appreciated.</p> <p>Thanks in advance</p> <p>Cheers,</p> <p>G</p>© 2009 Microsoft Corporation. All rights reserved.Thu, 19 Jun 2008 00:15:52 Zd1d03877-e2fb-4df1-b0c5-818625fd8aa0http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/d1d03877-e2fb-4df1-b0c5-818625fd8aa0#d1d03877-e2fb-4df1-b0c5-818625fd8aa0http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/d1d03877-e2fb-4df1-b0c5-818625fd8aa0#d1d03877-e2fb-4df1-b0c5-818625fd8aa0A.Kahnhttp://social.msdn.microsoft.com/Profile/en-US/?user=A.KahnCanvas KeyDown event<p>I can't able to get the keyword events of the canvas....</p> <p>Any help would be appreciated.</p> <p>Thanks in advance</p> <p>Cheers,</p> <p>G</p>Fri, 07 Jul 2006 15:57:35 Z2006-07-10T11:17:26Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/d1d03877-e2fb-4df1-b0c5-818625fd8aa0#e3d0b368-2be7-4146-b8f0-8d9718f5e38fhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/d1d03877-e2fb-4df1-b0c5-818625fd8aa0#e3d0b368-2be7-4146-b8f0-8d9718f5e38fKeith Boyd -MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Keith%20Boyd%20-MSFTCanvas KeyDown eventYou need to have an element that can accept keyboard input within your<br />Canvas for the KeyDown event to occur. I'm able to get the KeyDown event<br />to fire by doing the following:<br /> <br /><br />private void CreateAndShowMainWindow()<br /><br />{<br /><br />Canvas myCanvas = new Canvas();<br /><br />myCanvas.KeyDown += new<br />System.Windows.Input.KeyEventHandler(myCanvas_KeyDown);<br /><br />TextBox myTextBox = new TextBox();<br /><br />myTextBox.Width = 100;<br /><br />Canvas.SetTop(myTextBox, 300);<br /><br />Canvas.SetLeft(myTextBox, 25);<br /><br />myCanvas.Children.Add(myTextBox);<br /><br />}<br /><br />void myCanvas_KeyDown(object sender, System.Windows.Input.KeyEventArgs<br />e)<br /><br />{<br /><br />MessageBox.Show("keydown event occurred"); <br /><br />}<br /><br />If you just have UIElements that can't receive user input (like a<br />TextBlock or other UIElement) and you try typing anywhere in the Canvas<br />the event doesn't get raised.<br /> <br />Good luck.<br />-Keith<br /><br />Fri, 07 Jul 2006 16:33:02 Z2006-07-07T16:33:02Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/d1d03877-e2fb-4df1-b0c5-818625fd8aa0#08fd0a4c-cfa8-486c-8f76-31831097998fhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/d1d03877-e2fb-4df1-b0c5-818625fd8aa0#08fd0a4c-cfa8-486c-8f76-31831097998fviliescuhttp://social.msdn.microsoft.com/Profile/en-US/?user=viliescuCanvas KeyDown eventYou can do that by setting Focusable=&quot;True&quot; on the canvas and set the focus on the Canvas from the start (myCanvas.Focus() somewhere in the window's constructor or loaded event).<br>The problem is that if another element receives focus during the application's lifetime, you will not receive the keyboard events on the canvas.<br><br>Another way is to set the event handler up in the hierarchy (in the parent Window) - because of the tunneling/bubbling you will get the event no matter where the focus is in the window. I think in this case PreviewKeyDown is even better - you will get the event before any child will process it.<br>Fri, 07 Jul 2006 17:49:29 Z2006-07-10T11:17:26Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/d1d03877-e2fb-4df1-b0c5-818625fd8aa0#e1c670b2-5a28-4b3f-a789-0020b2f2a12chttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/d1d03877-e2fb-4df1-b0c5-818625fd8aa0#e1c670b2-5a28-4b3f-a789-0020b2f2a12cA.Kahnhttp://social.msdn.microsoft.com/Profile/en-US/?user=A.KahnCanvas KeyDown eventthanks guys :) your comments really help me to solved the problem.<br>Cheers<br>Mon, 10 Jul 2006 11:19:39 Z2006-07-10T11:19:39Z