Results 1 to 3 of 3

Thread: GetAsyncKeyState with Hotkeys

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    15

    GetAsyncKeyState with Hotkeys

    Code:
            If CheckBox_StartHotkey.Checked = True Then
                If Not ComboBox_StartHotkey.Text = Nothing Then
                    Dim StartHotKey As Boolean
                    StartHotKey = GetAsyncKeyState(ComboBox_StartHotkey.Text)
                    If StartHotKey = True Then
                        StartActions()
                    End If
                End If
            End If
    I am trying to input the function key that a user has selected from a ComboBox (containing the function keys F1~F12 as options) into the GetAsyncKeyState(). However, since the ComboBox_StartHotkey.Text is a string, it cannot be converted into Long data type needed for the GetAsyncKeyState() function. I have tried Conversion and Parse Methods but what I try doesn't seem to work. Anyone know how to effectively convert a String (such as F6) into a Long data type for using in the GetAsyncKeyState() function in this case?

    I used the tried to use the ItemData property to hold the Key Code representing the Function Key...
    Code:
    Dim intCount As Integer
    For intCount = 1 To 12
        ComboBox_StartHotkey.AddItem "F" & Cstr(intCount)
        ComboBox_StartHotKey.ItemData(ComboBox_StartHotKey.NewIndex) = 105 + intCount
    Next intCount
    ...then change the original code to...
    Code:
            If CheckBox_StartHotkey.Checked = True Then         
                If ComboBox_StartHotkey.ListIndex <> -1 Then
                    Dim StartHotKey As Boolean
                    StartHotKey = GetAsyncKeyState(ComboBox_StartHotkey.ItemData(ComboBox_StartHotKey.ListIndex))
                    If StartHotKey = True Then
                        StartActions()
                    End If
                End If
            End If
    ...but this code is not for my version of Visual Basic. Anyone know how to convert it? Thanks for helping!

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

    Re: GetAsyncKeyState with Hotkeys

    There isn't one. You'd just have to do it manually.

    A better idea would be to start with an Integer (which is what GetAsyncKeyState expects, NOT Long) and convert that to a String for display. Try defining an Enum where each field has the name you want to display to the user and the value you want to pass to GetAsyncKeyState. You can then display those values as strings in your UI and pass them directly to GetAsyncKeyState. You can even declare GetAsyncKeyState such that the parameter is your Enum type.
    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: GetAsyncKeyState with Hotkeys

    That said, if you're trying to create hotkeys then you should be using the RegisterHotKey API, not GetAsyncKeyState.

    Also, you might look at the existing Keys enumeration, which contains values for all keys. I would guess that it matches the Windows virtual key codes, although I've never explicitly 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

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