c# detect and send f13-f24 keys - c#

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

Related

C# Key Events - KeyDown

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);
}
}

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 do I tell when the enter key is pressed in a TextBox?

Basically, I want to be able to trigger an event when the ENTER key is pressed. I tried this already:
private void input_KeyDown(object sender, KeyEventArgs e)
{
if (e.Equals("{ENTER}"))
{
MessageBox.Show("Pressed enter.");
}
}
But the MessageBox never shows up. How can I do this?
Give this a shot...
private void input_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyData == Keys.Enter)
{
MessageBox.Show("Pressed enter.");
}
}
To add to #Willy David Jr answer: you also can use actual Key codes.
private void input_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyChar == 13)
{
MessageBox.Show("Pressed enter.");
}
}
You can actually just say
private void input_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
MessageBox.Show("Pressed enter.");
}
}
You can use the Keypress event. If you are just looking for the "Enter" keypress, then you probably don't care about modifier keys (such as Shift and/or Ctrl), which is why most would use KeyDown instead of Keypress. A second benefit is to answer the question that is almost always asked after implementing any of the other answers: "When I use the referenced code, why does pressing "Enter" cause a beep?" It is because the Keypress event needs to be handled. By using Keypress, you solve both in one place:
private void input_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
// Your logic here....
e.Handled = true; //Handle the Keypress event (suppress the Beep)
}
}
If your Form has AcceptButton defined, you won't be able to use KeyDown to capture the Enter.
What you should do is to catch it at the Form level. Add this code to the Form:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((this.ActiveControl == myTextBox) && (keyData == Keys.Return))
{
//do something
return true;
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}
You can also do this:
private void input_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode== Keys.Enter)
{
//Your business logic here.
}
}
The only difference with KeyCode vs KeyData is that KeyCode can detect modifiers combination with KeyCode (e.g. CTRL, Shift + A) which you don't need here.
Fast forward 2022, the following statement above is completely other way around.
"The only difference with KeyCode vs KeyData is that KeyCode can detect modifiers combination with KeyCode (e.g. CTRL, Shift + A) which you don't need here."
the KeyDown event e.KeyCode does not trigger Keys.Enter

Capture ESC in textbox

I want to have the Esc key undo any changes to a textbox since it got focus.
I have the text, but can't seem to figure out how to capture the Esc key. Both KeyUp and KeyPressed don't seem to get it.
This should work. How are you handling the event?
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
MessageBox.Show("Escape Pressed");
}
}
Edit in reply to comment - Try overriding ProcessCmdKey instead:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Escape && myTextBox.Focused)
{
MessageBox.Show("Escape Pressed");
}
return base.ProcessCmdKey(ref msg, keyData);
}
is this what you're looking for?
string origStr = String.Empty;
private void txtOrig_Enter(object sender, EventArgs e)
{
origStr = txtOrig.Text;
}
private void txtOrig_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == Convert.ToChar(Keys.Escape))
{
txtOrig.Text = origStr;
}
}
Supposedly some keys are not considered "input keys" and so are not listened to by default. You need to handle PreviewKeyDown first to enable it.
myTextBox.PreviewKeyDown += (s, e) => {
if (e.KeyCode == Keys.Escape) {
e.IsInputKey = true;
Debug.Print("ESC should get handled now.");
}
};
However, results from testing say otherwise, so it may depend on framework version. For me, whether I do that or not, KeyDown does not get called for ESC, and whether I do that or not, KeyPress DOES get called for ESC. This is while a TextBox has focus, so it may also depend on the control.

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