How to use key pressing on a form (Escape, Control and Help F1) (Windows and Linux)

from —-> codeproject.com —->Nelson Kosta Souto                                                                    There are various ways to use key pressing on a form using C#.
Here is an example:

Select properties of Form and select ‘KeyPreview‘ and change it from ‘false‘ to ‘true‘. By default, its value is false. Then write the below code in the KeyUp event of the Form:

 Collapse
public static string HelpPath = Application.StartupPath + Path.DirectorySeparatorChar + "Help" // Help Folder Name

// Folders Structure
// Exe File
//    |-> Help Folder
//            | -> HelpFile.chm

private void frmMain_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Escape) // Escape button
                {
                    This.Close;
                }

                if (e.KeyData == (Keys.Control  | Keys.S)) // Control + S
                {
                    if (boolIPWrite == true) // Permission to write
                    {
                        BtSave_Click(sender, e); // Save button
                    }
                }

                if (e.KeyCode == Keys.F1) // F1 - Help Information
                {
                    try
                    {
                        System.Diagnostics.Process.Start(strHelpPath + Path.DirectorySeparatorChar + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString() + ".chm");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("ERROR # " + ex.Message.ToString() + Environment.NewLine + Environment.NewLine + strHelpPath + Path.DirectorySeparatorChar + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString() + ".chm", this.Text.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            catch
            { }
        }
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s