Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to Use Function Keys in Windows application (C#).
Posted

C#
private void <Control>_KeyDown(object sender, KeyEventArgs e)
       {
           switch (e.KeyCode)
           {
               case Keys.F1:
                   break;

               case Keys.F10:
                   break;
               case Keys.F11:
                   break;
               case Keys.F12:
                   break;
               default:
                   break;
           }
       }



this might help you.
http://stackoverflow.com/questions/1707040/handling-function-key-press[^]
 
Share this answer
 
v3
Hi,

You can use FunctionKeys as shortcuts in your windows application. Just write a KeyDown event for your control and then you match the pressed key

For e.g
C#
if (e.KeyValue >= 112 && e.KeyValue <= 123)
{
MessageBox.Show("You pressed F" + (e.KeyValue - 111).ToString());
} 

or

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
 if (e.KeyCode == Keys.Enter)
  { MessageBox.Show("Enter key pressed"); }
}


Hope this helps !! :)

Regards,
Praneet
 
Share this answer
 
Comments
chetna2810 29-Oct-14 1:57am    
Hello,
You have used KyeDown Event on textbox, But i want to use function keys.
Like If i press F6 then new form will open.
[no name] 29-Oct-14 1:59am    
Where you want that F6 event ? on screen or on a control ?
chetna2810 29-Oct-14 2:04am    
i want to use these F6 on Screen. Like one form is open and by pressing f6 i want to close this form and open the new one.
[no name] 29-Oct-14 2:10am    
See my next solution
Hi,
In your form Load event, try it like this:
C#
private void Form1_Load(object sender, EventArgs e)
{
     this.Focus();
     this.KeyPreview = true;
     this.KeyDown += new KeyEventHandler(Form1_KeyDown);
}

void Form1_KeyDown(object sender, KeyEventArgs e)
{
  // Your logic goes here
}


Hope this helps !!

Regards,
Praneet
 
Share this answer
 
Comments
chetna2810 29-Oct-14 3:25am    
Yes, sir it is working.
[no name] 29-Oct-14 4:12am    
I am glad that it worked :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900