Capture Key Stroke in vb

Hi,

I need to capture all key strokes in the keyboard with a backround process written in vb

any ideas?
LVL 5
phyderousAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gafoorgkCommented:
u can do it very easily with hook library from www.vbaccelerator.com
or
there is a better class and type library in www.planetsourcecode.com/vb. go there and search for 'WinSubHook'
0
rajaamirapuCommented:
In the form_keydown event you can capture the keyascii.
0
rajaamirapuCommented:
In the form_keypress event you can capture the keyascii.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

rajaamirapuCommented:
Sorry you need to use the keyboardhook.
here is the code

'In a module
Public Const WH_KEYBOARD = 2
Public Const VK_SHIFT = &H10
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public hHook As Long
Public Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    'if idHook is less than zero, no further processing is required
    If idHook < 0 Then
        'call the next hook
        KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
    Else
        'check if SHIFT-S is pressed
        If (GetKeyState(VK_SHIFT) And &HF0000000) And wParam = Asc("S") Then
            'show the result
            Form1.Print "Shift-S pressed ..."
        End If
        'call the next hook
        KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
    End If
End Function

'In a form, called Form1
Private Sub Form_Load()
    'KPD-Team 2000
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    'set a keyboard hook
    hHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, App.hInstance, App.ThreadID)
End Sub
Private Sub Form_Unload(Cancel As Integer)
    'remove the windows-hook
    UnhookWindowsHookEx hHook
End Sub
0
phyderousAuthor Commented:
gafoorgk,

I d/l the tlb now what ... I need some code sample ...


rajaamirapu,

Your code only work if the form is active ....
0
cofneverlivetotellCommented:
Is it jsut me or does the 'WinSubHook' search not do anything?
0
gafoorgkCommented:
as i told u, u can find the sample source code in www.planetsourcode.com/vb

i'm sorry for the search string i gave u. use 'subclass thunk' as search string. u'll get the sample. i've not seen a better one than this.
0
cofneverlivetotellCommented:
lol, ok cheers
0
phyderousAuthor Commented:
gafoorgk,

Thanks for the new info, but this is not what I want

the message arrived only when the form is active.

I need to monitor the message all the time like we did in the good old asm.

in asm I used to hook to the keyboard inturrept and get each key stroke event if it was made in a diffrent program

the problem is I know how to do this in DOS I don't have a clue how to do it in windows.


what I need for example is

I am typing somthing in notepad I want the vb to run in the background capturing all what I wrote in notepad.

Thanks,

0
phyderousAuthor Commented:
I was hoping for a better way than what spy++ is doing
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vbwizardxxxCommented:
Here's a simple way of capturing all keystrokes, you can modify it to suit which keys you want to capture.
Put at timer (called Timer1) on your form.

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

'string to store all captured keystrokes
Dim str As String

Private Sub Form_Load()
make it not visible it task bar.
'App.TaskVisible = False
Timer1.Interval = 1
End Sub

Private Sub Timer1_Timer()
For i = 2 To 255
result = 0
result = GetAsyncKeyState(i)
If result = -32767 Then
str = str & Chr(i)
End If
Next i
End Sub

The string str now contains all the captured keystrokes. Notice however, that many of the keystrokes captured you might not want. For instance if you only want to capture the letters of the alphabet change For i = 2 to 255  to  For i = 65 To 90. (65-90 is the ascii for A-Z).

Hope this helps :)
0
fantasy1001Commented:
Step:
Create a new form with a timer, and a textbox. named: timer1, text1
Set timer1 interval to 1 and enabled
Then add this code to your form:

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Dim result As Integer

Private Sub Timer1_Timer()
   For i = 1 To 255
      result = 0
      result = GetAsyncKeyState(i)

      If result = -32767 Then
         Text1.Text = Text1.Text + Chr(i)
      End if
   Next i
End Sub

DeMo:
Try pressing the keyboard while you open others window, the pressed key will appear on the textbox,

                                                                      Thanks,
                                                                   ~ Fantasy ~
0
gafoorgkCommented:
if u experiment with that code, u can see that the form can be removed and it can be done in a startup module. and u can even start that application when windows starts.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.