Results 1 to 4 of 4

Thread: Menu Item Appear on Key Press

  1. #1

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    45

    Menu Item Appear on Key Press

    Hi,

    I am trying to get a menu item only appear on a certain key press and then disappear after a certain time.
    this is working for the first time only once hidden it will not appear.

    I am using RegisterHotKey on the keydown event please see the code below if anyone has a better way of getting this achieved please let me know.

    Code:
        Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
            If RegisterHotKey(Me.Handle, 100, MOD_Alt, Keys.X) Then 'alt+x
                MenuStrip1.Visible = True
                Timer1.Enabled = True
                Timer1.Start()
            End If
        End Sub
    Code:
       Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If m.Msg = WM_HOTKEY Then
                Dim id As IntPtr = m.WParam
                Select Case (id.ToString)
                    Case "100"
                        MessageBox.Show("Alt+X Key Press Detected Loading Admin Center", "Admin Center", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End Select
            End If
            MyBase.WndProc(m)
        End Sub
    Code:
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            MenuStrip1.Visible = False
            Timer1.Stop()
        End Sub
    Code:
       Public Const MOD_Alt As Integer = &H1
        Public Const WM_HOTKEY As Integer = &H312
        <DllImport("User32.dll")>
        Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
        End Function
        <DllImport("User32.dll")>
        Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
        End Function
    Cheers James

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,471

    Re: Menu Item Appear on Key Press

    Uuuuhh.... yeah... that's not how RegisterHotKey works... You should be registering your hot keys on FormLoad ... then in the WndProc you handle the hot key that was pressed. You should NOT keep registering the hot key on every singe keypress... What's happening is the first time, it registers, returns a code that allows it to enter your If statement, but on subsequent registers, it's already registered, so it returns a different value (presumably 0) so it skips the If statement... that's why it looks like it doesn't work. But the problem is that it's in the wrong spot to begin with.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,182

    Re: Menu Item Appear on Key Press

    Code:
    Dim id As IntPtr = m.WParam
                Select Case (id.ToString)
                    Case "100"
    ???

    Code:
     Select Case (m.WParam.ToInt32)
                    Case 100

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,182

    Re: Menu Item Appear on Key Press

    BTW… You don’t even need the parentheses on the Select Case…

    Code:
    Select Case m.WParam.ToInt32

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width