Hi George,
Keyboard input introduces the idea of focus. In Windows, a particular
element is designated as having the focus, meaning that it acts as the
target for keyboard input. The user sets the focus by clicking the mouse or
tapping the stylus on the control in question, or by using navigation keys
such as the Tab and cursor keys.
In principle, any user interface element can receive the focus. The
IsFocused property is defined on UIElement, the base class of
FrameworkElement. However, the Focusable property determines whether this
feature is enabled on any particular element. By default, this is true for
controls and false for other elements.
In your practice, neither the Rectangle nor the Polyline is control, so
they cannot receive focus and the KeyDown or KeyUp event.
To trap the KeyUp event, you can do it in the window level. You can also
trap the MouseDown event in the window and set the current "selected"
element. If the Delete key is pressed, remove the current selected element
from the Canvas. For example:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.MouseDown += new
MouseButtonEventHandler(Window1_MouseDown);
this.KeyUp += new KeyEventHandler(Window1_KeyUp);
}
object currentElement = null;
void Window1_MouseDown(object sender, MouseButtonEventArgs e)
{
currentElement = e.OriginalSource;
}
void Window1_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
if (currentElement != null && currentElement is UIElement)
{
if (myCanvas.Children.Contains(currentElement as
UIElement))
{
myCanvas.Children.Remove(currentElement as
UIElement);
currentElement = null;
}
}
}
}
}
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.