KeyDown event doesnt work [duplicate] - c#

This question already has answers here:
Best way to implement keyboard shortcuts in a Windows Forms application?
(9 answers)
Closed 7 years ago.
I would need that (on a WinForm) if the key A is pressed, an event is triggered.
I got this code from the MSDN site:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode != Keys.A)
{
MessageBox.Show("Key 'a' was pressed.");
}
}
The problem is that this code doesn't work for me, there is no error message, but if I am on the form and press the key A there is no event triggered. I tried to use breakpoints, but it never enters the if.
What is the problem here?

You need to set Form.KeyPreview property to True. This property gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus.
Also note that there is a mistake in your code; according to your message you need to verify if e.KeyCode == Keys.A.

Related

Get Delete Keypress event in Windows Panel control [duplicate]

This question already has answers here:
Panel not getting focus
(6 answers)
Closed 7 years ago.
I have got some controls on the Panel and I am trying to delete them using "Delete" button. I handled KeyPress Event as mentioned in How to get Keypress event in Windows Panel control in C#
and I am getting Event for buttons (A-Z and 1-9) pressed, but not for the Delete, Control/Alt/ Shift or F1, F2.... buttons.
Do we need to do something special for handling these buttons?
Try like this:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
e.Handled = true;
}
}
Also you need to set KeyPreview on.
You can also refer Keyboard.Modifiers Property

Key Down not working in listView [duplicate]

This question already has answers here:
C# Windows Forms code not working - Attach Event to button
(2 answers)
Forms not responding to KeyDown events
(3 answers)
Closed 7 years ago.
It is my second day doing c#...don't judge please. I have read other threads but they did not help.
I have this code:
private void listView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
MessageBox.Show("aa");
}
}
which is not working.
What am I doing wrong?
The issue is that you haven't told the ListView to actually use the event. To do this you have to assign the method to the required event. there are two ways to do this. Either select the ListView and open the Properties tab go to events and double click on the one you want. (Visual studio will automatically put the event out for you). Or in the constructor of the form or elsewhere you can manually assign it. In your case it would look like...
listView1.KeyDown += listView1_KeyDown;
Note you don't have to use a name similar to what visual studio would automatically produce. You can name your method whatever you want as long as the method signature matches the event. This is nice if you have multiple list boxes and want to use the same method to handle all of them. For example you could do something like.
listView2.KeyDown += listView1_KeyDown;
I suggest reading up on how events work in c#.

C# assign hotkey to a button [duplicate]

This question already has answers here:
How to set hotkeys for a Windows Forms form
(8 answers)
Closed 8 years ago.
Currently I want to add a hotkey to my button in C# windows application.
I went to the event, I didn't see anywhere I can assign hotkeys.
I wanna assign "F6' as the hotkey, which means whenever I press F6 it would trigger the button and run the code in it
You could try to handle the Forms Keypress-event (and route all components keypress-events to that)
Assigning hotkeys via the GUI works just for MenuItems, if I'm not mistaken.
Edit: some code:
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.F6)
button1_Click(this, new EventArgs());
}
Just what I quickly tried. If you register to the forms (or any other controls for that matter) KeyUp-event, you can query the released key and act accordingly. I suppose there are more elegant ways to do this, but this should get you started.

KeyEvent doesn't detect any keys C#

I'm currently stumped. I can't seem to get the KeyEvent to work. Simple code like this just won't respond to the key I'm pressing. I've tried KeyDown and KeyPress. No errors while compiling... what is causing this?? It will just let me enter the E key without prompting the MEssage box.
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.E)
{
MessageBox.Show("E");
}
}
I think you should be using the PreviewKeyDown Event, for example, instead of the standard key events, as sometimes these events are blocked an not bubbled up through the control.
You should change the Form KeyPreview property to true where textbox1 is located.

How to mark a pushbutton as a default for Enter key on form elements?

I have a Windows Mobile application in C#.
I have a couple of fields on the form, and when I hit enter I want the form submitted.
Is there a way to mark a pushbutton as default?
Also how can I make so the Down key moves the focus into the next field? Tab-order is not respected?
You can have a method that you link as the KeyDown event on all the controls from that form. In that you check if the KeyCode is Enter then you call the Submit method.
private void control_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
Submit();
}
Same thing, in the KeyDown method you handle the DownArrow key. Im not sure if you have the System.Windows.Forms.Control.SelectNextControl() method in the Compact Framework, but if you don't have it you can easily build something like that by yourself.

Categories