Click here to Skip to main content
15,888,527 members
Articles / Desktop Programming / Windows Forms
Article

Enabling Hot Keys (Short Cut keys) for windows Application

Rate me:
Please Sign up or sign in to vote.
1.54/5 (7 votes)
30 Jun 2008CPOL 48.3K   12   5
Enabling Hot Keys (Short Cut keys) for windows Application

Introduction

In .Net Windows Application Short Keys are not enabled automatically.

Background

Usually the underline appears only after you press the ALT Key, but you can enable it by changing the Operating System Settings. On Windows XP, Right Click Desktop to bring up the Display Properties Dialog and then choose Appearance tab and then the Effects Button and uncheck the checkbox "Hide Underlined letters for keyboard navigation until I press the ALT Key".

But how to do it programmatically? The below code demonstrate how to do that!!!

Using the code

Generally in Windows Projects, we will have General.cs Class which contains general functions, Constants, Delegates, and Global variables which are used by other classes of Application.

Paste code in General.cs

C#
[DllImport ("user32.dll")] 
static extern void SystemParametersInfo(uint uiAction, uint uiParam, ref int pvParam, 
                                        uint fWinIni); 

 
/* Constants used for User32 calls. */ 
const uint SPI_SETKEYBOARDCUES = 0x100B; 

/// <summary> 
/// Change the setting programmatically 
/// for keyboard shortcut Issue 
/// </summary> 
private static void GetAltKeyFixed() 
{ 
   int pv = 1; 
   /* Call to systemparametersinfo to set true of pv variable.*/ 
   SystemParametersInfo(SPI_SETKEYBOARDCUES, 0, ref pv, 0); 
   //Set pvParam to TRUE to always underline menu access keys, 
}

Please add

C#
using System.Runtime.InteropServices; 

assembly in General.cs

and call GetAltKeyFixed in Program.cs before Application.Run(new <FormNameWhichIsToBeDisplayed>);

Or copy below code before Application.Run(new ......)

C#
General _General=new general();
_General.GetAltKeyFixed();

Points of Interest

This issue is seems to be there for all .net windows applications.

Adding Two lines of code, will fix Displaying Underline Issue for Shortcut keys of application

License

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


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Member 1426888415-Jan-24 7:02
Member 1426888415-Jan-24 7:02 
QuestionRe: What if your windows app requires they be visible Pin
John988-Oct-15 12:32
John988-Oct-15 12:32 
GeneralCut and Paste a Control a control C#.net Pin
Saivjil16-Nov-08 19:59
Saivjil16-Nov-08 19:59 
GeneralUser hostile Pin
PIEBALDconsult30-Jun-08 4:09
mvePIEBALDconsult30-Jun-08 4:09 
GeneralRe: User hostile Pin
Ben Robbins10-Jun-09 20:48
Ben Robbins10-Jun-09 20:48 
I agree with this sentiment in general, but sometimes you need to do these things (for example if it is mandated in a corporate environment and the setting in the user's profile is being ignored by your terminal server software).

The code presented in this article does exactly what it says it does and worked no problems for me.

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.