Results 1 to 14 of 14

Thread: Subclassing!!! :(

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    15

    Unhappy

    How do i - without using an OCX - monitor all key presses in windows so my program can 'jump into action' when a certain key combo is pressed for example??

    Is this easy?
    If practice makes perfect...

    and nobody's perfect...

    why the hell practice?

  2. #2
    Guest
    No need to subclass when GetAsyncKeyState is easier.
    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    
    Private Sub Form_Load()
        Timer1.Interval = 1
    End Sub
    
    Private Sub Timer1_Timer()
        If GetAsyncKeyState(vbKeyReturn) Then MsgBox ("You pressed Enter")
    End Sub

  3. #3
    Junior Member
    Join Date
    Nov 1999
    Posts
    23
    This is an interesting idea, but here's another question:
    Is there a way to capture all keys pressed in windows?
    I want to make an app with configurable hot keys (several of them) that will work from anywhere in windows.
    Also, won't the timer loop cause a slight bit of lag on slower machines?
    What I'm looking for is like a KeyDown event that works from anywhere in windows.
    (and NO, i'm NOT trying to make a key logger!


  4. #4
    Guest
    I don't think the Timer will be a problem, seeing how many people have faster machines these days. I tested this on a Pentium 100 and all was fine.

  5. #5
    Lively Member
    Join Date
    Mar 2000
    Location
    Lowestoft
    Posts
    91
    I have often wondered without success how to "hook" the keyboard, so does anyone know how to do this? Yes I am aware of the threat of keyloggers etc. and that by knowing how to hook the keyboard you are creating a keylogger, but how do they work? Please don't flame me, for potentially being a 'hacker' or something I am just interested
    Mag-Net's Home
    Visual Studio 6-Enterprise - SP4
    ICQ: 35519773
    Have Fun

  6. #6
    Guest
    well I made a little keylogger once,
    I used GetAsyncKeyState, with a for loop and a timer,

    basicly this was it


    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    
    Private Sub Form_Load()
      
      Timer1.Enabled = True
      Timer1.Interval = 100
    
    End Sub
    
    Private Sub Timer1_Timer()
    
      For i = 0 To 255
        If GetAsyncKeyState(i) <> 0 Then
          Text1 = Text1 & LCase(Chr(i))
        End If
      Next
    
    End Sub
    it works OK, but the text gets a little messed up another problem is when you hold down the shift key, a bunch of non-supported characters appear(those little black bars)

    you could filter this out, but I didnt really want to because it was just a small experiment.

    [Edited by denniswrenn on 08-09-2000 at 02:04 PM]

  7. #7
    Guest
    Dennis: Your keylogger will detect a whole lot of jargon if you use all of the keys. I suggest you ignore the keys before 32.

  8. #8
    Junior Member
    Join Date
    Nov 1999
    Posts
    23
    While we're on the subject of key capturing,
    How do I detect if the shift, ctrl, alt, etc. keys were pressed while a specific key was pressed?
    I know that a keydown event on a form can detect the shift, ctrl, and alt keys along with the actual pressing of any key at any time. A form's keydown event could theoretically be a key logger, however it would not capture if it was not the active window. (unless there's some way to make the form a system service??)
    The timer and for/next loop that was suggested does seem to work on new machines (considering the typing speed of most computer users), however if I was to run a program like that on my old 486/33 with 8megs RAM (just barely running Win95) some keys might perhaps be missed?
    It's too bad the API doesn't have events as well as methods that can be called.

    Thanks

  9. #9
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Here's one method for getting about as close to a KeyLogger as you can without using a DLL to subclass the Keyboard Handler:
    Code:
    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()
        Dim iKey As Integer
        
        'First flush the Buffer of any residual key presses
        For iKey = 0 To 255
            While GetAsyncKeyState(iKey)
            Wend
        Next
        
        'Need a Very Low Interval to Ensure the Keys are Captured in the Correct Order
        Timer1.Interval = 1
        Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
        Dim iKey As Integer
        Dim iAsc As Long
        Dim bKeys(255) As Byte
        
        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
        Next
        If iKey < 256 Then
            'Get the Current Keyboard State For the Shift Keys Etc..
            Call GetKeyboardState(bKeys(0))
            While GetAsyncKeyState(iKey)
                'Wait for Key to be Released
            Wend
            'Conver the Key to it's ASCII Equivilant
            Call ToAscii(iKey, 0&, bKeys(0), iAsc, 0&)
            If iAsc Then
                'Store Keypress to Log Here
                Debug.Print Chr(iAsc);
            End If
        End If
    End Sub

  10. #10

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    15

    Unhappy Shift, Ctrl, Alt - arrgh

    So using the getasynckeystate function, is there a way to figure out if the SHIFT, CTRL, ALT keys etc. have been pressed aswell as another key.

    like CTRL + 6
    or CTRL + ALT + F7

    thanks to anyone who can help

    If practice makes perfect...

    and nobody's perfect...

    why the hell practice?

  11. #11

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    15

    Angry

    Please can someone help. thankyou
    If practice makes perfect...

    and nobody's perfect...

    why the hell practice?

  12. #12
    Guest
    Try this:
    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Const VK_MENU = &H12
    Const VK_CONTROL = &H11
    Const VK_SHIFT = &H10
    
    Private Sub Timer1_Timer()
    
        For I = 3 To 255
            If I = 16 Or I = 17 Or I = 18 Then GoTo EX:
            If GetAsyncKeyState(I) And GetAsyncKeyState(VK_SHIFT) Then Print Chr(I) & "+SHIFT"
            If GetAsyncKeyState(I) And GetAsyncKeyState(VK_CONTROL) Then Print Chr(I) & "+CTRL"
            If GetAsyncKeyState(I) And GetAsyncKeyState(VK_MENU) Then Print Chr(I) & "+ALT"
            If GetAsyncKeyState(I) Then Print Chr(I)
    EX:
        Next I
        
    End Sub

  13. #13
    Junior Member
    Join Date
    Jul 2000
    Location
    Antwerp,Belgium
    Posts
    21

    Question question

    Hi,

    I need a similar thing.
    I want to catch the use of the keycombination Ctrl+c.
    The thing is, I want to do this during a lengtly operation
    (copy of a large file for example).

    I tried the GetAsyncKeyState api but it seems not to detect the use of the keys. I think this is because the form doesn't have focus.

    Can anyone help me?

    God and coincidence are two big humorists

  14. #14
    Guest
    This should work regardless of whether the Form has the focus or now. Insert it into a Form with a Timer (set the Timer's interval to 1).
    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    
    Private Sub Timer1_Timer()
        If GetAsyncKeyState(vbKeyC) And GetAsyncKeyState(vbKeyControl) Then
            Open "C:\MyLog.txt" For Append As #1
            Print #1, "CTRL+C WAS PRESSED AT" & Now
            Close #1
        End If
    End Sub

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