Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to use the i.e Altr+A as a save key. Like if the user press the Key Altr+A the entries become save.

I want to write as public function so that user will press this Altr+A key at any time in a Entry mode will save the entry. I want to capture this key.


Best regards,
Jatinder Gupta.
Posted

Sorry...are you simply saying that you want to have a shortcut in your WinForms project?

You didn't say, but if this is a Windows Forms application, then you can easily set up a shortcut key.

If you already have menus set up, then go to your Save Menu item and set it's ShortcutKeys property.

Alternatively, you could hook the form's KeyDown and check for Alt+A and then run your save routine. (KeyDown uses KeyEventArgs which has a Control and Alt property that you can check to see if they're pressed).
 
Share this answer
 
v2
Hi,
You can get the help from following link:
MSIL
//Key:   http://msdn.microsoft.com/en-us/library/ms927178.aspx<br />
       //Code:  http://social.msdn.microsoft.com/forums/en-US/vbgeneral<br />



General GlobalHotkey class

VB
public class GlobalHotkeys
    {
        //Virtual-Key Codes
        //Key:   http://msdn.microsoft.com/en-us/library/ms927178.aspx
        //Code:  http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/5197e0ee-ef85-4f9e-8652-631b8a80e330/
        //The following table shows the symbolic constant names, hexadecimal values,
        //and keyboard equivalents for the virtual-key codes used by the Microsoft Windows CE operating system.
        //The codes are listed in numeric order.
        //You can combine any of the codes with a modifier key to create a hot key.
        //----------------------------------------------------------------------------------------
        //Symbolic constant Hexadecimal     Mouse or keyboard equivalent
        //----------------------------------------------------------------------------------------
        //VK_LBUTTON        01              Left mouse button
        //VK_RBUTTON        02              Right mouse button
        //VK_CANCEL         03              Control-break processing
        //VK_MBUTTON        04              Middle mouse button on a three-button mouse
        //VK_BACK           08              BACKSPACE key
        //VK_TAB            09              TAB key
        //VK_CLEAR          0C              CLEAR key
        //VK_RETURN         0D              ENTER key
        //VK_SHIFT          10              SHIFT key
        //VK_CONTROL        11              CTRL key
        //VK_MENU           12              ALT key
        //VK_PAUSE          13              PAUSE key
        //VK_CAPITAL        14              CAPS LOCK key
        //VK_ESCAPE         1B              ESC key
        //VK_SPACE          20              SPACEBAR
        //VK_PRIOR          21              PAGE UP key
        //VK_NEXT           22              PAGE DOWN key
        //VK_END            23              END key
        //VK_HOME           24              HOME key
        //VK_LEFT           25              LEFT ARROW key
        //VK_UP             26              UP ARROW key
        //VK_RIGHT          27              RIGHT ARROW key
        //VK_DOWN           28              DOWN ARROW key
        //VK_SNAPSHOT       2C              PRINT SCREEN key
        //VK_INSERT         2D              INS key
        //VK_DELETE         2E              DEL key
        //VK_HELP           2F              HELP key
        //VK_NUMPAD0        60              Numeric keypad 0 key
        //VK_NUMPAD1        61              Numeric keypad 1 key
        //VK_NUMPAD2        62              Numeric keypad 2 key
        //VK_NUMPAD3        63              Numeric keypad 3 key
        //VK_NUMPAD4        64              Numeric keypad 4 key
        //VK_NUMPAD5        65              Numeric keypad 5 key
        //VK_NUMPAD6        66              Numeric keypad 6 key
        //VK_NUMPAD7        67              Numeric keypad 7 key
        //VK_NUMPAD8        68              Numeric keypad 8 key
        //VK_NUMPAD9        69              Numeric keypad 9 key
        //VK_MULTIPLY       6A              Multiply key
        //VK_ADD            6B              Add key
        //VK_SEPARATOR      6C              Separator key
        //VK_SUBTRACT       6D              Subtract key
        //VK_DECIMAL        6E              Decimal key
        //VK_DIVIDE         6F              Divide key
        //VK_F1             70              F1 key
        //VK_F2             71              F2 key
        //VK_F3             72              F3 key
        //VK_F4             73              F4 key
        //VK_F5             74              F5 key
        //VK_F6             75              F6 key
        //VK_F7             76              F7 key
        //VK_F8             77              F8 key
        //VK_F9             78              F9 key
        //VK_F10            79              F10 key
        //VK_F11            7A              F11 key
        //VK_F12            7B              F12 key
        //VK_F13            7C              F13 key
        //VK_F14            7D              F14 key
        //VK_F15            7E              F15 key
        //VK_F16            7F              F16 key
        //VK_F17            80H             F17 key
        //VK_F18            81H             F18 key
        //VK_F19            82H             F19 key
        //VK_F20            83H             F20 key
        //VK_F21            84H             F21 key
        //VK_F22            85H             F22 key(PPC only) Key used to lock device.
        //VK_F23            86H             F23 key
        //VK_F24            87H             F24 key
        //VK_NUMLOCK        90              NUM LOCK key
        //VK_SCROLL         91              SCROLL LOCK key
        //VK_LSHIFT         0xA0            Left SHIFT
        //VK_RSHIFT         0xA1            Right SHIFT
        //VK_LCONTROL       0xA2            Left CTRL
        //VK_RCONTROL       0xA3            Right CTRL
        //VK_LMENU          0xA4            Left ALT
        //VK_RMENU          0xA5            Right ALT
        #region Enum
        public enum Key
        {
            NumpadDecimal = 1,
            NumpadMultiply,
            Escape,
            End,
            CtrlR,
            CtrlF,
            CtrlC,
            CtrlI
            /*NumpadEnter,
            NumpadDelete,
            NumpadSubtract,
            NumpadAdd,
            NumpadD1,
            NumpadD2,
            NumpadD3,
            NumpadD4,
            NumpadD5,
            NumpadD6,
            NumpadD7,
            NumpadD8,
            NumpadD9*/
        }
        public enum SymbolicKey
        {
            None = 0,
            Alt = 1,
            Ctrl = 2,
            Shift = 4
        }
        #endregion
        #region fields
        //DEL key
        public const int VK_DECIMAL = 0x6e;
        //Multiply key
        public const int VK_MULTIPLY = 0x6a;
        //Alt key
        //public static int MOD_ALT = 0x1;
        //public static int MOD_CONTROL = 0x2;
        //public static int MOD_SHIFT = 0x4;
        public static int MOD_WIN = 0x8;
        public static int WM_HOTKEY = 0x312;
        //public static int MOD_NONE = 0x0;
        //private static int keyId;
        #endregion
        [DllImport("user32.dll")]
        public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
        [DllImport("user32.dll")]
        public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
}



Register HotKey on form load

C#
private void frmMain_Load(object sender, EventArgs e)
        {
            GlobalHotkeys.RegisterHotKey(this.Handle, Convert.ToInt32(GlobalHotkeys.Key.NumpadDecimal), Convert.ToInt32(GlobalHotkeys.SymbolicKey.None), Convert.ToInt32(Keys.Decimal));
}


UnRegister HotKey on form closing

C#
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
       {
           GlobalHotkeys.UnregisterHotKey(this.Handle, Convert.ToInt32(GlobalHotkeys.Key.NumpadDecimal));
}
 
Share this answer
 
Comments
William Winner 26-Jul-10 14:40pm    
This is a great entry if you want to do a global hook...but it seems more like he just wants a shortcut for his form, not a global hook.

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