Determining which key is pressed - c#

This function has many applications. I have a TextBox control. How to display keys that the user pressed. For example pressed CTRL and z in the TextBox control should appear "Ctrl + Z"? It's WPF application. Thanks.
Now i'm trying like this:
private void txtHotKey_PreviewKeyUp(object sender, KeyEventArgs e)
{
txtHotKey.Text += e.Key.ToString();
txtHotKey.Text += "+";
e.Handled = true;
}
Now if I pressed Ctrl and Z in the textbox appear "Ctrl+Z+". Then press Ctrl and A. Will be "Ctrl+Z+Ctrl+A+". It's wrong.

Have you looked at the KeyEventArgs you get passed to the KeyDown event handler of the TextBox?
It's got plenty of properties that identify which key has been pressed.

Take a look at the following post with example code: Capture key press events with WPF
Does it help you? You can either attach yourself to the grid (or your textbox) with the KeyDown event handler or you could use a Window_KeyDown method.

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

Detecting multiple PageUp/PageDown key presses

I have a NumericUpDown control in WinForms. The Up/Down arrow keys increase/decrease the value by 1. I want to map the PageUp/PageDown keys for larger increments.
The Control.KeyPress event does not get fired with the PageUp/PageDown keys and if I use the Control.KeyUp/Control.KeyDown events, the combination fires only once even if the user keeps the keys pressed for a while.
How could I trap multiple PageUp/PageDown keys presses during long key presses?
Strange to say, but I cannot reproduce this issue. I have a new winform instance, numericUpDown control with default property` values and KeyDown event handler works perfectly with long key press:
private void numericUpDown1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.PageDown)
numericUpDown1.Value -= 10;
else if (e.KeyCode == Keys.PageUp)
numericUpDown1.Value += 10;
}
Can you provide your code in the event handlers?
/Sorry, I know Im not supposed to ask for clarification in the answer, but I can`t write comments./

How can you create Alt shortcuts in a Windows Forms application?

I'd like to create keyboard shortcuts for some controls in my Windows Forms application.
Example:
Notice the underlined, F E V P B.
I have a label and a textbox control. I'd like to associate that Alt keyboard shortcut to the label and the textbox. So if someone presses Alt + B, focus is given to the associated textbox. Is there a way to create this association?
When the label receives focus from pressing its accelerator key (set using the &), it forwards the focus to the next control in the tab order, since labels are not editable. You need the textbox to be next control in the tab order.
To view and correct the tab order of your form, use the View + Tab Order command in the IDE. Using TabPages or other containers adds a level of nesting to the tab order (e.g., 1.1, 1.2 instead of just 1 and 2), but if the label and textbox are within the same container it shouldn't be too hard to set properly.
Type &File or &Edit and you will get underline. That will automatically bind underlined letters with Alt keyword for shortcut.
EDIT.
You question has modified so I'd like to keep up with my answer. You would like to catch some keys combination (Alt + F) and set a focus to the text box.
You may try this solution using KeyDown event of the main form.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyCode == Keys.F)
{
this.textBox1.Focus();
}
}
To achieve this, you have to additionally set KeyPreview property of the form to true.
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyCode == Keys.W)
{
btnShowConstructionCdFun();
}
}

How to use keyboard shortcuts to select a portion of waveform instead of mouse drag and drop?

I am making a project where i am drawing waveform of an audio file in C#. Currently I am using mouse drag and drop for a part of waveform selection. But now i want that the waveform should also be selected by using following:
click at a point
press shift and click again on another point.
I dont have much knowledge about keyboard events. So need help in this.
Use the Control.ModifierKeys property to detect whether the Shift key is pressed. Sample code:
private void panel1_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
if (Control.ModifierKeys == Keys.Shift) SetSelectionEnd(e.X);
else SetSelectionStart(e.X);
}
}
Implementing the SetSelectionStart/End is up to you.
The KeyUp event won't work for this, because although the KeyEventArgs argument for this event includes a Shift property that indicates whether or not the shift key is currently down, the event is not triggered when you only press the shift key (and not any other key). The KeyPress event is also not triggered just by the shift key.
Fortunately, the PreviewKeyDown event is exactly what you need for this. The PreviewKeyDownEventArgs argument includes a Shift property (also Control for the ctrl key), and the event is fired when you just click the shift button.
Also, PreviewKeyDown is triggered on a Form regardless of whether that form's KeyPreview is set to true or not, which is handy.

Press Escape key to call method

Is there a way to start a method in C# if a key is pressed? For example, Esc?
use the OnKeyPress Event of your textbox and in the event
if(e.KeyCode==Keys.Escape)
{
yourTextBox.Text = string.Empty;
}
As others have mentioned, handle the KeyDown or KeyUp event of the appropriate control. The KeyPress event would work for the Escape key as well, though it will not trigger for some keys, such as Shift, Ctrl or ALt.
If you want to execute this function anytime the user presses the Escape key, then you probably want to handle the event on the Form. If you do this, you will probably also want to set the Form's KeyPreview property to true. This will allow the Form control to receive the event even if the focus is currently inside of one of the child controls.
If you want the behavior to be specific to a control, such as clearing the text within a textbox that currently has focus, then you should handle the KeyDown or KeyUp event of the TextBox control. This way, your event handler will not be triggered if the user presses the escape key outside of the textbox.
In some situations you might want to prevent child controls from handling the same event that you've just handled. You can use the SuppressKeyPress property on the KeyEventArgs class to control this behavior:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
MessageBox.Show("Escape key pressed");
// prevent child controls from handling this event as well
e.SuppressKeyPress = true;
}
}
In case someone is looking for how to do this in a console application
if (Console.ReadKey().Key == ConsoleKey.Escape)
{
return;
}
I am writing WinForms application. User fills the textbox and if he wants to delete everything, he just clicks esc key on keyboard
I think you need to handle the KeyDown event.
You have to switch the form property "KeyPreview" to true or your events will not be fired. Handling these events alone will not do anything even though the events are correct. It will look to you like nothing really happens even though you have subscribed the proper event handlers.
First in Properties do > KeyPreview : True
Then :
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
//call your method here
}
}
Are you writing a Console application, a WinForms application or something else? Are you trying to capture the ESC key at all times (regardless of the focused window/application) or something else?
More context required.
If you're writing a Console app, then you should start looking at things like Console.ReadKey...
http://msdn.microsoft.com/en-us/library/system.console.readkey.aspx
With Event KeyPress...
//Escape
if (e.KeyChar == '')
{
this.DialogResult = DialogResult.Cancel;
e.Handled = true;
}
You can use KeyUp event too. I prefer it though.
private void Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e) {
if (e.Key == Key.Escape) {
//WHAT WILL HAPPEN INSERT HERE
}
}
The basic answer is listed here several time
Implement Form_KeyDown
Private Sub frmCustomerSearch_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Try
If e.KeyCode = Keys.Escape Then
ClearFindForm()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Set form.keyPreview
form.KeyPreview = true
The additional thing you need to check is whether you have a button capturing ESC so it can be the form.cancelButton
to be sure ...
form.CancelButton = nothing
This is sneaky. If you have set that and forgot about it, the Escape key will not trigger the KeyDown event.
I was led to this because a button set to be the form.CancelButton does not seem to fire if it is invisible or on a non visible tab,so KEYDOWN is your only option.

Categories

Resources