Click here to Skip to main content
12,897,124 members (55,130 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 4 Jul 2011

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

, 4 Jul 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to use key pressing on a form
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:

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)
                if (e.KeyCode == Keys.Escape) // Escape button
                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
                        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);
            { }

Note: Works in Windows and Linux (with mono installed).


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


About the Author

Nelson Kosta Souto
Portugal Portugal
My name is Nelson Souto, I develop software for fun and profit since 1992.

You may also be interested in...


Comments and Discussions

QuestionArticle? Hardly. Snippet at best. Pin
tlhintoq12-Jul-11 6:39
membertlhintoq12-Jul-11 6:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 5 Jul 2011
Article Copyright 2011 by Nelson Kosta Souto
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid