On hotkey press program freezes

Elsys

Member
Joined
Jan 7, 2007
Messages
11
Programming Experience
Beginner
Heres the issue I have a listing of hotkeys ctrl and a function key from F3 to F11 and I have statments the execute on the raising of a function key event. Yet whenever I use my hotkeys the program immediately freezes and I cannot understand why?

VB.NET:
  Dim Buff1Run As Boolean = False
    Dim Buff2Run As Boolean = False
    Dim Buff3Run As Boolean = False
    Dim Autolootrun As Boolean = False
    Dim Autoattkrun As Boolean = False
    Dim Autoclickrun As Boolean = False
    Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dX As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
    Private Const LEFT_DOWN = &H2
    Private Const LEFT_UP = &H4
    Dim X As Long, Y As Long, Z As Long, A As Long

#Region "HotKey"
    Dim AtomIds(9) As Int32

    Private Sub SetupHotKey()
        'register hotkeys
        Dim fsModifiers, vkey As Int32
        fsModifiers = Win32.HotkeyModifierFlags.MOD_CONTROL
        'hotkey0 ctrl-f2 'buffa'
        vkey = Win32.VK_F2
        AtomIds(0) = Win32.GlobalAddAtom("JHbuffa")
        If Win32.RegisterHotKey(Me.Handle, AtomIds(0), fsModifiers, vkey) = False Then
            MsgBox("error registering hotkey 0")
        End If
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case (m.Msg)
            Case Win32.WM_HOTKEY
                If m.WParam.ToInt32 = AtomIds(0) Then 'it's my hotkey 'buffa'
                    'do something
                    hotkeybuffa()
                    'set result, message handled
                    m.Result = New IntPtr(1)
                End If
        End Select
        MyBase.WndProc(m)
    End Sub

    Sub hotkeybuffa()
        Buff1Run = Not Buff1Run
        Do While 1 = 1
            If Buff1Run = False Then Return Else 
            SendKeys.Send(KeyConfig.ComboBox1.SelectedValue)
        Loop
    End Sub
    Private Sub DeHotKey()
        Win32.UnregisterHotKey(Me.Handle, AtomIds(0))
        Win32.GlobalDeleteAtom(AtomIds(0))
    End Sub

#End Region 'Hotkey
    Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        DeHotKey()
    End Sub

Friend Class Win32
    Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal Id As Int32, ByVal fsModifiers As Int32, ByVal vkey As Int32) As Boolean
    Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal Id As Int32) As Boolean
    Public Enum HotkeyModifierFlags
        MOD_ALT = &H1
        MOD_CONTROL = &H2
        MOD_SHIFT = &H4
        MOD_WIN = &H8
    End Enum
    Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Int32
    Declare Function GlobalDeleteAtom Lib "kernel32" (ByVal nAtom As Int32) As Int32
    Public Const WM_HOTKEY As Integer = &H312
    Public Const VK_F2 As Int32 = &H71
End Class 'Win32

Even though its still rather long thats the smallest ammount of working code I have nine procedures set up in this way. Including this one it just freezes I dont understand why that is.

As for the value of send you can put anything in there to test it it still freezes.

This is probably really stupid on my part. Some simple thing is probably to blame.
 
"Do While 1 = 1" is always True (use "Do.. Loop" instead), so the code loops until Buff1Run = False. Because you're on main thread the continous loop prevent UI to respond, there is no chance for the thread to handle user input while this is happening. You can run it on another thread or use Application.DoEvents to "let go" every loop and give windows messages time to process.

(by the way, you're using "my" hotkey code, I wrote it 3-4 years ago, those "JH" atom strings are my initials ;))
 
I did not mean it like that I simply meant its mine as in its in my program. Also the loop is continues because I need it to spam keys. But the do while loop will work too. While I'm at it I can probably remove the if statement correct as it was meant to test if one of the run values was true and since the loop is performing that function now their is no need for it.
 
Last edited:
Back
Top