Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// The program flow does not enter this scope.
if (e.KeyCode == Keys.Right)
{
}
}
You need to set the KeyPreview property of the Form to true
As per MSDN
When this property is set to true, the form will receive all KeyPress,
KeyDown, and KeyUp events. After the form's event handlers have
completed processing the keystroke, the keystroke is then assigned to
the control with focus. For example, if the KeyPreview property is set
to true and the currently selected control is a TextBox, after the
keystroke is handled by the event handlers of the form the TextBox
control will receive the key that was pressed. To handle keyboard
events only at the form level and not allow controls to receive
keyboard events, set the KeyPressEventArgs.Handled property in your
form's KeyPress event handler to true.
This can be done either at design time, by simply setting true for Form's KeyPreview
or to manually do it
public class Form1 : Form
{
public Form1()
{
base.KeyPreview = true;
InitializeComponent();
}
}
Use the ProcessCmdKey override:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Right)
System.Diagnostics.Debug.WriteLine("Right key pressed");
return true;
}
You could use this:
KeyboardState ks = new KeyboardState();
if(ks.isKeyDown(Keys.Right)
{
System.Diagnostics.Debug.WriteLine("Right key pressed");
}
Note: This is off the top of my head so may not be 100% accurate but I'm 98% sure that it will work :)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm creating a program, that is able to draw on the full desktop. I have different functions for drawing for example an ellipse. There is also a function for inserting some Text. For this I use a transparent TextBox(Tbx). When I call this function, it creates a Label, that gets the Text of the Textbox, everytime I change the text. This works quite well, but I also have a function that should minimize the Window with ↓. After inserting text, the key is useless, but the other keys are still captured with the event handler. If I don't insert text into Textbox control, I can minimize. The main problem is, that the ↓ is not captured with the event handler and the minimization doesn't happen. I'm unsure if KeyDownEvent is the correct event to capture special keys such as arrows, Ctrl, ... So the question is how to fix this code to capture ↓ and execute the code to minimize the Window.
Here are some parts of my code:
Creating a Label:
Tbx.Clear();
if (!hasStartPoint)
{
START = current;
hasStartPoint = true;
}
this.canvas1.Children.Remove(Lab2);
Lab1 = new Label() { IsEnabled = true, Background = Brushes.Transparent, Foreground = this.brush, FontFamily = textFont, Content = "" };
if (this.thickness > 5000) { FontSize = 5000; }
else { FontSize = this.thickness; }
Canvas.SetLeft(Lab1, START.X);
Canvas.SetTop(Lab1, START.Y);
Tbx.Focus();
isWriting = true;
this.Cursor = Cursors.IBeam;
canvas1.Children.Add(Lab1);
Lab2 = Lab1;
Writing text:
void Tbx_TextChanged(object sender, TextChangedEventArgs e)
{
if (isWriting)
{
Lab1.Content = Tbx.Text;
}
}
Finished writing:
else if (e.Key == Key.Enter || e.Key == Key.End)
{
if (!isWriting)
{ this.Close(); }
else
{
Lab1 = new Label() { IsEnabled = true, Background = Brushes.Transparent, Foreground = this.brush, FontSize = this.thickness, FontFamily = textFont, Content = "" };
Canvas.SetLeft(Lab1, 1);
Canvas.SetTop(Lab1, 1);
canvas1.Children.Add(Lab1);
isWriting = false;
this.Cursor = Cursors.Cross;
}
}
Minimizing the Window in a KeyDownEvent:
else if (e.Key == Key.Down)
this.WindowState = WindowState.Minimized;
The solution for catching special keys is to use PreviewKeyDownEvent. This is because those keys are not considered to be input for some controls.
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. For example, by default, a Button control ignores
the arrow keys. Pressing the arrow keys typically causes the focus to
move to the previous or next control. The arrow keys are considered
navigation keys and pressing these keys typically do not raise the
KeyDown event for a Button. However, pressing the arrow keys for a
Button does raise the PreviewKeyDown event. 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.
This question already has answers here:
Stop the 'Ding' when pressing Enter
(16 answers)
Closed 8 years ago.
I'm using this to allow cursor progressing to next TextBox on a WinForm:
private void GoToNextControl(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
this.SelectNextControl((Control)sender, true, true, true, true);
}
}
This works flawlessly if not for a "ding" sound when I press enter. How could I "silence" the ding?
Setting SuppressKeyPress to true in the handler should do it:
private void GoToNextControl(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
this.SelectNextControl((Control)sender, true, true, true, true);
e.SuppressKeyPress = true;
}
}
Make sure your handler is hooked up to the KeyDown event, as this won't work in KeyUp.
"Ding" sound comes from unhandled Form event. On your Form:
1. Add a button, set its Visible property to false
2. Add OnClick event handler to that button. Keep the method empty
3. Set Form's AcceptButton property to the new button.
That's it. "Ding" will be gone.
Im trying to do like that: If I press the key "P" a messagebox will open in the screen.But I need to do it without a textbox or other tool, I want to do that direct in the form.
I tried:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.P)
{ MessageBox.Show("Key P pressed"); }
}
Try with the KeyPress Event of the form. It just works fine.
Assuming this is Winforms, on the form you're trying to catch the event on, make sure to set
Form1.KeyPreview = true;
KeyPreview ensures that keyboard events anywhere on the particular form (such as to a textbox with focus) will still count as a keyboard event for the form itself.
I want to move a button using a function which will be activated through a keyboard button, but I can't seem to make it accept my input. If I try to run the function through a button press, it works fine, so I know the function is not to blame. What am I doing wrong that it's not accepting my keyboard input?
private void MoveLeft()
{
_y = btnBot.Location.Y;
_x = btnBot.Location.X;
btnBot.Location = new System.Drawing.Point(_x - 10,_y);
}
void MoveLeft_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
MoveLeft();
}
}
To ensure that your form key handle event is triggered, even if another control has focus, make sure the KeyPreview property is set to true.
In your main form, add the following line, or set it during design time.
this.KeyPreview = true;
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