Button click and virtual keyboard enter key Windows Phone 8 - c#

I am developing an app for windows phone. I have a login screen where the user must enter his username and click the LOGIN button in the UI or the enter key in the virtual keyboard of the phone. I capture both the events separately. LOGIN button has a 'Click' event which logs the user in and there is a 'KeyDown' event for the enter key in the virtual keyboard which has the same code as that of the Click event. The events work fine. It logs the user in once the login button or the enter key is pressed. But only when the login button or the enter key is pressed twice. The event gets captured in the first click (I saw the page being refreshed) but only the second click takes the user into the application. Any possible ideas to come out of this issue?
Earlier I did not have the LOGIN button, only used the enter key in virtual keyboard and things were working fine in the first click
Regards
Karthik

Just created simple example that worked fine, try to reproduce it.
Xaml:
<StackPanel Orientation="Horizontal">
<TextBox KeyDown="TextBox_KeyDown"/>
<Button Click="Button_Click" VerticalAlignment="Top"/>
</StackPanel>
Code-behind:
private void TextBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter || e.Key == Windows.System.VirtualKey.Accept)
HandleAll();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
HandleAll();
}
private void HandleAll()
{
//Hit breakpoint here
}
"HandleAll" method invoked every-time when Button or Keyboard Enter clicked.

I am not sure whether my approach was wrong. Theoretically if the logic of the HandleAll() method is present in both the Button_click and key_down event, it should give the same result (comparitevely poor performance though). Now I found that the problem is not due to using the same logic in both the events (without the HandleAll()) but because of the creation of new AppSettings in the windows phone. The following solves the issue for me:
private void Login_button_click(object sender, RoutedEventArgs e)
{
//Code
AppSettings settings = new AppSettings();
settings.IsLoggedOutSetting = false;
//Code
}
private void textbox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
//code
AppSettings settings = new AppSettings();
settings.IsLoggedOutSetting = false;
//code
}
}
With the above code , the issue is solved. So from my observation, the problem was because of not creating a new setting for the App when the user is trying to log in the windows phone app.
But the answer provided above by Vladimir would NOT have led me into this issue at the first place as well as the answer by Vladimir was efficient(space and time).
Thanks to you Vladimir. I was so curious why my earlier logic didn't work and found out the windows app needs a new setting to be setup when a user logs in through either the button_click or key_down events

Related

The web browser address bar makes Windows Asterisk sound every time I press enter

I'm trying to make a web browser, but the web browser address bar makes Windows Asterisk sound every time I press enter. I don't want the system to make that sound every time I press enter.
I didn't add a play sound code or something else, or the system think it's an error so it makes the sound? (Sorry, I'm new to .NET and C#)
Here are part my codes:
private void txtUrl_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
browser.Load(txtUrl.Text);
}
Maybe you can use KeyDown event to monitor "Enter".
private void txtUrl_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
// do some operation
}
Besides, you need to modify the TextBox properties, AutoCompleteMode and AutoCompleteSource.
txtUrl.AutoCompleteMode = AutoCompleteMode.Append;
txtUrl.AutoCompleteSource = AutoCompleteSource.HistoryList;

How to intercept Enter key in AutoSuggestBox?

'Enter' key in AutoSuggestBox calls to QuerySubmitted event, like clicking the icon. It is a Windows 10 project.
I need to discriminate the Enter key because I use it to go to the next field. I tried KeyDown event, but it is not called.
How can I do it?
Don't ask me why, but you need to use KeyUp event and everything works fine.
Ex:
private void ContactsBox_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
//some stuff here
}
}

Handle Mouse Click in a AutoComplete Combobox in C# Windows Forms

I have a ComboBox control in C# Windows forms project. I have set the DropDownStyle of the control to DropDown and have also configured AutoComplete properties to the control. This is working well.
On this control, I have a requirement when I press Enter after selecting a value, it should fire a button click. For this, I handled the keyDown event and checked if (e.KeyCode==Keys.Enter) which works well.
The problem is the following -
When I start typing the AutoComplete box opens up for the control and if I select one of them from mouse, the KeyDown event is fired and the system thinks that an "Enter" key is pressed.
CODE
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("hello");
}
private void cb_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1_Click(sender, e);
}
}
BEHAVIOR
I type B from keyboard and clicked on "B2" and got the alert.
Any ideas on what could be the issue? Happy to share more details if required.

KeyDown event handler is not working

I am developing an app for Windows 8.1
am using XAML + C#
I read the article this article in MSDN for Responding to keyboard interaction
I did as they say , but the problem is that the event occurs only when i press a key inside a TextBox
but i want the event to occur everywhere i press in the Page
Note: I use a laptop (no touch hardware)
XAML :
<Grid x:Name="GameGrid" Margin="0,0,0,0.111" KeyDown="Grid_KeyDown">
C# :
private void Grid_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.A)
this.DoSomething();
}
Try registering an accelerator key instead of a key event on grid (it must have focus to fire the event):
http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.core.coredispatcher.acceleratorkeyactivated
Example:
Window.Current.Dispatcher.AcceleratorKeyActivated += ...

KeyEvents only raised after first interaction

I have a WPF MainWindow and try to react to a certain key combination (CTRL + F4). I registered the following methods for testing purposes:
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
Log.AsInfo("PreviewKeyDown");
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
Log.AsInfo("KeyDown");
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
Log.AsInfo("KeyUp");
}
private void Window_PreviewKeyUp(object sender, KeyEventArgs e)
{
Log.AsInfo("PreviewKeyUp");
}
Crazy thing is, those methods are only triggered after I interact with the application for the first time:
How it does not work:
I start the application
I enter CTRL + F4 on the keyboard
Nothing happens
How it works:
I start the application
I click on a random menu item with no functionality
I enter CTRL + F4 on the keyboard
Everything works, log messages are written
Any ideas? I am not able to even debug the situation, because none of the handler methods is called in the first place. I even tried this.Focus() in the MainWindow constructor, but this did not help either.
Found the solution:
The browser control I used is the EO.WebBrowser. It seems, that this browser control swallows every first time key interaction. I implemented the shortcut combination by binding the functionality I needed to a hotkey of the WebBrowser control as mentioned here. So it was more of a third party than a WPF problem.

Categories