Skip to main content

Microsoft Silverlight

Answered Question KeyDown event for Silverlight User ControlRSS Feed

(0)

RootCanvas
RootCanvas

Member

Member

10 points

8 Posts

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,

 

mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

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!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


FredHirschfeld
FredHirs...

Participant

Participant

928 points

165 Posts

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")

RootCanvas
RootCanvas

Member

Member

10 points

8 Posts

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, 

 

FredHirschfeld
FredHirs...

Participant

Participant

928 points

165 Posts

Answered Question

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")

RootCanvas
RootCanvas

Member

Member

10 points

8 Posts

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.

hammadmirza
hammadmirza

Member

Member

324 points

107 Posts

Answered Question

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)

forci
forci

Member

Member

351 points

275 Posts

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.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities