TeeJay
TeeJay

Reputation: 119

Hotkeys not working anymore

I was using hot keys, but out of nowhere, they are not working anymore. It's very confusing.

It happened when my friend checked my code through team-viewer. Then it stopped working.

Public Class Form2

Public Const MOD_ALT As Integer = &H1 'Alt key
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

Private Sub Form2_Load(ByVal sender As System.Object, _
                    ByVal e As System.EventArgs) Handles MyBase.Load
    RegisterHotKey(Me.Handle, 100, MOD_ALT, Keys.E)
    RegisterHotKey(Me.Handle, 200, MOD_ALT, Keys.A)
    Me.TopMost = True
    Me.ShowInTaskbar = False
    Me.TransparencyKey = Me.BackColor
    Dim leftpos As Long
    Dim toppos As Long
    leftpos = (My.Computer.Screen.WorkingArea.Right - 2) - Me.Width
    toppos = (My.Computer.Screen.WorkingArea.Bottom - 2) - Me.Height
    Me.Location = New Point(leftpos, toppos)
End Sub

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"
                Application.Exit()
            Case "200"
                Form3.Show()

        End Select
    End If
    MyBase.WndProc(m)
End Sub

Private Sub Form2_FormClosing(ByVal sender As System.Object, _
                    ByVal e As System.Windows.Forms.FormClosingEventArgs) _
                    Handles MyBase.FormClosing
    UnregisterHotKey(Me.Handle, 100)
    UnregisterHotKey(Me.Handle, 200)
End Sub


Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
    If e.KeyCode = Keys.Enter Then

        Form3.Show()
        Form3.Activate()

    End If
End Sub
End Class

Upvotes: 1

Views: 201

Answers (1)

TeeJay
TeeJay

Reputation: 119

i fixed some api function signatures with the help of a good guy IronRazer in dreamincode as per the below

 <DllImport("user32.dll", EntryPoint:="RegisterHotKey")> _
Private Shared Function RegisterHotKey(ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

<DllImport("user32.dll", EntryPoint:="UnregisterHotKey")> _
Private Shared Function UnregisterHotKey(ByVal hWnd As IntPtr, ByVal id As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

  and it worked fine.

Upvotes: 0

Related Questions