Results 1 to 3 of 3

Thread: [RESOLVED] [2008] Best way to use GetAsyncKeyState

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Resolved [RESOLVED] [2008] Best way to use GetAsyncKeyState

    I'm creating a program that takes screen shots for the user, then automatically saves them into a folder for the user.

    My program will remain in the system tray (Unless they close it).

    I've got an option that allows the user to set their own Hotkeys for the program to trigger an event to record the screen shot.

    Since this is not local to the program, the program needs to "Listen" for the user to press the keys they have entered in.

    The problems are:
    1. Will using a timer with a low interval to listen to for the keys use a lot of memory?

    2. How can I listen for multiple keys at the same time? Such as "Ctrl + Shift + ~"?

    I don't want to have to do a for loop and go through every keycode passing it through the GetAsyncKeyState API, so if there is an easier way, or another way to achieve my goal, then I would like to know please .

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: [2008] Best way to use GetAsyncKeyState

    Code:
    Public Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Integer) As Integer
    
        Public Function checkForCombination(ByVal keyString As String) As Boolean
    
            Const keyCTRL = 17
            Const keyALT = 18
            Const keyShift = 16
    
            Dim eachKey() As String
    
            eachKey = Split(keyString, " + ")
    
            Dim keySuccess(0 To UBound(eachKey)) As Boolean
    
            For I = 0 To UBound(eachKey) - 1
    
                eachKey(I) = Replace(LCase(eachKey(I)), "ctrl", keyCTRL)
                eachKey(I) = Replace(LCase(eachKey(I)), "alt", keyALT)
                eachKey(I) = Replace(LCase(eachKey(I)), "shift", keyShift)
    
                keySuccess(I) = getPressedKey(eachKey(I))
    
            Next I
    
            eachKey(UBound(eachKey)) = Asc(eachKey(UBound(eachKey)))
    
            If getPressedKey(eachKey(UBound(eachKey))) Then
    
                keySuccess(UBound(eachKey)) = True
    
            End If
    
            Debug.Print(UBound(eachKey))
    
            For I = 0 To UBound(eachKey)
    
                Debug.Print(keySuccess(I), I)
    
                If keySuccess(I) = False Then Return False
    
            Next I
    
            Return True
    
        End Function
    
        Public Function getPressedKey(ByVal keyCodes As Integer) As Boolean
    
            If GetAsyncKeyState(keyCodes) Then
                Return True
            End If
    
            Return False
    
        End Function
    I just called the checkForCombination function with a timer.

    This is the best I could do .

  3. #3
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [RESOLVED] [2008] Best way to use GetAsyncKeyState

    You don't need any of that. There is a way to have a global shortcut to inform your program. See the RegisterHotKey example by TGOSeraph.
    VB 2005, Win Xp Pro sp2

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