locked
How To Enable Hot-Keys RRS feed

  • Question

  • I am developing an application in VB.Net.I have a windows form with several buttons like Save,Exit,Delete etc. When the user Press Ctrl+S:Data must Save similarly Ctrl+E:Data must Exit, Ctrl+D:Data must Delete.How to Do this Requirement in Windows Form Using VB.NET.


    Alluru.Midhun Kumar Singh



    Wednesday, May 4, 2011 10:39 AM

Answers

  • Please use the below code:

      Dim altPressed As Boolean = False
      Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, keyData As System.Windows.Forms.Keys) As Boolean
    
        If altPressed Then
          altPressed = False
          If msg.WParam.ToInt32() = CInt(Keys.S) Then
            ' TO DO ...
            Console.WriteLine("Alt+S is pressed")
    
          End If
        End If
    
        If msg.WParam.ToInt32() = &H12 Then 'Alt Key
          altPressed = True
    
          Console.WriteLine("Alt is pressed")
        End If
    
        Return MyBase.ProcessCmdKey(msg, keyData)
      End Function
    


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 16, 2011 3:36 PM

All replies

  • Try using the Keydown event to determine what key was pressed.

    Also set the KeyPreview property for the form to True.


    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code
    Wednesday, May 4, 2011 10:44 AM
  • I didn't get it.If i wrote my code in this event, how should i have to trigger it when user presses Ctrl+S. Can you suggest any examples.


    Alluru.Midhun Kumar Singh



    Wednesday, May 4, 2011 11:20 AM
  • Hi,

    You can use the ProcessCmdkey to define your hot key for your application, for the usage you can ref the following thread:

    http://social.msdn.microsoft.com/Forums/en-GB/csharpgeneral/thread/a3f88ade-af0d-4040-afde-f319c4defd7b

     

    Best wishes,


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 6, 2011 10:58 AM
  • Please use the below code:

      Dim altPressed As Boolean = False
      Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, keyData As System.Windows.Forms.Keys) As Boolean
    
        If altPressed Then
          altPressed = False
          If msg.WParam.ToInt32() = CInt(Keys.S) Then
            ' TO DO ...
            Console.WriteLine("Alt+S is pressed")
    
          End If
        End If
    
        If msg.WParam.ToInt32() = &H12 Then 'Alt Key
          altPressed = True
    
          Console.WriteLine("Alt is pressed")
        End If
    
        Return MyBase.ProcessCmdKey(msg, keyData)
      End Function
    


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 16, 2011 3:36 PM
  • Thanks for the reply Bob!

    Your code is  working very fine but just want to know,What is &H12 means?


    Alluru.Midhun Kumar Singh


    Tuesday, May 17, 2011 8:21 AM
  • It is got from testing, I set a breakpoint and check the WParam while we pressing the "Alt" key. It returned &H12. So we could know the WParam is the Virtual-Key Code.
    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 17, 2011 10:08 AM