Results 1 to 3 of 3

Thread: hotkeys?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2000
    Posts
    33
    Im looking at putting hotkey into my program. My program consists of a list of objects. I want to be able to assign hotkeys for each of these objects in the listbox. Another issue here i want my hotkeys to be on the number pad only and in a specific format. e.g. i wish to be able to press
    +1+ in succession not at the same time, and by doing this access the first item in my list for the next item +2+ and so on. please someone point me to somewhere i can get help or someone. thanks

  2. #2
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    Try this

    This uses some API's to get the key pressed from anywhere in the system (including other applications).

    It saves the last two key presses in static variables, which are checked (along with the current key) in a function to see if the three keys match the desired combination.

    The keypress checking goes on in a timer, so drop a timer on a form and paste the following into the form's code window. Then start pressing keys. It will give you a message when you press "+1+" or "+2+".
    Code:
    Option Explicit
    
    'Add a Timer to a Form..
    Private Declare Function GetAsyncKeyState Lib "user32" _
        (ByVal vKey As Long) As Integer
    Private Declare Function GetKeyboardState Lib "user32" _
        (pbKeyState As Byte) As Long
    Private Declare Function ToAscii Lib "user32" _
        (ByVal uVirtKey As Long, ByVal uScanCode As Long, lpbKeyState As Byte, _
        lpwTransKey As Long, ByVal fuState As Long) As Long
    
    Private Const VK_SHIFT = &H10   'Used by Win9x
    Private Const VK_LSHIFT = &HA0  'Used by NT
    Private Const VK_RSHIFT = &HA1  'Used by NT
    
    Private Sub Form_Load()
        'Need a Very Low Interval to Ensure the Keys are Captured in the Correct Order
        Timer1.Interval = 10
        Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
        Dim iKey As Integer
        Dim iAsc As Long
        Dim bKeys(255) As Byte
        Static FirstKey As Long     ' Holds the second to last key the user pressed
        Static SecondKey As Long    ' Holds the last key the user pressed
        
        For iKey = 1 To 255
            'Use GetAsyncKeyState to Monitor Keypress From Anywhere in the O/S.
            If GetAsyncKeyState(iKey) And iKey <> VK_LSHIFT And _
                iKey <> VK_RSHIFT And iKey <> VK_SHIFT Then
                    Exit For
            End If
        Next
        If iKey < 256 Then
            'Get the Current Keyboard State For the Shift Keys Etc..
            GetKeyboardState bKeys(0)
            Do While GetAsyncKeyState(iKey)
                DoEvents    ' Waiting for key to be pressed
            Loop
            'Convert the Key to it's ASCII Equivilant and store in iAsc
            ToAscii iKey, 0&, bKeys(0), iAsc, 0&
            If iAsc Then
                ' A key was pressed so determine if it was a hotkey sequence
                ' and handle it appropriately.
                Select Case GetHotKey(FirstKey, SecondKey, iAsc)
                    Case 1
                        ' User pressed +1+. Execute code for that hotkey here
                        MsgBox "You pressed +1+"
                    Case 2
                        ' User pressed +2+. Execute code for that hotkey here
                        MsgBox "You pressed +2+"
                End Select
                
                'Re-assign first and second keys
                FirstKey = SecondKey
                SecondKey = iAsc
            End If
        End If
    End Sub
    
    Private Function GetHotKey(FirstKey As Long, SecondKey As Long, ThirdKey As Long) As Integer
        
        ' This function returns 0 if no hotkey was pressed, 1 if "+1+" was pressed, 
        ' and 2 if "+2+" was pressed.
    
        If Chr(ThirdKey) = "+" Then
            ' User pressed plus sign, so check if they already pressed "+" and a number
            If Chr(FirstKey) = "+" Then
                ' Now check to see what number they pushed
                Select Case Chr(SecondKey)
                    Case "1"
                        GetHotKey = 1
                    Case "2"
                        GetHotKey = 2
                    Case Else
                        GetHotKey = 0
                End Select
            End If
        End If
    End Function

    [Edited by seaweed on 06-23-2000 at 07:42 PM]
    ~seaweed

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2000
    Posts
    33
    Ok, this works, but how would i get it to work for any other number other than 1 or 2?

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