Solved

Capture Key Stroke in vb

Posted on 2003-10-30
14
1,103 Views
Last Modified: 2008-02-01
Hi,

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

any ideas?
0
Comment
Question by:phyderous
  • 3
  • 3
  • 3
  • +4
14 Comments
 
LVL 3

Expert Comment

by:gafoorgk
ID: 9649882
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
 
LVL 5

Expert Comment

by:rajaamirapu
ID: 9649884
In the form_keydown event you can capture the keyascii.
0
 
LVL 5

Expert Comment

by:rajaamirapu
ID: 9649885
In the form_keypress event you can capture the keyascii.
0
 
LVL 5

Expert Comment

by:rajaamirapu
ID: 9649906
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
 
LVL 5

Author Comment

by:phyderous
ID: 9650690
gafoorgk,

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


rajaamirapu,

Your code only work if the form is active ....
0
 
LVL 2

Expert Comment

by:cofneverlivetotell
ID: 9652685
Is it jsut me or does the 'WinSubHook' search not do anything?
0
 
LVL 3

Expert Comment

by:gafoorgk
ID: 9652859
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 2

Expert Comment

by:cofneverlivetotell
ID: 9652879
lol, ok cheers
0
 
LVL 5

Author Comment

by:phyderous
ID: 9653017
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
 
LVL 5

Author Comment

by:phyderous
ID: 9653102
I was hoping for a better way than what spy++ is doing
0
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 250 total points
ID: 9654567
0
 

Expert Comment

by:vbwizardxxx
ID: 9654861
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
 
LVL 5

Expert Comment

by:fantasy1001
ID: 9655236
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
 
LVL 3

Expert Comment

by:gafoorgk
ID: 9655977
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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now