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"
private void frmMain_KeyUp(object sender, KeyEventArgs e)
{
try
{
if (e.KeyCode == Keys.Escape)
{
This.Close;
}
if (e.KeyData == (Keys.Control | Keys.S))
{
if (boolIPWrite == true)
{
BtSave_Click(sender, e);
}
}
if (e.KeyCode == Keys.F1)
{
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
{ }
}
Note: Works in Windows and Linux (with mono installed).
My name is Nelson Souto, I develop software for fun and profit since 1992.