Results 1 to 3 of 3

Thread: Toggle on/off hotkeys

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Toggle on/off hotkeys

    Hello guys.
    I am facing an issue using some piece of code to create a hotkey
    This is what I have which works fine (Checkbox is turnes on/off)

    Module:

    Code:
    Public Declare Function GetAsyncKeyState Lib "USER32" _
        (ByVal vKey As Int32) _
        As Int16
    Form Code:

    Code:
    Public Class Form1
    
    Dim hotkey As Boolean
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    hotkey=true
    End Sub
    
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    If hotkey = True Then
    CheckBox1.Checked = True
    hotkey = False
    Else
    CheckBox1.Checked = False
    hotkey = True
    End If
    
    End Sub
    End Class
    Now, instead of a BUTTON, I use the following code to create a keyboard event

    Code:
    Private Sub KeyLogger_Tick() Handles KeyLogger.Tick
    Dim hotkey As Boolean
    
    hotkey = GetAsyncKeyState(Keys.NumPad1) 
    If hotkey = True Then
    CheckBox1.Checked = True
    hotkey = False
    Else
    CheckBox1.Checked = False
    hotkey = True
    End If
    
    End Sub
    With this code, checkbox is turned on while NumPad1 is holding down. When I let it go, is turned off
    How can I make checkbox turned on when NumPad1 is pressed once, and turned it off when NumPad1 is pressed once again?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Toggle on/off hotkeys

    That's not how you create a hotkey. You use the RegisterHotKey API. That way, you receive a notification if and when the hotkey is pressed. When you receive that notification, you toggle the Checkbox like so:
    Code:
    CheckBox1.Checked = Not CheckBox1.Checked
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Re: Toggle on/off hotkeys

    Well, i found some things about RegisterHotkey API, but I still have an issue to toggle off the hotkey...

    Code:
    Public Class Form1
    
    #Region "REGISTER HOTKEYS"
    
    
    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
    #End Region
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    RegisterHotKey(Me.Handle, 1, 0, Keys.NumPad8) 'REGISTER NUMPAD8
    
    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 "1"
                        RegisterHotKey(Me.Handle, 1, 0, Keys.NumPad8)
                        checkbox1.checked=true
                End Select
            End If
            MyBase.WndProc(m)
    End Sub
    
    End Class
    So, where do I place this flag "CheckBox1.Checked = Not CheckBox1.Checked" to toggle on/off the checkbox?

Tags for this Thread

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