Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Capture Key Stroke in vb

Posted on 2003-10-30
14
Medium Priority
?
1,132 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 1000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

824 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