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!
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.
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.