Detecting multiple PageUp/PageDown key presses - c#

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./

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

C# Ctrl+F doesn't work sometimes

I have a winforms application where in I have a textbox inside a form. I need to set focus to the textbox whenever I press 'Ctrl+F'. I have the following code in keyup event.
private void frm_KeyUp(object sender, KeyEventArgs e)
{
// Handle 'Ctrl + F' to Find
if (e.KeyData == (Keys.Control | Keys.F))
SetFocus();
}
Problem I have here is that sometimes, even though the focus is on form and I try 'Ctrl+F' the condition doesn't run. I know, as soon as I press 'Ctrl' the event gets fired even before I would have pressed key 'F'. Eventually it works, when I press both the keys at the very same time. So to the user it might look like the screen is unresponsive to the keys sometimes.
How can I overcome this situation?
You're using the KeyUp event and checking if the event contains both keys. This will only happen when you release both keys at the same time.
Change it to the KeyDown event instead, and check whether Ctrl was pressed at the moment F was pressed:
if (e.Control && e.KeyCode == Keys.F)
{
// ...
}

How to navigate tabcontrol with arrow keys?

I have a tabcontrol with a series of tabs that contain textboxes and other input controls. If I click on the tab headers I can navigate through the tabs using the left and right arrow keys, but if I'm currently in a textbox or other control then I can't, assuming this is as the textbox takes all of the key events. I have tried attaching event handlers to keydown and previewkeydown but they don't get fired.
Is there a way to get the key events through the tabcontrol even when a child control has focus?
You can use the TabControl's KeyDown event to listen for arrow keys.
But this will likely conflict with other controls:
e.g. Moving the cursor left/right in a TextBox.
If all the controls are read-only, then this won't be much of an issue.
But if people are allowed to alter the data, then intercepting the arrow keys will interfere with standard navigation.
private void tabControl1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Left) {
if (tabControl1.SelectedIndex > 0) {
tabControl1.SelectedIndex--;
}
};
if (e.KeyCode == Keys.Right) {
if (tabControl1.SelectedIndex < tabControl1.TabCount - 1) {
tabControl1.SelectedIndex++;
}
};
}
Note: Having done a bit more testing, it works fine with TabSheets containing TextBoxes, but doesn't work if RadioButtons have focus in a TabSheet.

Determining which key is pressed

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.

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.

Categories

Resources