By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,198 Members | 1,698 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,198 IT Pros & Developers. It's quick & easy.

RE: wpf: Trap keyup event in canvas

P: n/a
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.
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Thanks Linda,

Even though Chris's suggestion worked very good, I ended up using a
variation of your suggestion below because I also had to create a collection
of selected UIElements (for when the user wants to select more than one item
at a time). Then based on the window's key up event, I could delete
everything in the collection.
"Linda Liu[MSFT]" <v-****@online.microsoft.comwrote in message
news:uC**************@TK2MSFTNGHUB02.phx.gbl...
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.


Jun 27 '08 #2

P: n/a
Hi George,

Thank you for your feedback! I'm glad to hear the problem is solved now.

If you have any other questions in the future, please don't hesitate to
contact us. It's always our pleasure to be of assistance!

Have a nice day!

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.

This posting is provided "AS IS" with no warranties, and confers no rights.
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.