[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

WPF question --- getting keyboard events

John Mott

7/2/2008 6:06:00 PM

Hi all,

If this isn't the right newsgroup it would be great to get a pointer to the
correct one.

I can't get keyboard events in a trivial WPF application, the keydown event
doesn't seem to be fired. The xaml is

<Grid Keyboard.KeyDown="Grid_KeyDown">
</Grid>

and the handler is

private void Grid_KeyDown(object sender, KeyEventargs e)
{
string s = "set a breakpoint here";
}

when i set a breakpoint on the handler it never fires when I type.

Ideas?

thanks,

john


2 Answers

John Mott

7/2/2008 6:21:00 PM

0

Never mind, i found it. I had searched before but missed it. The 'focusable'
property has to be set on an object (like a canvas) before it will get
keyboard events.


"John Mott" <johnmott59@hotmail.com> wrote in message
news:OJjMG5G3IHA.1436@TK2MSFTNGP05.phx.gbl...
> Hi all,
>
> If this isn't the right newsgroup it would be great to get a pointer to
> the correct one.
>
> I can't get keyboard events in a trivial WPF application, the keydown
> event doesn't seem to be fired. The xaml is
>
> <Grid Keyboard.KeyDown="Grid_KeyDown">
> </Grid>
>
> and the handler is
>
> private void Grid_KeyDown(object sender, KeyEventargs e)
> {
> string s = "set a breakpoint here";
> }
>
> when i set a breakpoint on the handler it never fires when I type.
>
> Ideas?
>
> thanks,
>
> john
>
>


Prof Von Lemongargle

8/10/2008 7:04:00 AM

0

I also had this issue and once you find focusable, there is decent help on
MSDN. Besides setting Focusable to True, Visible must be true, and you must
set keyboard focus to the item you want to get the event. In XAML, you can
use Loaded="OnLoad" for your page and then in OnLoad you can use
Keyboard.Focus(<UI element>);

Once an element is focusable and has Keyboard focus, it seems to work well.