Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

handling function key press

Tags:

I have a C# form with 5 buttons. The users enters the information and depending on the press of a function key, a specific action is performed. F9-Execute Order, F6-Save, F3-LookUp.

I have added the foolowing code:

OnForm_Load

this.KeyUp += new System.Windows.Forms.KeyEventHandler(KeyEvent); 

and

private void KeyEvent(object sender, KeyEventArgs e) //Keyup Event      {         if (e.KeyCode == Keys.F9)         {             MessageBox.Show("Function F9");          }         if (e.KeyCode == Keys.F6)         {             MessageBox.Show("Function F6");         }         else             MessageBox.Show("No Function");      } 

but nothing happens

Thanks

like image 798
Tj. Avatar asked Nov 10 '09 10:11

Tj.


People also ask

How do you call a function on key press?

You can execute a function by pressing the enter key in a field using the key event in JavaScript. If the user presses the button use the keydown to get know its enter button or not. If it enters the key then call the JavaScript function.

What is key press?

keypress (plural keypresses) The depression of an input key; a keystroke. (computing) The buffered electrical signal resulting from such an event, sometimes distinguished from the release.

What does keypress do in JavaScript?

The keypress event is fired when a key that produces a character value is pressed down. Examples of keys that produce a character value are alphabetic, numeric, and punctuation keys. Examples of keys that don't produce a character value are modifier keys such as Alt , Shift , Ctrl , or Meta .


1 Answers

You need to set

KeyPreview=True 

for your form. Otherwise key press is swallowed by the control that has focus.

like image 114
yu_sha Avatar answered Sep 28 '22 11:09

yu_sha