views:

38

answers:

2

Consider a XAML TextBox in Win Phone 7.

  <TextBox x:Name="UserNumber"   />

The goal here is that when the user presses the Enter button on the on-screen keyboard, that would kick off some logic to refresh the content on the screen.

I'd like to have an event raised specifically for Enter. Is this possible?

  • Is the event specific to the TextBox, or is it a system keyboard event?
  • Does it require a check for the Enter on each keypress? i.e. some analog to ASCII 13?
  • What's the best way to code this requirement?

alt text

+2  A: 

Hi P.Campbell,

A straight forward approach for this in a textbox is

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        Debug.WriteLine("Enter");
    }
}
Mick N
+1  A: 

You'll be looking to implement the KeyDown event specific to that textbox, and checking the KeyEventArgs for the actual Key pressed (and if it matches Key.Enter, do something)

<TextBox Name="Box" InputScope="Text" KeyDown="Box_KeyDown"></TextBox>

private void Box_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key.Equals(Key.Enter))
    {
        //Do something
    }
}

Just a note that in the Beta version of the WP7 emulator, although using the software on-screen keyboard detects the Enter key correctly, if you're using the hardware keyboard (activated by pressing Pause/Break), the Enter key seems to come through as Key.Unknown - or at least, it was doing so on my computer...

Blakomen