Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai all,

How to check the pressed key is a function key or not..?

please reply me..

Thanks in advance..
Posted
Updated 19-Oct-11 2:07am
v2

You should be able to just write

C#
void KeyUpEvent(object sender, KeyEventArgs e)
{
    if (e.KeyCode >= Keys.F1 && e.KeyCode <= Keys.F24)
    {
    }
}
 
Share this answer
 
Comments
Simon Bang Terkildsen 19-Oct-11 7:03am    
+5
C#
public static bool IsFunctionKey(Keys k)
{
    return k >= Keys.F1 && k <= Keys.F24;
}
 
Share this answer
 
Comments
Reiss 19-Oct-11 7:07am    
+5, I did think about going down the extension route, but I can't think of a scenario whereby I need to check if any function key has been pressed.
Simon Bang Terkildsen 19-Oct-11 7:17am    
Very true, I was about to post it without the method signature so just k >= Keys.F1 && k <= Keys.F24 but I thought it would be more clear if I put it into a method.
set KeyPreview = True

add the KeyUp event as below in InitializeComponent() or Form2_Load
C#
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form2_KeyUp);

in code behind add
C#
private void Form2_KeyUp(object sender, KeyEventArgs e)
{
  if (e.KeyCode == Keys.F1 || e.KeyCode == Keys.F2 || e.KeyCode == Keys.F3 ||e.KeyCode == Keys.F4 ||
      e.KeyCode == Keys.F5 ||e.KeyCode == Keys.F6 ||e.KeyCode == Keys.F7 ||e.KeyCode == Keys.F8 ||
      e.KeyCode == Keys.F9 ||e.KeyCode == Keys.F10 ||e.KeyCode == Keys.F11 ||e.KeyCode == Keys.F12 )
     {
        MessageBox.Show(string.Format("Function Key {0} is pressed", e.KeyCode));
     }
}
 
Share this answer
 
v2
Comments
Reiss 19-Oct-11 6:29am    
There are 24 Function keys available
version_2.0 19-Oct-11 6:32am    
need some simple and smart code...

5+ to Reiss..
Philippe Mori 19-Oct-11 8:37am    
Has anyone found a keyboard that actually has more than 12 functions keys? Also anyone could easily extend this code to support all 24 function keys...

That code will works... Other code assumes that Keys.F1 to Keys.F24 are consecutives which is not documented in the official MSDN documentation thus to be sure, one has to "go to definition" of the enumeration to confirm that.
Try this
on form load
C#
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form2_KeyUp);


code behind
C#
private void Form2_KeyUp(object sender, KeyEventArgs e)
{
     if (e.KeyCode.In(Keys.F1, Keys.F2, Keys.F3, Keys.F4, Keys.F5, Keys.F6,  
         Keys.F7, Keys.F8, Keys.F9, Keys.F10, Keys.F11, Keys.F12))
         {
            MessageBox.Show(string.Format("Function Key {0} is pressed", e.KeyCode));
         }
}


in project namespace
C#
public static class Ext
{
    public static bool In<t>(this T t, params T[] values)
    {
        return values.Contains(t);
    }
}
 
Share this answer
 
v5
Comments
Philippe Mori 19-Oct-11 8:43am    
The code in itself is not wrong (well you might support all 24 function keys) but is probably relatively slow as you have to create an array each time there is a KeyUp event. At least, the array should be a member of the form. Also, it the array is sorted, then search code be made more efficient.
You can test if the KeyCode of KeyEventArgs is a function key like this :-

C#
void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            string keyCode = e.KeyCode.ToString();
            if (keyCode.StartsWith("F") && keyCode.Length > 1)
            {
                MessageBox.Show("you pressed function key");
            }
        }


(I assume this is Winforms)


You need to set the forms KeyPreview proeprty to true else the form does not handle the event.
Hope this helps
 
Share this answer
 
Comments
Philippe Mori 19-Oct-11 8:44am    
That code will have false positive as some enum value starting with F are not a function key. If that solution is used, then you might want to check that F is followed by a number.

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