PDA

Click to See Complete Forum and Search --> : To store key strokes received by other applications


Aparna Argade
Nov 29th, 1999, 01:16 PM
I want to store key strokes received by other application through my VB application. We can send key strokes to other application by using Shell,AppActivate & SendKeys commands. In a reverse manner, I want to know to the key strokes received by other application & store it. How to do it?

msdnexpert
Nov 29th, 1999, 05:53 PM
What do you need to store the keystrokes? Can you please elaborate on your application?

Aaron Young
Nov 30th, 1999, 01:53 AM
Here's some code I've just finished which Logs all Keyboard Input regardless of which Application is recieving it.

It's really only the Skeleton of any kind of Key Log Program but should be easily configured to meet your specific needs:

Add a Timer Control to your 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

For iKey = 0 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



------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
adyoung@win.bright.net

Aparna Argade
Nov 30th, 1999, 12:57 PM
Thanks a lot for your help.
I've one more problem. The above code will receive all the key strokes. Many applications may be running. Can I put a filter so that I'll know the key operations made on a perticular application ?
Suppose a,b,c,d are the applications running. Our this program may be say 'd'. Now I want 'd' to store key operation made for 'a' application & should not store keys for 'b', 'c'.

Aaron Young
Nov 30th, 1999, 07:54 PM
What you could do is find out the Window Handle of the Form/Window you wish to Recieve Input for, then in the Routine Check if that Window is the ForeGround Window with the GetForegroundWindow API.

------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
adyoung@win.bright.net