c# Starting events with hotkey? - c#

private void MeretOK_Click(object sender, EventArgs e)
{
//code
}
private void MeretTB_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode==Keys.Enter)
{
MeretOK_Click();
}
}
How can I start an event with a hotkey?
(I know I can just copy the code there but that is ugly)
If I just copy the code and run it I hear a beep. Why?

First, you have to allow your form to handle key events globally (set the form KeyPreview property to true) and to mark your event as internally handled. Second, if the purpose of this code is to simulate the mouse click on a specific control (programmatic click), for example a button, there is an easier way to accomplish it:
private void MeretTB_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
myButton.PerformClick();
}
}

Related

How to handle Crtl+Click event in WPF XAML

I want to handle crtl+click event in WPF XAML code behind. I registered KeyDown event and MouseDown event, but I am not able to register for them together. What is that I am missing.
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key.Equals(Key.LeftCtrl))
{
//do something
}
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 1)
{
//do something
}
}
Thanks,
RDV
Don't bother tracking Key Up/Down events--they're not reliable (e.g., user may switch windows between pressing and releasing). Just check Keyboard.Modifiers when you process the click.
If you only want to handle Ctrl+Click and not, for example, Ctrl+Shift+Click, then check Modifiers for an exact match:
if (e.ClickCount == 1 && Keyboard.Modifiers == ModifierKeys.Control) {
}
Otherwise, do a selective bitwise match:
if (e.ClickCount == 1 && (Keyboard.Modifiers & ModifierKeys.Control) != 0) {
}
Keyboard.Modifiers should generally be consistent: it won't reflect the release of the control key until after you process the mouse event, because all input events are processed in FIFO order.
Be sure to read up on bubbling versus tunneling events (e.g., MouseDown vs. PreviewMouseDown), and how events stop routing once they are marked as Handled.
As an alternative, you may be able to use a simple MouseGesture with a MouseAction of LeftClick and Modifiers of Control. It really depends on what your UI looks like, and whether you want a chance at handling the event before any descendant controls. Using a mouse gesture should work in (mostly) the same scenarios as handling the bubbling MouseDown event. But if you need to rely on tunneling events like PreviewMouseDown, then a gesture wouldn't work.
Do something like this, with a flag:
bool _isControlPressed = false;
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key.Equals(Key.LeftCtrl))
{
_isControlPressed = true;
}
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key.Equals(Key.LeftCtrl))
{
_isControlPressed = false;
}
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 1 && _isControlPressed)
{
//do something
}
}
The _isControlPressed flag is set/unset in the key down/key up events. You can then use this flag in other methods to determine the key state.

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 to Cancel an Event (EventArgs e) that is not bound to a WindowsForm

I have a code to detect keyboard input on a plugin
private void Plugin_Load(object sender, EventArgs e)
{
mouse = new MouseInput();
mouse.MouseMoved += mouse_MouseMoved;
}
void mouse_MouseMoved(object sender, EventArgs e)
{
if (testBool== true)
{
return; // Trying to cancel mouse event
}
}
I need to cancel event e(EventArgs), but return does not seems to work.
MouseEventArgs is a Winform event so doesn't works on my code.
Kindly suggest on how to cancel the event.
Thanks

textBox Action Methods

I'm working on a program that allows the user to enter a barcode via scanner and then do stuff, and I've got most of it worked out, I just can't figure out which action method for textBox1 would allow me to do something when "Enter" was hit while in the textBox. I've looked at the description of most of the actions, and I can't find one that sounds like it would work.
Is there one that would work? Or do I just have to check every time a key is pressed?
You want the KeyDown / OnKeyDown or KeyUp/OnKeyUp event, just filter for the right key:
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Enter)
{
// Do Something
}
}
Or in your case since your parent form is most likely subscribing to the TextBox event then you would add a method like the following using the designer:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// Do Something
}
}
Keep in mind that what you are calling "Action Methods" are called Events.
Try this, using the KeyUp event:
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
DoSomething();
}
}
try handler the keypress event.
stop the handler and work better.
using System;
using System.Windows.Forms;
public class Form1: Form
{
public Form1()
{
// Create a TextBox control.
TextBox tb = new TextBox();
this.Controls.Add(tb);
tb.KeyPress += new KeyPressEventHandler(keypressed);
}
private void keypressed(Object o, KeyPressEventArgs e)
{
// The keypressed method uses the KeyChar property to check
// whether the ENTER key is pressed.
// If the ENTER key is pressed, the Handled property is set to true,
// to indicate the event is handled.
if (e.KeyChar != (char)Keys.Enter)
{
e.Handled = true;
}
}
public static void Main()
{
Application.Run(new Form1());
}
}

Alternative to set focus from within the Enter event

I have a textbox and in some cases in Enter event I need to set the focus to a different textbox.
I tried that code:
private void TextBox1_Enter(object sender, EventArgs e)
{
if(_skipTextBox1) TextBox2.Focus();
}
But this code doesn't work. After that I found on MSDN:
Do not attempt to set focus from within the Enter, GotFocus, Leave, LostFocus, Validating, or Validated event handlers.
So how can I do it other way?
Postpone executing the Focus() method until after the event is finished executing. Elegantly done by using the Control.BeginInvoke() method. Like this:
private void textBox2_Enter(object sender, EventArgs e) {
this.BeginInvoke((MethodInvoker)delegate { textBox3.Focus(); });
}
You could handle the KeyPress event instead:
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
e.Handled = true;
TextBox2.Focus();
}
}
textBox.Select();
or
textBox.Focus();
or
set TabIndex = 0 from properties of that textBox.
both methods are use to set focus on textBox in C#, .NET

Categories

Resources