C# Key Events - KeyDown - c#

I have a Windows Form Application. I want some functions to work with the space key. But when I press the space key, the function I want is not working and it goes to the next form. (I did KeyPreview = true)
private void Form7_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
IEyeTracker eyeTracker = EyeTrackingOperations.FindAllEyeTrackers().FirstOrDefault();
GazeDataStop(eyeTracker);
}
}

Because:
1- If you have buttons, ... keydown won't work as form won't have focus anymore
2-you must handle the keydown so that it is not passed to ohter controls
Solution for 1:
set KeyPreview property of your form to true
Solution for 2:
set e.Handled = true:
private void Form7_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
if (e.KeyCode == Keys.Space)
{
IEyeTracker eyeTracker = EyeTrackingOperations.FindAllEyeTrackers().FirstOrDefault();
GazeDataStop(eyeTracker);
}
}

Related

How to block space in passwordbox in wpf

I’m using a PasswordBox control in wpf.
I want to block space in the control, but i can’t find the way.
I tried to use a KeyDown event but the event isn’t working.
What is the best way to block space in the PasswordBox?
For WPF you should using PreviewKeyDown Based on docs occurs before the KeyDown event When a key is pressed while focus is on this control.
XAML:
<PasswordBox x:name="txtPasscode" PreviewKeyDown="txtPasscode_PreviewKeyDown"/>
and in behind :
private void txtPasscode_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space && txtPasscode.IsFocused == true)
{
e.Handled = true;
}
}
Also in C# native try this :
private void txtPasscode_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == ' ') e.Handled = true;
}
The KeyDown event you're handling actually fires after the character added to the passwordbox. You can block it by handling PreviewKeyDown this way(KeyDown won't fire anymore if user pressed space):
private void passwordbox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
{
e.Handled = true;
}
}
If you need to block an event you're going to use the event beginning with "Preview"(read more here).

c# detect and send f13-f24 keys

I need an application that can detect the keypress of F13-F24. I tried making a form and setting keydown and Keypress events and printing in a messagebox the key pressed but when i press F13-F24, i don't get a messagebox
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show(e.KeyCode.ToString());
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show(e.KeyChar.ToString());
}
This didn't seem to work. How can I do this?
You can capture this using System.Windows.Forms.KeyEventHandler.
The KeyEventHandler will provide a KeyEventArgs object that includes a KeyCode property. KeyCode is a System.Windows.Forms.Keys enumeration and supports F1-F24.
private void KeyDownHandler(object sender, System.Windows.Forms.KeyEventArgs e) {
if (e.KeyCode == Keys.F13) {
//F13
} else if (e.KeyCode == Keys.F14) {
//F14
} else if (e.KeyCode == Keys.F15) {
//F15
}
// etc
}
If you don't have a keyboard capable of sending higher F-keys, the F13-F24 keys are the equivilent of SHIFT-F1-SHIFT-F12. You can capture this sequence using the KeyCode and Modifiers (Modifiers == Keys.Shift) properties together.
Windows 10 doesn't recognize the higher Function keys 13-24. You can test this by turning on the Windows Narrator functionality, and then press Shift -F1. The narrator just says "Shift F1", not "F13"
So, instead I recommend that you just detect Shift -F1 to Shift -F12 instead.
private void Form1_PreviewKeyDown(object sender, KeyPressEventArgs e)
{
if(e.KeyCode == Keys.F1 && e.Modifiers == Keys.Shift)
{
MessageBox.Show("Shift-F1 pressed");
}
}
As MstfAsan commented, you need to use the PreviewKeyDown eventhandler and set the form's KeyPreview property to True for this to work

Focus selected controls by pressing Enter Key

In C# windows application to navigate all control of a Form (using Enter Key) I am using the below code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == System.Windows.Forms.Keys.Enter)
{
SendKeys.Send("{TAB}");
}
}
N.B.: Form Property KeyPreview = True;
The above code works fine but when I am going to navigate between two dateTimePicker (dateTimePicker1, dateTimePicker2) pressing Enter Key.
When Form open Focus on dateTimePicker1 and press Enter Key then Focus dateTimePicker2 and press Enter Key Focus dateTimePicker1.
The below code works fine without the above code. What is the best way to navigate the two dateTimePicker using the above code or any other way?
private void dateTimePicker1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) {
dateTimePicker2.Focus();
}
}
private void dateTimePicker2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) {
dateTimePicker1.Focus();
}
}
Anybody please help me.
You can subscribe your two DateTimePickers to the same event handler instead of using two events, and use the sender object:
private void dateTimePicker_KeyDown(object sender, KeyEventArgs e)
{
var dtp = sender as DateTimePicker;
if (e.KeyCode == Keys.Enter)
{
if (dtp?.Name[dtp.Name.Length - 1] == '1')
dateTimePicker2.Focus();
else dateTimePicker1.Focus();
}
}
Just don't forget to change the value of the KeyDown event in the properties window of the both DateTimePickrs to point to this event.

How can I override the default button on a form when focus is in a TextBox

I have a winform form which has typical OK and Cancel buttons.
The OK button is set as the default button. When the user presses the ENTER key, the form is closed.
The form also has a text box which has a Search button beside it. The text box allows the user to enter search text and the Search button initiates the search function.
I would like the user to be able to press the ENTER key when the text box has the input focus and have the search function activate.
The problem is that the form is grabbing the ENTER key event before the text box's handler gets it.
EDIT: I've tried using the following event handlers, but they never get hit, and the form closes when the Enter key is hit:
private void txtFilter_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Search();
e.Handled = true;
}
}
private void txtFilter_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Search();
e.Handled = true;
}
}
I've also tried enabling the KeyPreview property on the form and implementing a KeyDown event handler for the form, but the Enter key never seems to cause any of these to be hit.
What is the best way to accomplish this?
Try handling the Enter and Leave events of the TextBox to clear out your form's AcceptButton property:
private void txtFilter_Enter(object sender, EventArgs e) {
this.AcceptButton = null;
}
private void txtFilter_Leave(object sender, EventArgs e) {
this.AcceptButton = closeButton;
}
Then you can just process your KeyUp event as your want:
private void txtFilter_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Search();
}
}
Add a KeyDown event handler to the textBox and then add this to it
if (e.KeyCode == Keys.Enter)
{
btnSearch.PerformClick();
}

Detect Shift+Tab Function Into Backspace

Software Utilize : C#, VS-2005
Is This Possible to override Shift+Tab Function/Method or detect Shift+Tab Function and Utilize it with Backspace.?
In Shot replace Shift+Tab Function with Backspace. And Then Backspace will Behave like Shift+Tab:
Is this possible in C#?
I suppose you are working on a win-form. Register a key down event:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift)
{
// act like a backspace is pressed
}
else if (e.KeyCode == Keys.Back)
{
SendKeys.Send("+{TAB}"); // simualte a shift-tab press
}
}
To resolve the issue #liggett78 mentioned in the comment, you can set
form.KeyPreview = true;
to handle all the key events of child controls in the KeyDown event of the from.
EDIT: To prevent deleting a character in textbox when pressing BACKSPACE, you can:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back)
{
e.SuppressKeyPress = true;
SendKeys.Send("+{TAB}");
}
}
Override ProcessDialogKey or ProcessTabKey on your form.

Categories

Resources