I wanted to handle Arrow key press event on the slider control. I tried googling for it but almost all the links gave me information about handling it on the windows(overrideing WndProc or ProcessCmdKey).The KeyDwon and Key Up events aren't fired for the Arrow press. How can i handle it?
Look here.
Here's a short quote from there:
Certain keys, such as the TAB, RETURN, ESC, and arrow keys are handled by controls automatically. To have these keys raise the KeyDown event, you must override the IsInputKey method in each control on your form. The code for the override of the IsInputKey would need to determine if one of the special keys is pressed and return a value of true. Instead of overriding the IsInputKey method, you can handle the PreviewKeyDown event and set the IsInputKey property to true. For a code example, see the PreviewKeyDown event.
And here's the code sample from the PreviewKeyDown event from here (PreviewKeyDown):
// By default, KeyDown does not fire for the ARROW keys
void button1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Down:
case Keys.Up:
if (button1.ContextMenuStrip != null)
{
button1.ContextMenuStrip.Show(button1,
new Point(0, button1.Height), ToolStripDropDownDirection.BelowRight);
}
break;
}
}
// PreviewKeyDown is where you preview the key.
// Do not put any logic here, instead use the
// KeyDown event after setting IsInputKey to true.
private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Down:
case Keys.Up:
e.IsInputKey = true;
break;
}
}
I'm presuming you're using a track bar control when you say slider control? If not, then this answer probably won't help.
Anyway, you need to set the OnKeyDown event for your track bar control. Something as simple as the following code will allow the user to use the left and right arrows to move from side to side.
private void trackBar1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == Keys.Right) && (trackBar1.Value < trackBar1.Maximum))
trackBar1.Value += 1;
if ((e.KeyCode == Keys.Left) && (trackBar1.Value > trackBar1.Maximum))
trackBar1.Value -= 1;
}
You simply need to detect a key press, and then decide whether it's a left or right arrow, and then what to do from there.
I've tried it and the left and right arrows do trigger it for me. Again, if you're using a different slider control (there isn't any control called the slider control, so I'm assuming track bar) then it may be different.
Related
I have a WinForms form with a TableLayoutPanel on it.
In this TableLayoutPanel, I have 2 Button controls.
I press one of these buttons.
Then I press the Up or Down arrow (↑ ↓) on the keyboard.
The focus jumps from one button to the other.
I don't want this behaviour.
Setting the TableLayoutPanel's "TabStop" to "False" doesn't help.
I would instead like to intercept the event and set the focus to another control manually.
However, I didn't find the event that I need to intercept.
How could I disable this jumping by arrow keys?
Which event would I have to intercept to set the focus to another control?
Thank you!
You can handle PreviewKeyDown event of all the buttons using a single handler and check if the pressed key is an arrow key, set e.IsInputKey = true and prevent focus change:
private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
var keys = new[] { Keys.Left, Keys.Right, Keys.Up, Keys.Down };
if (keys.Contains(e.KeyData))
e.IsInputKey = true;
}
You can read about the behavior and the solution in Remarks section of the PreviewKeyDown documentations.
The behavior is in fact implemented in ProcessDialogKey method of the container control (you can see source code) and you can also prevent it by overriding ProcessDialogKey of the Form, like this:
protected override bool ProcessDialogKey(Keys keyData)
{
var keys = new[] { Keys.Left, Keys.Right, Keys.Up, Keys.Down };
if (keys.Contains(keyData))
return true;
else
return base.ProcessDialogKey(keyData);
}
I have a situation where I'm provided with a WinForms TextBox instance which I want to attach autocomplete functionality to.
I've got the autocomplete (string matching + dropdown) all figured out and it works reliable so far.
What is the ability to navigate the dropdown with the keyboard (as is the norm with this sort of UI).
The natural solution would be to handle KeyDown (or somesuch) event for the textbox and moving the selection in the dropdown accordingly.
However, it happens that to do this, you need to override the IsInputKey() event to allow capture of arrow key events. The alternative is to override ProcessCmdKey() and handle the event there. The problem with these two is that I cannot override anything since I can't replace the textbox instance.
Edit: Let's assume I have the code below:
void _textBox_KeyDown(object sender, KeyEventArgs e)
{
if (_dropdown.Visible)
{
// TODO The stuff below fails because we need to either handle ProcessCmdKey or override IsInputKey
switch (e.KeyCode)
{
case Keys.Tab:
{
// click selected item
_dropdown.Items[GetSelectedItemIndex()].PerformClick();
break;
}
case Keys.Down:
{
// select next (or first) item
int i = GetSelectedItemIndex() + 1;
if (i >= _dropdown.Items.Count) i = 0;
_dropdown.Items[i].Select();
break;
}
case Keys.Up:
{
// select previous (or last) item
int i = GetSelectedItemIndex() - 1;
if (i < 0) i = _dropdown.Items.Count - 1;
_dropdown.Items[i].Select();
break;
}
}
}
}
Them problem with the code above is that it is never called. The event is never triggered for arrow keys. More info: Up, Down, Left and Right arrow keys do not trigger KeyDown event
I hope i haven't missunderstood you, but is this a solution:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
// Place logic for textbox here
}
}
I'd use a KeyDown event on the form and then compare the keycode with the Keys.Down keycode
Not working
see here: Up, Down, Left and Right arrow keys do not trigger KeyDown event
I may not be understanding your question entirely, but wouldn't an approach like this work?
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
comboBox1.Text = //results of your matching algorithm.
}
private void textBox1_Validated(object sender, EventArgs e)
{
textBox1.Text = (string) comboBox1.Text;
}
The lead developer says that when he uses my app, his keyboard beeps when he moves between TextBoxes on the TableLayoutPanel via the directional arrow keys.
However, I hear no such aural activity.
Here's my code:
// Had to intercept Up and Down arrows from Windows
private void textBoxPlatypi_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
TextBox tb = (TextBox)sender;
if (e.KeyCode.Equals(Keys.Up)) {
SetFocusOneRowUp(tb.Name);
return;
}
if (e.KeyCode.Equals(Keys.Down)) {
SetFocusOneRowDown(tb.Name);
return;
}
}
private void textBoxPlatypi_KeyDown(object sender, KeyEventArgs e) {
TextBox tb = (TextBox)sender;
if (e.KeyCode.Equals(Keys.Left)) {
SetFocusOneColumnBack(tb.Name);
e.Handled = true;
return;
}
if (e.KeyCode.Equals(Keys.Right)) {
SetFocusOneColumnForward(tb.Name);
e.Handled = true;
return;
}
}
..He thought maybe I needed "e.Handled" but that is not available in the PreviewKeyDown event.
Is there a way to suppress the beeping (which apparently occurs only with certain keyboards or specific setups (he's using Windows7, I'm on XP still))?
UPDATE
I've got this code now:
private void textBoxPlatypus1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
switch (e.KeyCode) {
case Keys.Down:
case Keys.Up:
e.IsInputKey = true;
break;
}
}
private void textBoxPlatypus1_KeyDown(object sender, KeyEventArgs e) {
TextBox tb = (TextBox)sender;
if (e.KeyCode.Equals(Keys.Up)) {
SetFocusOneRowUp(tb.Name);
e.Handled = true;
return;
}
if (e.KeyCode.Equals(Keys.Down)) {
SetFocusOneRowDown(tb.Name);
e.Handled = true;
return;
}
if (e.KeyCode.Equals(Keys.Left)) {
SetFocusOneColumnBack(tb.Name);
e.Handled = true;
return;
}
if (e.KeyCode.Equals(Keys.Right)) {
SetFocusOneColumnForward(tb.Name);
e.Handled = true;
return;
}
}
...but he still hears the beeping (I don't).
He's in Alaska and using Windows 7; I'm in California and using XP. I don't know if some combination/mismatch there is the problem...
UPDATED AGAIN
I know this may be shocking to some, but the Alaska/California disconnection has nothing to do with it. I'm now hearing the beeps, too, and it's not from the arrow keys. It's when a value is entered in a TextBox and then, if that text box already has a character, focus is moved to the next textBox and the value is entered there (this is my code that causes this to happen). But the irritating beeping seems to be random - I haven't figured out the pattern for when it beeps (sometimes it does, sometimes it doesn't)...has anybody ever run across anything like that, or, better yet, know how to suppress the beep? All I'm doing is pressing either the "1" or the "2" key above the keyboard.
There is no way in the PreviewKeyDownEvent to Handle / Supress a KeyEvent like there is in the normal KeyDown Event. What the documentation suggests is to set the PreviewKeyDownEventArgs.IsInputKey property to true in order to handle key presses that are not available normally in the KeyDown Event.
From above Link, they are using a button as an example:
Some key presses, such as the TAB, RETURN, ESC, and arrow keys, are typically ignored by some controls because they are not considered input key presses... By handling the PreviewKeyDown event for a Button and setting the IsInputKey property to true, you can raise the KeyDown event when the arrow keys are pressed. However, if you handle the arrow keys, the focus will no longer move to the previous or next control.
Try this:
e.SuppressKeyPress = true;
I'm making a program in C#, And I want to know when the user is pressing\pressed a keyboard button (for now: 1-9 buttons on keyboard).
How can I do it?
The Control.KeyPress event (and related KeyDown and KeyUp) should do what you need. Just define an event handler for the one you need in your form:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show("Key pressed: " + e.KeyChar);
}
The MSDN page under the link has a more extensive example that deals with "special" keys (you will need to use KeyPress or KeyDown for those).
If you want to capture keys while the focus is not on your form, that's a different matter entirely, but I don't think that's the case as you want to capture keys 1-9. Not the typical global hotkey :)
Don't forget to set the KeyPreview property to true, else other controls on your form (if you have other controls on your form) will receive the event (if they have focus) before the form gets it.
Hook an function to the OnKeyUp event of a form.
see here
private void form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if ((e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9) || (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9))
{
//Do something
}
}
What you search are the 2 Events KeyDown and KeyUp.
KeyUp is when a Key "was" pressed and the user lift his finger up.
KeyDown is the other event.
Just take a new Form. Go to the Events(Press F4) and you will find KeyDown and Up.
private void maskedTextBox1_KeyDown_1(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
and so on :D
My ActiveX control contains various shapes which are drawn. CTRL-A is used in the control to select all the objects. Similarly CTRL-C to copy, CTRL-V to paste etc.
However, when I insert this control within a Windows form in a .Net application, it does not receive these keyboard events. I tried adding a PreviewKey event, and this does allow certain keystrokes to be sent e.g. TAB, but not these modified keys.
Does anybody know how to redirect modified keystrokes to a user control?
Thanks.
It's possible that the ActiveX control doesn't have focus and is therefore not receiving the key events. You may want to handle the key events at the form level and then call the appropriate methods on your ActiveX control. If you set the KeyPreview property of your form to true your form will receive the key events for all controls on the form. That way, your shortcuts should work no matter what control currently has focus. Here is a quick example you can play with to test this out. Create a new form with several different controls on it and modify the code like so:
public Form1()
{
InitializeComponent();
KeyPreview = true; // indicates that key events for controls on the form
// should be registered with the form
KeyUp += new KeyEventHandler(Form1_KeyUp);
}
void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control)
{
switch (e.KeyCode)
{
case Keys.A:
MessageBox.Show("Ctrl + A was pressed!");
// activeXControl.SelectAll();
break;
case Keys.C:
MessageBox.Show("Ctrl + C was pressed!");
// activeXControl.Copy();
break;
case Keys.V:
MessageBox.Show("Ctrl + V was pressed!");
// activeXControl.Paste();
break;
}
}
}
No matter what control has focus when you enter the key combinations, your form's Form1_KeyUp method will be called to handle it.
You need to trap the keys and override the ProcessCmdKey method.
class MyDataGrid : System.Windows.Forms.DataGrid
{
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
...........
}
}
http://support.microsoft.com/kb/320584
KeyPreview is just the wrong method. Try using KeyUp or KeyDown, like this:
private void ControlKeyTestForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
this.label1.Text = "Ctrl+A pressed";
}
If you want the containing form to deal with shortcut keys remember to set the KeyPreview property on the form to true then set the KeyDown or KeyUp handlers in the form.
Use Control.ModifierKeys Property to check for Modifier keys.
For example, to check for shift key,
try if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift) { }
Full example here:
http://msdn.microsoft.com/en-us/library/aa984219%28VS.71%29.aspx