Windows Phone Keyboard Back-key press event? - c#

I am working on a windows phone application.
I noticed that after I show up a textbox and a keyboard, if I press physical "back" key, the keyboard will hide, and press again it will go back to previous page.
But I want the page to navigate to previous page when the "back" key is press the first time. So its like the keyboard is up, when I press back key, it directly go back to the previous page.
I tried to implement the BackKeyPressed event handler of the page but it does not seem work, since it is not called when the first time back key is pressed.
Anyone has any good idea with this? thank you

You can implement a KeyUp eventhandler on the inputbox and then detect for Escape key event before calling NavigationService.GoBack(). Here is a piece of code to illustrate my point and the effect you are describing above:
The XAML (SecondPage.xaml):
<TextBox x:Name="SomeTextBox" KeyUp="SomeTextBox_KeyUp"/>
The C# Code-behind:
private void SomeTextBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
NavigationService.GoBack();
}
}

you can implement the LostFoucs event handler of theTextBox,navigate to the page which you want in the handler.

Related

Assign a Keyboard key to a button click WINFORM

I am developing a small application with some buttons and textbox. What I am having problem is assigning a keyboard key (e.g. F3) to a button click.
For example if the user click the button Cash the code I wanted it's executed fine, but I want to make more easier instead clicking the button with mouse, I want the user be able to press the key on keyboard. I used the keydown event, also keypress event of that button, but still nothing.
I tried this keydown event
if (e.KeyCode == Keys.Enter)
{
btncash.PerformClick()
}
But still nothing
Do not use F3 function button it's used by OS for activating search. Enter key is fairs click event on focused control so do not use this also. Implement as suggested below.
In your Main form
Set KeyPreview to True in form load event.
Add KeyDown event handler with the following code
private void Form1_KeyDown(object sender,
KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.H)
{
btncash.PerformClick();
//btncash_Click(null, null);
}
}

Canceling the SpaceBar Click/Tap on uwp controls throughout the app

When a UI element ( control ) is focused in a uwp app it can be triggered with Spacebar or the Enter keys, this is not limited to Desktop but also helps in Xbox so user can navigate through the controls and press on any focused control to active its Command.
Use case
But in My use case I want only Enter key to trigger that behaviour and Spacebar should not do anything at all no matter which control is pressed on the screen.
The reason to this requirement is that I am building a MediaPlayer application and no matter which control or button is focused within the app when I press Spacebar I want to simply link it to the Play/Pause Behaviour of my media element.
Not a Duplicate
This question is not a duplicate of : UWP - Don't fire Click event when pressing space (like in Movies & TV app)
Because in the question linked above, the answer was only relevant if any of the AppBarButtons were focused so they will not do anything on pressing Space but only will be invoked with Enter. But in my use case I want to apply the same behavior even outside the MediaPlayerElement control. I have a NavigationView and MediaPlayerElement resides in one of the pages, so I want this behavior to work even when a NavigationViewItem is focused or any other control which can be focused and invoked should only be invoked with Enter and not Space.
Is there a app level solution where I can apply this behaviour at the very root control and it descends to all of its children i.e : whole app?
What I have tried
I have tried with the already answered question (linked above) and that works fine for its limited scenario. And I have also tried setting AllowFocusOnInteraction=false to every app bar button and also other extra controls I have in the style of my CustomMediaTransportControls. But this is also limited to MediaPlayerElement only and also it prevents tab navigation which is not good for accessibility.
You can do this by handling the PreviewKeyDown event higher in the visual tree hierarchy, for example in the Page.
Subscribe to the event in the Page constructor:
public MainPage()
{
this.InitializeComponent();
this.PreviewKeyDown += MainPage_PreviewKeyDown;
}
Or in XAML:
<Page ... PreviewKeyDown="MainPage_PreviewKeyDown">
And in the event handler set the KeyRoutedEventArgs to handled when the Space key was pressed:
private void MainPage_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Space)
{
e.Handled = true;
}
}
This way the key down event will never reach any control below in the hierarchy because the PreviewKeyDown event propagates the tree before the event takes place.
There are many ways could approach, You could listen the current Content PreviewKeyDown event to detect Space press.
public Scenario1()
{
this.InitializeComponent();
Window.Current.Content.PreviewKeyDown += Content_PreviewKeyDown;
}
private void Content_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
e.Handled = e.Key == VirtualKey.Space ? true : false;
}
You could also GLOBAL HOTKEY for your uwp app that could be used when your app's window not in foreground. For more derail you could check this blog .

Omit action when pressing Enter on button

Is there a way to omit the (whatever) performed action on a button when the key is pressed? I already got some good inputs from SO and tried to catch the event via OnKeyPress or OnKeyDown - but I could not omit the Enter-action. I also liked the idea of disabling the Tabstop property, but as soon as I click the button the problem reoccurs.
OnKeyDown only seems to fire when "casual" keys (like a or z) are pressed, but not when Enter is hit. How can I detect it - when Enter is hit - on the button itself?
When you press ENTER inside a non-multiline field on a form with a default button set, the click event for this button is fired, like a click, not like a keyevent (keypress, keyup, keydown, etc).
Form.AcceptButton
Gets or sets the button on the form that is clicked when the user
presses the ENTER key.
If you don't want this behaviour (or want to prevent it), maybe you should remove it as the default button.
But if you need it to be default only in certain situations, you can bind/unbind it programatically:
form1.AcceptButton = btnSave;
form1.AcceptButton = null;
You can use : if(e.KeyCode == Keys.Enter){}
Check the Button.IsDefault property
Well, the fact that
When you press ENTER [...] the click event for this button is fired
made me think about the use of the OnMouseUp(MouseEventArgs e) method instead of OnClick(EventArgs e). I gave it a shot and it totally fits my requirements - plus: it gives me the opportunity to only exclude the wrapped buttons from said issue, not involving any other controls in my application (like Form.AcceptButton).
Thanks for all your input, which made me think about the issue from another perspective!

press Enter key instead of clicking on a button in c#

as you can understand from the title that how to when you press Enter key the button automatically work, how to write code in c# for pressing Enter key instead of clicking on button?
Thank you!!
Is this WinForms? If so, you can set the AcceptButton of the form to be the button in question. Doing so will make pressing Enter behave exactly like clicking the button with the mouse, but it will only have that effect if the currently focused element isn't something else that will also capture the keypress.
I had been trying to solve the same problem. When a button had focus, hitting the enter key did not result in the KeyDown or KeyPress event firing. However, the KeyUp event fired. Problem solved.
private void CmdNoP_KeyUp(object sender, KeyEventArgs e) {
// I do not understand why this works
if (e.KeyCode == Keys.Return) {
cmdNoP_MouseClick(sender, null);
}
}
I believe, [space] is default key, which would "click" active button (or check checkbox and so on). You can always write method for events like OnKeyDown, OnKeyUp or similar. In these events you can check pushed key and do, whats clicking button is doing if this key is [enter].
For WPF set IsDefault="True" in XAML or use the button properties to set IsDefault checkbox.

create textbox one by one on enter keypress event

am using c# vs-2005
am on project to create textbox one by one on form1 and am success on button click event my code is below.
// declare location point of textbox on Global Area.
private point txtboxstartpoint=new point(10,10);
private void button_click (Object sender,EventArgs)
{
Textbox tbx = new TextBox();
tbx.Location= txtboxstartpoint;
tbx.size=new size (200,20);
this.panel1.control.add(tbx);
txtboxstartpoint.y += 25;
}
this works fine on button click event but problem is on keypress event like on enter
i wants to create textbox on enter one by one. and for that i assume that any method have
to create and call enter keypress event on newly created control like textbox to create
another new textbox below the previous one.
Kindly help me. suggest proper code for the same.
It's very hard to understand your question, but let's try some guessing:
You have a form, and if a user presses some specific key, you'd like to create a new TextBox and show it on your form regardless which control has currently the focus in your form.
If this statement is true, you can set Form.KeyPreview to true. And add an event handler to Form.KeyDown.
Due to the fact, that you set the preview to true you'll get every keyboard hit before it will be give further to the control that has currently the focus. So here you can check if the key that was pressed is the one you're listening for. And if yes, just call your TextBoxFactory and set the e.Handled to true to prevent that this key stroke will additionally reach the currently focused control.
I use the KeyDown event, to intercept the 'F1' key to provide my small help in a very small programm. Here is the code:
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F1)
{
//Your Code here
}
}

Categories

Resources