Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining the Enter key is pressed in a TextBox

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

like image 555
p.campbell Avatar asked Jul 20 '10 22:07

p.campbell


People also ask

How do you check if the Enter key is pressed?

Check the event. And the following JavaScript code to detect whether the Enter key is pressed: const input = document. querySelector("input"); input. addEventListener("keyup", (event) => { if (event.

How do you check if Enter key is pressed in asp net?

You can use the Panel. DefaultButton property to automatically click a button when the user presses Enter. If you don't want to show the button, you can set its style="display: none" . This is the best solution IMO.

Which syntax can be used to check if the key is still pressed or not?

The keyIsDown() function checks if the key is currently down, i.e. pressed.


2 Answers

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");     } } 
like image 175
Mick N Avatar answered Sep 20 '22 13:09

Mick N


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...

like image 29
Henry C Avatar answered Sep 20 '22 13:09

Henry C