UWP problems in detecting the "Enter" key pressed - c#

I'm trying to run some functions in my app when the Enter key is pressed on the keyboard, but I'm having problems in doing that.
KeyboardControl is in the KeyDown of my textbox.
Key.Enter is not recognized as a function, and I don't know what to do.
// When a key is pressed on the keyboard
private void KeyboardControl(object sender, KeyEventArgs e)
{
if (e.KeyStatus == Key.Enter)
{
PercentCalc();
PercentageValue.Text = Convert.ToString(result, new CultureInfo("en-US")) + "%";
}
}

Attach KeyDown event to your TexBox like this :
<TextBox KeyDown="Box_KeyDown" />
at the backend keydown event check if the pressed key is Enter and then execute your code in that if condition.
private async void Box_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{//execute code here
PercentCalc();
PercentageValue.Text = Convert.ToString(result, new CultureInfo("en-US")) + "%";
}
}
you were trying to check the KeyStatus which is not required in your usecase, instead you should be checking which key is pressed.

Related

c# listening to keyboard combination

I want to write simple program that send my user and password in some places for example when I want to log in to website and i found this project that listening to keyboard.
SO i have this function:
private void HookManager_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
}
private void HookManager_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
}
private void HookManager_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
}
And when i press key for example a i have this pice of result:
int value = e.KeyValue; // 65
Keys key = e.KeyCode; // A
So i wondoer how to catch specific keyboard combination and not only one key for example Ctrl + l ?
You can use code like this for KeyDown:
if(e.KeyCode == Keys.F1 && (e.Alt || e.Control || e.Shift))
{
}
Based on the KeyEventArgs documentation from MSDN:
A KeyEventArgs, which specifies the key the user pressed and whether any modifier keys (CTRL, ALT, and SHIFT) were pressed at the same time, is passed with each KeyDown or KeyUp event.
The KeyDown event occurs when the user presses any key. The KeyUp
event occurs when the user releases the key. Duplicate KeyDown events
occur each time the key repeats, if the key is held down, but only one
KeyUp event is generated when the user releases the key.
The KeyPress event also occurs when a key is pressed. A
KeyPressEventArgs is passed with each KeyPress event, and specifies
the character that was composed as a result of each key press.
Overriding the ProcessDialogKey() method is the generic solution:
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == (Keys.Control | Keys.I))
{
MessageBox.Show("Ctrl+I");
return true;
}
return base.ProcessDialogKey(keyData);
}

How do I make it so pressing Enter in a textbox does the same as clicking the button next to it?

I'm working on a C# based chat client in Windows Forms. I want to make it so a user won't always have to click the button when they want to send a message. Instead, hitting the Enter or Return key should be sufficient.
Here is a screnshot of my Form:
Handle KeyPress event for your textbox and code it like below in .cs page
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
MessageBox.Show("Your code here for enter key", "Enter Pressed");
}
}
You could create a Keydownevent for the textbox and ask for the enter key:
private void TextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1.PerformClick();
e.Handled = true;
e.SuppressKeyPress = true;
}
}
You can use the KeyPress event of the TextBox to know which key is pressed. When the Return key is pressed, programmatically click the Senden button.
Event Handler:
private void CheckEnter(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
// Enter key pressed
}
}
And register the event like this (or use the visual editor):
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckEnter);
Source
You can use your textbox keydown event to check whether you have pressed enter or not then call the submit button click event from there,
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
buttonsSubmit_Click(this, new EventArgs());
}
}

KeyUp event firing from next control

I've 5 buttons in my windows application. When I click arrow keys the focus changing between buttons, then only
KeyUp
event firing. How to stop this?
Subscribe to the PreviewKeyDown event instead.
Occurs before the KeyDown event when a key is pressed while focus is on this control.
As you move through the buttons, the sender parameter will contain the previously selected button.
I found a solution that should work for you, adapted from here. Apparently, MS made the decision that the arrow keys wouldn't trigger the KeyDown event, so you can't cancel them.
One workaround is to specify that your arrow keys are normal input keys, like any other key. Then the KeyDown event will fire and you can cancel the button press if you want.
private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right || e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
e.IsInputKey = true;
}
private void button1_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
}
You may want to read the other answers and comments in that post to see what would work best in your situation.
Answer for your question in comment
void button1_LostFocus(object sender, EventArgs e)
{
button1.Focus();
}
To prevent Up from moving focus from a Button you have to utilize at least 3 methods:
bool _focus;
private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Up)
_focus = true;
}
private void button1_KeyUp(object sender, KeyEventArgs e)
{
_focus = false;
}
private void button1_Leave(object sender, EventArgs e)
{
if(_focus)
button1.Focus(); // or (sender as Control)
}
Trick is to use flag when user press Up and to return focus in Leave. You have to unflag in KeyUp, otherwise it would be impossible to change focus (by pressing Tab to example).
You could possible unflag in Leave, I didn't test it.

C# - How to override actions for "Up arrow" and "Down arrow" for a textbox?

I have a textbox and below it i have a listbox.
While the user is typing in the textbox if he presses the up or down arrow he should make a selection in the listbox. The textbox detects all the characters (except space) but it seems that it can't detect the arrow presses.
Any solution for this? This is a WPF project btw.
EDIT, Here's the working code thanks to T.Kiley:
private void searchBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.IsDown && e.Key == Key.Down)
{
e.Handled = true;
//do your action here
}
if (e.IsDown && e.Key == Key.Up)
{
e.Handled = true;
//do another action here
}
}
I just tried this and it works. Add a preview key down event to the textbox
private void TextBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.IsDown && e.Key == Key.Down)
MessageBox.Show("It works");
}
You can listen to they KeyDown event of the TextBox. In the handler, check whether the arrow key was pressed (you might need to listen to key up to avoid triggering your code multiple times if the user holds down the button for too long).
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
// Do some code...
}
}

handling function key press

I have a C# form with 5 buttons. The users enters the information and depending on the press of a function key, a specific action is performed. F9-Execute Order, F6-Save, F3-LookUp.
I have added the foolowing code:
OnForm_Load
this.KeyUp += new System.Windows.Forms.KeyEventHandler(KeyEvent);
and
private void KeyEvent(object sender, KeyEventArgs e) //Keyup Event
{
if (e.KeyCode == Keys.F9)
{
MessageBox.Show("Function F9");
}
if (e.KeyCode == Keys.F6)
{
MessageBox.Show("Function F6");
}
else
MessageBox.Show("No Function");
}
but nothing happens
Thanks
You need to set
KeyPreview=True
for your form. Otherwise key press is swallowed by the control that has focus.

Categories