Page view counter
KeyDown event for Silverlight User Control Subscribe to this thread
Last post 10-21-2008 3:47 AM by forci. 7 replies.
Sort Posts:
02-06-2008 12:59 PM
KeyDown event for Silverlight User Control

Hi,

I have tried several ways to track the KeyDown event on a SL User Control but not success. Does anyone can know how can we capture the key events of a user control?

Thanks,

 

RootCanvas

Loading...
Joined on 02-06-2008
Posts 8
02-06-2008 3:22 PM
Re: KeyDown event for Silverlight User Control

Hello,

AFAIK, Key input events (KeyDown and Keyup)  works only for root canvas (e.g. Canvas from Page.xaml).  I think it won't work for user control.

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Best Regards,
Michael Sync

Microsoft WPF & Silverlight Insider
Blog : http://michaelsync.net


mchlsync

Loading...
Joined on 09-16-2005
Singapore
Posts 2,478
02-06-2008 10:02 PM
Re: KeyDown event for Silverlight User Control

You will need to implement some additional logic to support bubbling the events to your controls... from the root canvas.

 
Fred Hirschfeld
(If this answers your question please select "Mark as Answered")

FredHirschfeld

Loading...
Joined on 02-03-2008
Tacoma
Posts 159
02-07-2008 12:34 PM
Re: Re: KeyDown event for Silverlight User Control

Do you have any idea about how to do this, ie as you say need to implement some additional logic,

Thanks, 

 

RootCanvas

Loading...
Joined on 02-06-2008
Posts 8
02-07-2008 6:42 PM
Marked as Answer
Re: Re: KeyDown event for Silverlight User Control

I have created a FocusManager to manage the current focused control and then from the base Page class I push the events to the focus manager to send to the focused control. I also have a base control class that supports focus to manage that interaction as well.

 So your page would connect the events like:

KeyUp += new KeyboardEventHandler(HandleKeyUp);
KeyDown += new KeyboardEventHandler(HandleKeyDown);

Then the processing methods would call the appropriate FocusManager.Process***(e) to start processing the events. These handlers would simply detect if there is a focused control and if so send it the event. The focusing would simply be set when the user clicks on the control or you could get even more in depth and support tab ordering as well.

 
Fred Hirschfeld
(If this answers your question please select "Mark as Answered")

FredHirschfeld

Loading...
Joined on 02-03-2008
Tacoma
Posts 159
02-09-2008 1:24 PM
Re: Re: Re: KeyDown event for Silverlight User Control

Thank you both of you for replies. still i dont hv luck. could you pls give some sample code for this.

RootCanvas

Loading...
Joined on 02-06-2008
Posts 8
02-10-2008 12:55 PM
Marked as Answer
Re: Re: Re: Re: KeyDown event for Silverlight User Control

FredHirschfeld is right,

this is the only way of doing it

you can call a public method of your custom control e.g public void OnKeyDown(...)

and pass in the sender and eventargs , if the custom control is focused ( Mouse Over that control or MouseLeftButtonDown occured on it)

i also did it by this way

http://hammadmirza.wordpress.com
http://www.idraw.co.cc
(IF YOUR PROBLEM HAS BEEN ANSWERED OR SOLVED, MARK IT AS ANSWER)

hammadmirza

Loading...
Joined on 05-08-2007
Wah Cantt, PAKISTAN
Posts 107
10-21-2008 3:47 AM
Re: Re: Re: Re: Re: KeyDown event for Silverlight User Control

What if you have for root element Grid.

This behaviour is very strange for me. Only root canvas can receive keys events?

So if i understand correctly everything, in your UserControl, you must have for your root element Canvas and only this control can receive keys events?

Make love not war.

forci

Loading...
Joined on 05-10-2007
Slovenia
Posts 275
Microsoft Communities